# -*- 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)

@@ -415,143 +415,580 @@
 def 
-neighbors(generator):%0A    for nxt, cur, prv in iter3(generator.articles):%0A        cur.next_article = nxt%0A        cur.prev_article = prv
+get_translation(article, prefered_language):%0A    if not article:%0A        return None%0A    for translation in article.translations:%0A        if translation.lang == prefered_language:%0A            return translation%0A    return article%0A%0Adef neighbors(generator):%0A    for nxt, cur, prv in iter3(generator.articles):%0A        cur.next_article = nxt%0A        cur.prev_article = prv%0A%0A        for translation in cur.translations:%0A            translation.next_article = get_translation(nxt, translation.lang)%0A            translation.prev_article = get_translation(prv, translation.lang)
 %0A%0Ade

Make neighbors plugin support translated articles.