diff options
author | Sebastian Krzyszkowiak <dos@dosowisko.net> | 2014-06-15 23:35:27 +0200 |
---|---|---|
committer | Sebastian Krzyszkowiak <dos@dosowisko.net> | 2014-06-15 23:35:27 +0200 |
commit | c4f74cf04eef62477e93a3ddfa00e4b574849202 (patch) | |
tree | 4707d7981b0338ad27dddee2f888602d767fce9c | |
parent | 9c8be3c54ff28ee3201366e8afcd9ee3fca865ed (diff) | |
download | www-c4f74cf04eef62477e93a3ddfa00e4b574849202.tar.gz www-c4f74cf04eef62477e93a3ddfa00e4b574849202.tar.bz2 www-c4f74cf04eef62477e93a3ddfa00e4b574849202.zip |
add prev/next links to articles
Signed-off-by: Sebastian Krzyszkowiak <dos@dosowisko.net>
-rw-r--r-- | pelicanconf.py | 5 | ||||
-rw-r--r-- | plugins/neighbors/Readme.rst | 97 | ||||
-rw-r--r-- | plugins/neighbors/__init__.py | 1 | ||||
-rwxr-xr-x | plugins/neighbors/neighbors.py | 58 | ||||
-rw-r--r-- | theme/templates/article.html | 7 |
5 files changed, 167 insertions, 1 deletions
diff --git a/pelicanconf.py b/pelicanconf.py index d5c85a2..32ce4e2 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -39,4 +39,7 @@ ARTICLE_URL = "news/{slug}" ARTICLE_SAVE_AS = "news/{slug}.html" PAGE_URL = "{slug}" -PAGE_SAVE_AS = "{slug}.html"
\ No newline at end of file +PAGE_SAVE_AS = "{slug}.html" + +PLUGIN_PATH = 'plugins' +PLUGINS = ['neighbors'] diff --git a/plugins/neighbors/Readme.rst b/plugins/neighbors/Readme.rst new file mode 100644 index 0000000..573b914 --- /dev/null +++ b/plugins/neighbors/Readme.rst @@ -0,0 +1,97 @@ +Neighbor Articles Plugin for Pelican +==================================== + +This plugin adds ``next_article`` (newer) and ``prev_article`` (older) +variables to the article's context. + +Also adds ``next_article_in_category`` and ``prev_article_in_category``. + + +Usage +----- + +.. code-block:: html+jinja + + <ul> + {% if article.prev_article %} + <li> + <a href="{{ SITEURL }}/{{ article.prev_article.url}}"> + {{ article.prev_article.title }} + </a> + </li> + {% endif %} + {% if article.next_article %} + <li> + <a href="{{ SITEURL }}/{{ article.next_article.url}}"> + {{ article.next_article.title }} + </a> + </li> + {% endif %} + </ul> + <ul> + {% if article.prev_article_in_category %} + <li> + <a href="{{ SITEURL }}/{{ article.prev_article_in_category.url}}"> + {{ article.prev_article_in_category.title }} + </a> + </li> + {% endif %} + {% if article.next_article %} + <li> + <a href="{{ SITEURL }}/{{ article.next_article_in_category.url}}"> + {{ article.next_article_in_category.title }} + </a> + </li> + {% endif %} + </ul> + +Usage with the Subcategory plugin +--------------------------------- + +If you want to get the neigbors within a subcategory it's a little different. +Since an article can belong to more than one subcategory, subcategories are +stored in a list. If you have an article with subcategories like + +``Category/Foo/Bar`` + +it will belong to both subcategory Foo, and Foo/Bar. Subcategory neighbors are +added to an article as ``next_article_in_subcategory#`` and +``prev_article_in_subcategory#`` where ``#`` is the level of subcategory. So using +the example from above, subcategory1 will be Foo, and subcategory2 Foo/Bar. +Therefor the usage with subcategories is: + +.. code-block:: html+jinja + + <ul> + {% if article.prev_article_subcategory1 %} + <li> + <a href="{{ SITEURL }}/{{ article.prev_article_in_subcategory1.url}}"> + {{ article.prev_article_in_subcategory1.title }} + </a> + </li> + {% endif %} + {% if article.next_article %} + <li> + <a href="{{ SITEURL }}/{{ article.next_article_subcategory1.url}}"> + {{ article.next_article_subcategory1.title }} + </a> + </li> + {% endif %} + </ul> + <ul> + {% if article.prev_article_in_subcategory2 %} + <li> + <a href="{{ SITEURL }}/{{ article.prev_article_in_subcategory2.url}}"> + {{ article.prev_article_in_subcategory2.title }} + </a> + </li> + {% endif %} + {% if article.next_article %} + <li> + <a href="{{ SITEURL }}/{{ article.next_article_in_subcategory2.url}}"> + {{ article.next_article_in_subcategory2.title }} + </a> + </li> + {% endif %} + </ul> + diff --git a/plugins/neighbors/__init__.py b/plugins/neighbors/__init__.py new file mode 100644 index 0000000..9038d7e --- /dev/null +++ b/plugins/neighbors/__init__.py @@ -0,0 +1 @@ +from .neighbors import * diff --git a/plugins/neighbors/neighbors.py b/plugins/neighbors/neighbors.py new file mode 100755 index 0000000..b65fcfe --- /dev/null +++ b/plugins/neighbors/neighbors.py @@ -0,0 +1,58 @@ +# -*- 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 get_translation(article, prefered_language): + if not article: + return None + for translation in article.translations: + if translation.lang == prefered_language: + return translation + return article + +def set_neighbors(articles, next_name, prev_name): + for nxt, cur, prv in iter3(articles): + exec("cur.{} = nxt".format(next_name)) + exec("cur.{} = prv".format(prev_name)) + + for translation in cur.translations: + exec( + "translation.{} = get_translation(nxt, translation.lang)".format( + next_name)) + exec( + "translation.{} = get_translation(prv, translation.lang)".format( + prev_name)) + +def neighbors(generator): + set_neighbors(generator.articles, 'next_article', 'prev_article') + + for category, articles in generator.categories: + articles.sort(key=(lambda x: x.date), reverse=(True)) + set_neighbors( + articles, 'next_article_in_category', 'prev_article_in_category') + + if hasattr(generator, 'subcategories'): + for subcategory, articles in generator.subcategories: + articles.sort(key=(lambda x: x.date), reverse=(True)) + index = subcategory.name.count('/') + next_name = 'next_article_in_subcategory{}'.format(index) + prev_name = 'prev_article_in_subcategory{}'.format(index) + set_neighbors(articles, next_name, prev_name) + +def register(): + signals.article_generator_finalized.connect(neighbors) diff --git a/theme/templates/article.html b/theme/templates/article.html index 8481d80..095718a 100644 --- a/theme/templates/article.html +++ b/theme/templates/article.html @@ -69,6 +69,13 @@ </div> {% endif %} + {% if article.next_article %} + <p style="float: right"><a href="/{{article.next_article.url}}">{{article.next_article.title}} »</a></p> + {% endif %} + {% if article.prev_article %} + <p><a href="/{{article.prev_article.url}}">« {{article.prev_article.title}}</a></p> + {% endif %} + <hr /> <a style="float: right; margin: 3px 35px" href="http://neo900.org/" class="btn btn-default">Learn more</a> |