From 8710982d2a49afff772305388649f469b3958ac6 Mon Sep 17 00:00:00 2001 From: Deniz Turgut Date: Tue, 26 Mar 2013 23:17:59 -0400 Subject: [PATCH] fixed related_posts plugin --- docs/plugins.rst | 6 +-- pelican/plugins/related_posts.py | 76 +++++++++----------------------- 2 files changed, 23 insertions(+), 59 deletions(-) diff --git a/docs/plugins.rst b/docs/plugins.rst index 3537f16b..9985f25e 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -291,10 +291,10 @@ Related posts ------------- This plugin adds the ``related_posts`` variable to the article's context. -To enable, add the following to your settings file:: +By default, up to 5 articles are listed. You can customize this value by +defining ``RELATED_POSTS_MAX`` in your settings file:: - from pelican.plugins import related_posts - PLUGINS = [related_posts] + RELATED_POSTS_MAX = 10 You can then use the ``article.related_posts`` variable in your templates. For example:: diff --git a/pelican/plugins/related_posts.py b/pelican/plugins/related_posts.py index dc52de9a..81999002 100644 --- a/pelican/plugins/related_posts.py +++ b/pelican/plugins/related_posts.py @@ -1,71 +1,35 @@ -from pelican import signals - """ Related posts plugin for Pelican ================================ Adds related_posts variable to article's context - -Settings --------- -To enable, add - - from pelican.plugins import related_posts - PLUGINS = [related_posts] - -to your pelicanconf.py. - -Control the number of entries with in the config file with: - -RELATED_POSTS = { - 'numentries': 6, -} - - -Usage ------ - {% if article.related_posts %} - - {% endif %} - - """ -related_posts = [] +from pelican import signals +from collections import Counter -def add_related_posts(generator, metadata): - if 'tags' in metadata: - for tag in metadata['tags']: - #print tag - for related_article in generator.tags[tag]: - related_posts.append(related_article) +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) - if len(related_posts) < 1: - return - - metadata["related_posts"] = sorted(set(related_posts)) + for article in generator.articles: + # no tag, no relation + if not hasattr(article, 'tags'): + continue - relation_score = dict(list(zip(set(related_posts), list(map(related_posts.count, - set(related_posts)))))) - ranked_related = sorted(relation_score, key=relation_score.get) - - #Load the confg file and get the number of entries specified there - settings = generator.settings - config = settings.get('RELATED_POSTS', {}) + # score = number of common tags + scores = Counter() + for tag in article.tags: + scores += Counter(generator.tags[tag]) - #check if the related_posts var is set in the pythonconfig.py - if not isinstance(config, dict): - info("realted_links plugin: Using default number of related links ("+numentries+")") - else: - numentries = config.get('numentries', 5) - - metadata["related_posts"] = ranked_related[:numentries] + # remove itself + scores.pop(article) + + article.related_posts = [other for other, count + in scores.most_common(numentries)] def register(): - signals.article_generate_context.connect(add_related_posts) + signals.article_generator_finalized.connect(add_related_posts) \ No newline at end of file