1
0
Fork 0
forked from github/pelican
pelican-theme/pelican/plugins/related_posts.py

35 lines
895 B
Python
Raw Normal View History

2012-07-11 01:16:19 +05:45
"""
Related posts plugin for Pelican
================================
Adds related_posts variable to article's context
"""
2012-07-11 08:52:00 +05:45
2013-03-26 23:17:59 -04:00
from pelican import signals
from collections import Counter
2012-07-12 15:29:08 +02:00
2013-03-26 23:17:59 -04:00
def add_related_posts(generator):
# get the max number of entries from settings
# or fall back to default (5)
numentries = generator.settings.get('RELATED_POSTS_MAX', 5)
2012-07-12 15:29:08 +02:00
2013-03-26 23:17:59 -04:00
for article in generator.articles:
# no tag, no relation
if not hasattr(article, 'tags'):
continue
2012-07-12 15:29:08 +02:00
2013-03-26 23:17:59 -04:00
# score = number of common tags
scores = Counter()
for tag in article.tags:
scores += Counter(generator.tags[tag])
2012-07-12 15:29:08 +02:00
2013-03-26 23:17:59 -04:00
# remove itself
scores.pop(article)
2013-03-26 23:17:59 -04:00
article.related_posts = [other for other, count
in scores.most_common(numentries)]
2012-07-11 01:16:19 +05:45
2012-07-12 15:29:08 +02:00
2012-07-11 01:16:19 +05:45
def register():
2013-03-26 23:17:59 -04:00
signals.article_generator_finalized.connect(add_related_posts)