duncanlock.net

Adding Jinja Filters to Pelican

https://github.com/hdeshev/pelican-site-example/blob/master/blog/pelicanconf.py

def popular_articles(articles):
    populars = [article for article in articles if hasattr(article, 'popularity')]

    def comparator(x, y):
        if x.popularity != y.popularity:
            return -1 * cmp(x.popularity, y.popularity)
        else:
            return -1 * cmp(x.date, y.date)

    return sorted(populars, cmp=comparator)


def media_url(url, site_root):
    if re.match("^https?://", "https://yahoo.com", re.IGNORECASE):
        return url
    else:
        return "%s/%s" %(site_root, url)


JINJA_FILTERS = {
    "popular_articles": popular_articles,
    "media_url": media_url,
}

Related Posts


Comments