From 9b77ec15e5beeff3f57f845be883416d2a68b84d Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Mon, 30 Nov 2020 22:56:26 +0100 Subject: New article on rst & Latex. Changed theme --- plugins/related_posts/Readme.rst | 38 ++++++++++++++++++++++++++++++++++ plugins/related_posts/__init__.py | 2 +- plugins/related_posts/related_posts.py | 11 +++++----- 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 plugins/related_posts/Readme.rst mode change 100755 => 100644 plugins/related_posts/related_posts.py (limited to 'plugins/related_posts') diff --git a/plugins/related_posts/Readme.rst b/plugins/related_posts/Readme.rst new file mode 100644 index 0000000..0edf4bd --- /dev/null +++ b/plugins/related_posts/Readme.rst @@ -0,0 +1,38 @@ +Related posts +------------- + +**NOTE:** `This plugin has been moved to its own repository `_. Please file any issues/PRs there. Once all plugins have been migrated to the `new Pelican Plugins organization `_, this monolithic repository will be archived. + +------------------------------------------------------------------------------- + +This plugin adds the ``related_posts`` variable to the article's context. +By default, up to 5 articles are listed. You can customize this value by +defining ``RELATED_POSTS_MAX`` in your settings file:: + + RELATED_POSTS_MAX = 10 + +You can then use the ``article.related_posts`` variable in your templates. +For example:: + + {% if article.related_posts %} + + {% endif %} + + +Your related posts should share a common tag. You can also use ``related_posts:`` in your post's meta data. +The 'related_posts:' meta data works together with your existing slugs:: + + related_posts: slug1, slug2, slug3, ... slugN + +``N`` represents the ``RELATED_POSTS_MAX``. + +Additionally, you can specify:: + + RELATED_POSTS_SKIP_SAME_CATEGORY = True + +in your settings file. With this setting, ``article.related_posts`` will +contain only related posts from categories other than the original article's. diff --git a/plugins/related_posts/__init__.py b/plugins/related_posts/__init__.py index c78861e..057540e 100644 --- a/plugins/related_posts/__init__.py +++ b/plugins/related_posts/__init__.py @@ -1 +1 @@ -from .related_posts import * +from .related_posts import * diff --git a/plugins/related_posts/related_posts.py b/plugins/related_posts/related_posts.py old mode 100755 new mode 100644 index a0cfa15..fbb2426 --- a/plugins/related_posts/related_posts.py +++ b/plugins/related_posts/related_posts.py @@ -14,15 +14,14 @@ 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) - ignore_tags = generator.settings.get('RELATED_POSTS_IGNORE_TAGS', []) # Skip all posts in the same category as the article skipcategory = generator.settings.get('RELATED_POSTS_SKIP_SAME_CATEGORY', False) - for article in generator.articles: + for article in chain(generator.articles, generator.drafts): # set priority in case of forced related posts if hasattr(article,'related_posts'): - # split slugs + # split slugs related_posts = article.related_posts.split(',') - posts = [] + posts = [] # get related articles for slug in related_posts: i = 0 @@ -41,7 +40,7 @@ def add_related_posts(generator): continue # score = number of common tags - related = chain(*(generator.tags[tag] for tag in article.tags if tag.name not in ignore_tags)) + related = chain(*(generator.tags[tag] for tag in article.tags)) if skipcategory: related = (other for other in related if other.category != article.category) @@ -50,7 +49,7 @@ def add_related_posts(generator): # remove itself scores.pop(article, None) - article.related_posts = [other for other, count + article.related_posts = [other for other, count in scores.most_common(numentries)] def register(): -- cgit v1.2.3