mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
added unique filtering and config limit param
I hacked some code from other modules and some quick googling, first time with py so this may need to be cleaned up. The functionality is to have a config var in pythonconfig.py and to remove the duplicates `sorted(set())`
This commit is contained in:
parent
ccef2a6f13
commit
11bdb437d2
1 changed files with 23 additions and 4 deletions
|
|
@ -13,14 +13,21 @@ To enable, add
|
|||
from pelican.plugins import related_posts
|
||||
PLUGINS = [related_posts]
|
||||
|
||||
to your settings's file pelicanconf.py .
|
||||
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>{{ related_post }}</li>
|
||||
<li><a href="{{ related_post.url }}">{{ related_post.title }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
|
@ -40,12 +47,24 @@ def add_related_posts(generator, metadata):
|
|||
|
||||
if len(related_posts) < 1:
|
||||
return
|
||||
|
||||
metadata["related_posts"] = sorted(set(related_posts))
|
||||
|
||||
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', {})
|
||||
|
||||
metadata["related_posts"] = ranked_related[:5]
|
||||
#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]
|
||||
|
||||
|
||||
def register():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue