page-relations.py - Forward-Backward-Relations

Forward-Backward-Relations by Numerical Suffixes

When slug without numerical suffix match, pages are not identical, share the language and the numerical suffix differs exactly by 1, then the prequel and sequel fields are set. This allows for a lateral navigation, as exemplified on this page.

    # set prequel and sequel for pages below toplevel
    for page in _all_pages():
        if page.parent != None:
            page_stripped_slug = page.slug.rstrip('0123456789')
            if len(page.slug) != len(page_stripped_slug):
                for sibbling_page in page.parent.children:
                    sibbling_stripped_slug = sibbling_page.slug.rstrip('0123456789')
                    if len(sibbling_page.slug) != len(sibbling_stripped_slug):
                        if sibbling_page != page and sibbling_page.lang == page.lang and sibbling_stripped_slug == page_stripped_slug:
                            page_number = int(page.slug[len(page_stripped_slug):].lstrip('0'))
                            sibbling_number = int(sibbling_page.slug[len(sibbling_stripped_slug):].lstrip('0'))
                            if page_number + 1 == sibbling_number:
                                page.sequel = sibbling_page
                                sibbling_page.prequel = page

Finally the complete list of prequels and sequels are computed. This would simplify go-first or go-last jumps.

    # set list of prequels and sequels
    for page in _all_pages():
        a_prequel = page.prequel
        while a_prequel:
            page.prequels.insert(0, a_prequel)
            a_prequel = a_prequel.prequel
        a_sequel = page.sequel
        while a_sequel:
            page.sequels.insert(0, a_sequel)
            a_sequel = a_sequel.sequel

Copyright © 2016 Uwe Ritzmann - Created with Pelican, Python and Skeleton.