Merge pull request #813 from avaris/fix_related_posts

fixed related_posts plugin
This commit is contained in:
Justin Mayer 2013-03-27 09:28:56 -07:00
commit 024ecb301e
2 changed files with 23 additions and 59 deletions

View file

@ -291,10 +291,10 @@ Related posts
------------- -------------
This plugin adds the ``related_posts`` variable to the article's context. 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 RELATED_POSTS_MAX = 10
PLUGINS = [related_posts]
You can then use the ``article.related_posts`` variable in your templates. You can then use the ``article.related_posts`` variable in your templates.
For example:: For example::

View file

@ -1,71 +1,35 @@
from pelican import signals
""" """
Related posts plugin for Pelican Related posts plugin for Pelican
================================ ================================
Adds related_posts variable to article's context 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 %}
<ul>
{% for related_post in article.related_posts %}
<li><a href="{{ related_post.url }}">{{ related_post.title }}</a></li>
{% endfor %}
</ul>
{% endif %}
""" """
related_posts = [] from pelican import signals
from collections import Counter
def add_related_posts(generator, metadata): def add_related_posts(generator):
if 'tags' in metadata: # get the max number of entries from settings
for tag in metadata['tags']: # or fall back to default (5)
#print tag numentries = generator.settings.get('RELATED_POSTS_MAX', 5)
for related_article in generator.tags[tag]:
related_posts.append(related_article)
if len(related_posts) < 1: for article in generator.articles:
return # no tag, no relation
if not hasattr(article, 'tags'):
metadata["related_posts"] = sorted(set(related_posts)) continue
relation_score = dict(list(zip(set(related_posts), list(map(related_posts.count, # score = number of common tags
set(related_posts)))))) scores = Counter()
ranked_related = sorted(relation_score, key=relation_score.get) for tag in article.tags:
scores += Counter(generator.tags[tag])
#Load the confg file and get the number of entries specified there
settings = generator.settings
config = settings.get('RELATED_POSTS', {})
#check if the related_posts var is set in the pythonconfig.py # remove itself
if not isinstance(config, dict): scores.pop(article)
info("realted_links plugin: Using default number of related links ("+numentries+")")
else: article.related_posts = [other for other, count
numentries = config.get('numentries', 5) in scores.most_common(numentries)]
metadata["related_posts"] = ranked_related[:numentries]
def register(): def register():
signals.article_generate_context.connect(add_related_posts) signals.article_generator_finalized.connect(add_related_posts)