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.'))