Allow to export the navbar in several formats

This commit is contained in:
Rodolphe Breard 2018-02-17 19:03:37 +01:00
parent 5d367f3f49
commit a09ff2edc1
5 changed files with 103 additions and 12 deletions

View file

View file

View file

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

View file

@ -0,0 +1,9 @@
<?php
{% for lang in output_langs %}
if ($conf['lang'] == "{{ lang.name }}") {
?>
{{ lang.html|safe }}
<?php
}
{% endfor %}

View file

@ -21,28 +21,33 @@ def get_lang_name(lang_code):
return li['name_local'] return li['name_local']
def get_lang_data(request, lang_code, curr_lang): def get_lang_data(path_info, lang_code, curr_lang):
lang_name = get_lang_name(lang_code) lang_name = get_lang_name(lang_code)
rm = resolve(request.path_info) rm = resolve(path_info)
activate_lang(lang_code) activate_lang(lang_code)
lang_url = reverse(rm.func, args=rm.args, kwargs=rm.kwargs) lang_url = reverse(rm.func, args=rm.args, kwargs=rm.kwargs)
activate_lang(curr_lang) activate_lang(curr_lang)
return (lang_code, lang_name, lang_url) return (lang_code, lang_name, lang_url)
def get_base_context(lang, url):
return {
'user': None,
'current_url': url,
'elems': Element.objects.filter(parent=None).order_by('weight'),
'current_lang_code': lang,
'current_lang_name': get_lang_name(lang),
'all_langs': [
get_lang_data(url, l[0], lang) for l in settings.LANGUAGES
],
}
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def navbar(context): def navbar(context):
request = context['request'] request = context['request']
curr_lang = get_language() curr_lang = get_language()
ctx = { ctx = get_base_context(curr_lang, request.path_info)
'user': request.user, ctx['user'] = request.user
'current_url': request.path_info,
'elems': Element.objects.filter(parent=None).order_by('weight'),
'current_lang_code': curr_lang,
'current_lang_name': get_lang_name(curr_lang),
'all_langs': [
get_lang_data(request, l[0], curr_lang) for l in settings.LANGUAGES
],
}
tpl = TemplateResponse(request, 'navbar/navbar.html', context=ctx).render() tpl = TemplateResponse(request, 'navbar/navbar.html', context=ctx).render()
return mark_safe(tpl.rendered_content) return mark_safe(tpl.rendered_content)