langmenu.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. '''
  2. This plugin attemps to create something similar to menuitems,
  3. but more meaningful with respect to l10n
  4. '''
  5. from __future__ import print_function
  6. from pelican import signals
  7. def add_localmenuitems(generator):
  8. menu = {} # lang: list of pages
  9. for page in generator.context['pages']:
  10. menu.setdefault(page.lang, [])
  11. for tr in page.translations:
  12. menu.setdefault(tr.lang, [])
  13. print('we have langs ' + ','.join(menu.keys()))
  14. for page in sorted(generator.context['pages'],
  15. key=lambda x: int(x.navbar_sort)):
  16. defined_langs = []
  17. menu[page.lang].append(page)
  18. defined_langs.append(page.lang)
  19. for tr in page.translations:
  20. menu[tr.lang].append(tr)
  21. defined_langs.append(tr.lang)
  22. for lang in menu.keys():
  23. if lang not in defined_langs:
  24. menu[lang].append(page)
  25. menuitems = {}
  26. for lang in menu:
  27. menuitems[lang] = []
  28. for page in menu[lang]:
  29. menuitems[lang].append((page.title, page.url))
  30. generator.context['LOCALMENUITEMS'] = menuitems
  31. def register():
  32. signals.page_generator_finalized.connect(add_localmenuitems)