neighbors.py 630 B

12345678910111213141516171819202122232425262728
  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 neighbors(generator):
  18. for nxt, cur, prv in iter3(generator.articles):
  19. cur.next_article = nxt
  20. cur.prev_article = prv
  21. def register():
  22. signals.article_generator_finalized.connect(neighbors)