From a09ff2edc15c1112732094f58d0a2e2724fb724d Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 17 Feb 2018 19:03:37 +0100 Subject: [PATCH] Allow to export the navbar in several formats --- navbar/management/__init__.py | 0 navbar/management/commands/__init__.py | 0 navbar/management/commands/navbar_export.py | 77 ++++++++++++++++++++ navbar/templates/navbar/export_docuwiki.html | 9 +++ navbar/templatetags/navbar.py | 29 +++++--- 5 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 navbar/management/__init__.py create mode 100644 navbar/management/commands/__init__.py create mode 100644 navbar/management/commands/navbar_export.py create mode 100644 navbar/templates/navbar/export_docuwiki.html diff --git a/navbar/management/__init__.py b/navbar/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/navbar/management/commands/__init__.py b/navbar/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/navbar/management/commands/navbar_export.py b/navbar/management/commands/navbar_export.py new file mode 100644 index 0000000..ef54b17 --- /dev/null +++ b/navbar/management/commands/navbar_export.py @@ -0,0 +1,77 @@ +from django.core.management.base import BaseCommand, CommandError +from django.template.loader import render_to_string +from django.conf import settings +from navbar.templatetags.navbar import get_base_context +import os + + +class Command(BaseCommand): + help = 'Generates the CSS files used by Pygments' + + def export_static(self, curr_lang): + self.stdout.write('Plain HTML export for language %s…' % curr_lang) + output_dir = os.path.join(self.export_dir, 'html') + output_file = 'khanav_%s.html' % curr_lang + os.makedirs(output_dir, mode=0o755, exist_ok=True) + with open(os.path.join(output_dir, output_file), 'w') as f: + ctx = get_base_context(curr_lang, '/') + html = render_to_string('navbar/navbar.html', ctx) + f.write(html) + + def export_docuwiki(self, langs): + self.stdout.write( + 'Docuwiki export for languages %s…' % ', '.join(langs) + ) + output_langs = [] + for lang in langs: + ctx = get_base_context(lang, '/') + output_langs.append({ + 'name': lang, + 'html': render_to_string('navbar/navbar.html', ctx) + }) + output_dir = os.path.join(self.export_dir, 'docuwiki') + os.makedirs(output_dir, mode=0o755, exist_ok=True) + with open(os.path.join(output_dir, 'khanav.php'), 'w') as f: + f.write(render_to_string('navbar/export_docuwiki.html', { + 'output_langs': output_langs + })) + + def add_arguments(self, parser): + parser.add_argument( + '--dir', '-d', + help='Export directory.', + default=os.path.join(settings.BASE_DIR, 'build/navbar'), + dest='export_dir' + ) + parser.add_argument( + '--lang', '-l', + help='Language to export.', + action='append', + dest='langs' + ) + parser.add_argument( + '--plain', '--html', + help='Export for plain HTML.', + action='store_true', + dest='static' + ) + parser.add_argument( + '--docuwiki', + help='Export for docuwiki.', + action='store_true', + dest='docuwiki' + ) + + def handle(self, *args, **options): + if options['langs'] is None: + raise CommandError('No language specified.') + if 'all' in options['langs']: + options['langs'] = [name for name, _ in settings.LANGUAGES] + self.export_dir = options['export_dir'] + + if options['static']: + for lang in options['langs']: + self.export_static(lang) + if options['docuwiki']: + self.export_docuwiki(options['langs']) + self.stdout.write(self.style.SUCCESS('Ok.')) diff --git a/navbar/templates/navbar/export_docuwiki.html b/navbar/templates/navbar/export_docuwiki.html new file mode 100644 index 0000000..583b3db --- /dev/null +++ b/navbar/templates/navbar/export_docuwiki.html @@ -0,0 +1,9 @@ + +{{ lang.html|safe }} +