From 102fde3931f5675c9294c08f1f26386a058a296e Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 25 Jun 2015 13:34:13 +0200 Subject: [PATCH] langmenu plugin --- pelicanconf.py | 2 ++ plugins/langmenu.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 plugins/langmenu.py diff --git a/pelicanconf.py b/pelicanconf.py index d7726b1..64681f2 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -48,3 +48,5 @@ THEME = 'themes/bs3' BOOTSTRAP_THEME = 'cyborg' HIDE_SIDEBAR = True +PLUGIN_PATHS = ['plugins'] +PLUGINS = ['langmenu'] diff --git a/plugins/langmenu.py b/plugins/langmenu.py new file mode 100644 index 0000000..ccf865a --- /dev/null +++ b/plugins/langmenu.py @@ -0,0 +1,42 @@ +''' +This plugin attemps to create something similar to menuitems, +but more meaningful with respect to l10n +''' +from __future__ import print_function + +from pelican import signals + + +def add_localmenuitems(generator): + menu = {} # lang: list of pages + for page in generator.context['pages']: + menu.setdefault(page.lang, []) + for tr in page.translations: + menu.setdefault(tr.lang, []) + print('we have langs ' + ','.join(menu.keys())) + for page in sorted(generator.context['pages'], + key=lambda x: x.navbar_sort): + defined_langs = [] + menu[page.lang].append(page) + defined_langs.append(page.lang) + for tr in page.translations: + menu[tr.lang].append(tr) + defined_langs.append(tr.lang) + for lang in menu.keys(): + if lang not in defined_langs: + menu[lang].append(page) + + menuitems = {} + for lang in menu: + menuitems[lang] = [] + for page in menu[lang]: + menuitems[lang].append((page.title, page.url)) + + print(menuitems['en']) + print(menuitems['it']) + generator.context['LOCALMENUITEMS'] = menuitems + + +def register(): + signals.page_generator_finalized.connect(add_localmenuitems) + pass