Pelican plugin to create tiny single-page feeds
git clone https://github.com/m-col/pelican-microfeed
Files | Refs | Readme

Commit 1c46921d8abf70e4099f84c5fb672f34e1f28fde
Parent: 
Author: mcol <mcol@posteo.net>
Date: 2019-11-17 15:44:25 +0000
Committer: mcol <mcol@posteo.net>
Committed: 2019-11-17 15:44:25 +0000

init commit with working setup

microfeed.py Added

@@ -0,0 +1,106 @@
+# -*- coding: utf-8 -*-
+"""
+microfeed - Pelican plugin
+==========================
+
+microfeeds allows for the creation of pages that each act as a feed of content separate
+to the main articles. The how and what:
+
+ - Write articles under a category that will be used to make a microfeed.
+ - Create the config setting MICROFEEDS as a list of these categories.
+ - Articles in these categories are extracted early in generation so they are excluded
+   from any article lists used in templates.
+ - A *microfeed* object is exposed to page templates via which articles can be accessed
+   as microfeed.*category* or microfeed[*category*] (these are equivalent).
+ - Each microfeed will also generate an ATOM/RSS feed as usual if you have set
+   CATEGORY_FEED_ATOM or CATEGORY_FEED_RSS.
+
+"""
+
+
+from pelican import signals
+
+
+class Microfeed:
+    categories = ()
+    articles = {}
+
+microfeed = Microfeed()
+
+
+def get_categories(pelican):
+    """
+    Collect the list of categories that the user wants to turn into microfeeds.
+    """
+    print('====> GETTING MICROFEED CATEGORIES')
+    categories = pelican.settings.get('MICROFEEDS', None)
+    if categories and type(categories) in (list, set, tuple):
+        microfeed.categories = categories
+        for cat in categories:
+            microfeed.articles[cat] = []
+
+def collect_microfeed_articles(article_generator):
+    """
+    Collect the microfeed articles from the article generator and move them to the
+    microfeed object for later use.
+    """
+    print('====> COLLECTING MICROFEED ARTICLES')
+    if microfeed.categories:
+
+        normal_articles = []
+        for article in article_generator.articles:
+            if article.category in microfeed.categories:
+                microfeed.articles[article.category].append(article)
+            else:
+                normal_articles.append(article)
+        article_generator.articles = normal_articles
+
+def add_microfeed_to_context(page_generator):
+    """
+    Expose the microfeed object to the page generator's context for use in page
+    templates'
+    """
+    print('====> ADDING MICROFEED TO PAGE CONTEXT')
+    for cat in microfeed.categories:
+        setattr(microfeed, cat, microfeed.articles[cat])
+    page_generator.context['microfeed'] = microfeed
+
+def gen_microfeed_feed(article_generator, writer):
+    """
+    Optionally generate an ATOM/RSS feed for each microfeed.
+    """
+    print('====> GENERATING MICROFEED ATOM/RSS FEED')
+    for cat_name in microfeed.categories:
+        cat = microfeed.articles[cat_name][0].category
+
+        if article_generator.settings.get('CATEGORY_FEED_ATOM'):
+            writer.write_feed(
+                microfeed.articles[cat.name],
+                article_generator.context,
+                article_generator.settings['CATEGORY_FEED_ATOM'].format(slug=cat.slug),
+                article_generator.settings.get(
+                    'CATEGORY_FEED_ATOM_URL',
+                    article_generator.settings['CATEGORY_FEED_ATOM']
+                ).format(slug=cat.slug),
+                feed_title=cat.name
+            )
+
+        if article_generator.settings.get('CATEGORY_FEED_RSS'):
+            writer.write_feed(
+                microfeed.articles[cat.name],
+                article_generator.context,
+                article_generator.settings['CATEGORY_FEED_RSS'].format(slug=cat.slug),
+                article_generator.settings.get(
+                    'CATEGORY_FEED_RSS_URL',
+                    article_generator.settings['CATEGORY_FEED_RSS']
+                ).format(slug=cat.slug),
+                feed_title=cat.name,
+                feed_type='rss'
+            )
+
+def register():
+    signals.initialized.connect(get_categories)
+    signals.article_generator_pretaxonomy.connect(collect_microfeed_articles)
+    #signals.page_generator_context.connect(add_microfeed_to_context)
+    signals.page_generator_finalized.connect(add_microfeed_to_context)
+    signals.article_writer_finalized.connect(gen_microfeed_feed)

__init__.py Added

@@ -0,0 +1,2 @@
+# -*- coding: utf-8 -*-
+from .microfeed import *

README.rst Added

@@ -0,0 +1,14 @@
+microfeed - Pelican plugin
+==========================
+
+microfeeds allows for the creation of pages that each act as a feed of content separate
+to the main articles. The how and what:
+
+ - Write articles under a category that will be used to make a microfeed.
+ - Create the config setting MICROFEEDS as a list of these categories.
+ - Articles in these categories are extracted early in generation so they are excluded
+   from any article lists used in templates.
+ - A *microfeed* object is exposed to page templates via which articles can be accessed
+   as microfeed.*category* or microfeed[*category*] (these are equivalent).
+ - Each microfeed will also generate an ATOM/RSS feed as usual if you have set
+   CATEGORY_FEED_ATOM or CATEGORY_FEED_RSS.