Skip to content

Commit

Permalink
add --generate-redirects to the distill-publish command, related to #82
Browse files Browse the repository at this point in the history
  • Loading branch information
meeb committed May 24, 2024
1 parent d7e6599 commit aeaf642
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ urlpatterns = (

### Parameters in file names

You can standard Python string formatting in `distill_file` as well to enable
You can use standard Python string formatting in `distill_file` as well to enable
you to change the output file path for a file if you wish. Note this does not
update the URL used by Django so if you use this make sure your `path` pattern
matches the `distill_file` pattern or your links might not work in Django. An
Expand Down Expand Up @@ -322,6 +322,11 @@ to update most of them, and you don't care if old files remain on the server.
`--parallel-publish [number of threads]`: Publish files in parallel on multiple
threads, this can speed up publishing. Defaults to `1` thread.

`--generate-redirects`: Attempt to generate static redirects stored in the
`django.contrib.redirects` app. If you have a redirect from `/old/` to `/new/` using
this flag will create a static HTML `<meta http-equiv="refresh" content="...">`
style redirect at `/old/index.html` to `/new/`.

**Note** that this means if you use `--force` and `--quiet` that the output
directory will have all files not part of the site export deleted without any
confirmation.
Expand Down
2 changes: 1 addition & 1 deletion django_distill/management/commands/distill-local.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def handle(self, *args, **options):
raise CommandError(str(err)) from err
stdout('')
if generate_redirects:
stdout('Generating redirects...')
stdout('Generating redirects')
render_redirects(output_dir, stdout)
stdout('')
stdout('Site generation complete.')
9 changes: 7 additions & 2 deletions django_distill/management/commands/distill-publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django_distill.distill import urls_to_distill
from django_distill.errors import DistillError
from django_distill.renderer import (run_collectstatic, render_to_dir,
copy_static_and_media_files)
copy_static_and_media_files, render_redirects)
from django_distill.publisher import publish_dir


Expand All @@ -25,6 +25,7 @@ def add_arguments(self, parser):
parser.add_argument('--skip-verify', dest='skip_verify', action='store_true')
parser.add_argument('--ignore-remote-content', dest='ignore_remote_content', action='store_true')
parser.add_argument('--parallel-publish', dest='parallel_publish', type=int, default=1)
parser.add_argument('--generate-redirects', dest='generate_redirects', action='store_true')

def _quiet(self, *args, **kwargs):
pass
Expand All @@ -50,6 +51,7 @@ def handle(self, *args, **options):
ignore_remote_content = options.get('ignore_remote_content', False)
quiet = options.get('quiet')
force = options.get('force')
generate_redirects = options.get('generate_redirects')
if quiet:
stdout = self._quiet
else:
Expand Down Expand Up @@ -93,9 +95,12 @@ def handle(self, *args, **options):
except DistillError as err:
raise CommandError(str(err)) from err
stdout('')
if generate_redirects:
stdout('Generating redirects')
render_redirects(output_dir, stdout)
stdout('')
stdout('Publishing site')
backend.index_local_files()
publish_dir(backend, stdout, not skip_verify, parallel_publish, ignore_remote_content)

stdout('')
stdout('Site generation and publishing complete.')

0 comments on commit aeaf642

Please sign in to comment.