neighbors.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. """
  3. Neighbor Articles Plugin for Pelican
  4. ====================================
  5. This plugin adds ``next_article`` (newer) and ``prev_article`` (older)
  6. variables to the article's context
  7. """
  8. from pelican import signals
  9. def iter3(seq):
  10. it = iter(seq)
  11. nxt = None
  12. cur = next(it)
  13. for prv in it:
  14. yield nxt, cur, prv
  15. nxt, cur = cur, prv
  16. yield nxt, cur, None
  17. def get_translation(article, prefered_language):
  18. if not article:
  19. return None
  20. for translation in article.translations:
  21. if translation.lang == prefered_language:
  22. return translation
  23. return article
  24. def neighbors(generator):
  25. for nxt, cur, prv in iter3(generator.articles):
  26. cur.next_article = nxt
  27. cur.prev_article = prv
  28. for translation in cur.translations:
  29. translation.next_article = get_translation(nxt, translation.lang)
  30. translation.prev_article = get_translation(prv, translation.lang)
  31. def register():
  32. signals.article_generator_finalized.connect(neighbors)