page-relations.py - Page Relations

Setup

Setup and support function get_path are from page_hierachy.py.

from pelican import signals, contents
import os.path
from copy import copy
from itertools import chain

class UnexpectedException(Exception): pass

def get_path(page, settings):
    path = os.path.split(page.get_relative_source_path())[0] + '/'
    path = path.replace( os.path.sep, '/' )

    for prefix in sorted(settings['PAGE_PATHS'], key=len, reverse=True):
        if not prefix.endswith('/'): prefix += '/'
        if path.startswith(prefix):
            relative_path = '/' + path[len(prefix):]
            return relative_path
    raise UnexpectedException('Page outside of PAGE_PATHS setting')

The central function set_relationships begins with initializing the result fields.

def set_relationships(generator):
    def _all_pages():
        return chain(generator.pages, generator.translations)

    # initialize parent, parents and children
    for page in _all_pages():
        page.parent = None
        page.parents = []
        page.children = []

        page.prequel = None
        page.sequel = None
        page.prequels = []
        page.sequels = []
 

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