Antti Kaihola avatar Antti Kaihola committed c642885 Draft

Render RSS feeds for each category, even if it has no posts

Comments (0)

Files changed (3)

 '''
 
 
-requires = ["Jinja2>=2.3", "Sphinx>=1.1", "argparse>=1.2"]
+requires = ["Jinja2>=2.3",
+            "Sphinx>=1.1",
+            "argparse>=1.2",
+            "python-dateutil>=1.5"]
 test_requires = ['nose', 'tox']
 
 setup(

tinkerer/ext/blog.py

     for name, context, template in rss.generate_feed(app):
         yield (name, context, template)
 
+    for category in app.builder.env.filing["categories"]:
+        for name, context, template in rss.generate_feed(app, category=category):
+            yield (name, context, template)
+
     for name, context, template in filing.make_tag_pages(app):
         yield (name, context, template)
 

tinkerer/ext/rss.py

     :license: FreeBSD, see LICENSE file
 '''
 import cgi
+from datetime import datetime
+import dateutil.tz
 import email.utils
 import time
 from tinkerer.ext import patch
+from tinkerer import utils
 
 
 
 
 
 
-def generate_feed(app):
+def generate_feed(app, category=None):
     '''
     Generates RSS feed.
     '''
     env = app.builder.env
  
-    # don't do anything if no posts are available
-    if not env.blog_posts:
-        return
-
     context = dict()
 
     # feed items
                 time.mktime(env.blog_metadata[post].date.timetuple()),
                 localtime=True)
 
-        categories = [category[1] for category in env.blog_metadata[post].filing["categories"]]
+        categories = [cat[1] for cat in env.blog_metadata[post].filing["categories"]]
 
-        context["items"].append({
+        if not category or category in categories:
+            context["items"].append({
                     "title": env.titles[post].astext(),
                     "link": link,
                     "description": patch.strip_xml_declaration(patch.patch_links(
 
     # feed metadata 
     context["title"] = app.config.project
-    context["link"] = app.config.website
     context["description"] = app.config.tagline
     context["language"] = "en-us"
-  
-    # feed pubDate is equal to latest post pubDate
-    context["pubDate"] = context["items"][0]["pubDate"]
 
-    yield ("rss", context, "rss.html")
+    if context["items"]:
+        # feed pubDate is equal to latest post pubDate
+        context["pubDate"] = context["items"][0]["pubDate"]
+    else:
+        today = datetime.now(dateutil.tz.tzlocal())
+        context["pubDate"] = today.strftime("%a, %d %b %Y %H:%M:%S %z")
 
+    if category:
+        category_name = utils.name_from_title(category)
+        name = "%s.rss" % category_name
+        context["title"] += u': %s' % category
+        context["link"] = "%scategories/%s.html" % (app.config.website,
+                                                     category_name)
+    else:
+        name = "rss"
+        context["link"] = app.config.website
+
+    yield (name, context, "rss.html")
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.