From 69a83d3e8fe8806d21eba86df4361052ead02b00 Mon Sep 17 00:00:00 2001 From: Deniz Turgut Date: Wed, 13 Mar 2013 14:39:43 -0400 Subject: [PATCH] Neighbor Articles plugin --- pelicanext/neighbors/README.rst | 44 +++++++++++++++++++++++++++++++ pelicanext/neighbors/neighbors.py | 27 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 pelicanext/neighbors/README.rst create mode 100755 pelicanext/neighbors/neighbors.py diff --git a/pelicanext/neighbors/README.rst b/pelicanext/neighbors/README.rst new file mode 100644 index 0000000..e772fde --- /dev/null +++ b/pelicanext/neighbors/README.rst @@ -0,0 +1,44 @@ +Neighbor Articles Plugin for Pelican +==================================== + +This plugin adds ``next_article`` (newer) and ``prev_article`` (older) +variables to the article's context + +Installation +------------ +To enable, ensure that ``neighbors.py`` is in somewhere you can ``import``. +Then use the following in your `settings`:: + + PLUGINS = ["neighbors"] + +Or you can put the plugin in ``plugins`` folder in pelican installation. You +can find the location by typing:: + + python -c 'import pelican.plugins as p, os; print os.path.dirname(p.__file__)' + +Once you get the folder, copy the ``neighbors.py`` there and use the following +in your settings:: + + PLUGINS = ["pelican.plugins.neighbors"] + +Usage +----- + +.. code-block:: html+jinja + + \ No newline at end of file diff --git a/pelicanext/neighbors/neighbors.py b/pelicanext/neighbors/neighbors.py new file mode 100755 index 0000000..f729b92 --- /dev/null +++ b/pelicanext/neighbors/neighbors.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +""" +Neighbor Articles Plugin for Pelican +==================================== + +This plugin adds ``next_article`` (newer) and ``prev_article`` (older) +variables to the article's context +""" + +from pelican import signals + +def iter3(seq): + it = iter(seq) + nxt = None + cur = next(it) + for prv in it: + yield nxt, cur, prv + nxt, cur = cur, prv + yield nxt, cur, None + +def neighbors(generator): + for nxt, cur, prv in iter3(generator.articles): + cur.next_article = nxt + cur.prev_article = prv + +def register(): + signals.article_generator_finalized.connect(neighbors)