Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

First attempt at creating per-category Atom feeds. #201

Merged
merged 2 commits into from

2 participants

@mikl

One of the things I've missed from Octopress is per-category RSS/Atom feeds. Many open source projects have "planet" sites where they aggregate community blogs, but they want a feed of only the relevant posts – Planet Drupal only wants posts about Drupal, for example.

This is my first Ruby code ever, so I more or less expect that I've done something horrible here, but it does work, and I'm currently in the process of deploying it to my own blog.

So I'd really like to hear how this could be improved :)

@mikl mikl commented on the diff
...mes/classic/source/_includes/custom/category_feed.xml
((10 lines not shown))
+ <link href="{{ site.url }}/"/>
+ <updated>{{ site.time | date_to_xmlschema }}</updated>
+ <id>{{ site.url }}/</id>
+ <author>
+ <name><![CDATA[{{ site.author | strip_html }}]]></name>
+ {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
+ </author>
+ <generator uri="http://octopress.org/">Octopress</generator>
+
+ {% for post in site.categories[page.category] limit: 5 %}
+ <entry>
+ <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
+ <link href="{{ site.url }}{{ post.url }}"/>
+ <updated>{{ post.date | date_to_xmlschema }}</updated>
+ <id>{{ site.url }}{{ post.id }}</id>
+ <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | markdownify | cdata_escape }}]]></content>
@mikl
mikl added a note

I had to add markdownify here to get my content rendered properly. This confounds me, since the other templates that I've looked at didn't have to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@fhemberger fhemberger merged commit d84daa2 into imathis:master
@fhemberger

Cool feature, thanks!

@lars-derichter lars-derichter referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@yortz yortz referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 4, 2011
  1. @mikl
Commits on Oct 6, 2011
  1. @mikl

    Fixed duplicate title.

    mikl authored
This page is out of date. Refresh to see the latest.
View
27 .themes/classic/source/_includes/custom/category_feed.xml
@@ -0,0 +1,27 @@
+---
+layout: nil
+---
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+
+ <title><![CDATA[{{ page.title }} | {{ site.title }}]]></title>
+ <link href="{{ site.url }}/{{ page.feed_url }}" rel="self"/>
+ <link href="{{ site.url }}/"/>
+ <updated>{{ site.time | date_to_xmlschema }}</updated>
+ <id>{{ site.url }}/</id>
+ <author>
+ <name><![CDATA[{{ site.author | strip_html }}]]></name>
+ {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
+ </author>
+ <generator uri="http://octopress.org/">Octopress</generator>
+
+ {% for post in site.categories[page.category] limit: 5 %}
+ <entry>
+ <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
+ <link href="{{ site.url }}{{ post.url }}"/>
+ <updated>{{ post.date | date_to_xmlschema }}</updated>
+ <id>{{ site.url }}{{ post.id }}</id>
+ <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | markdownify | cdata_escape }}]]></content>
@mikl
mikl added a note

I had to add markdownify here to get my content rendered properly. This confounds me, since the other templates that I've looked at didn't have to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ </entry>
+ {% endfor %}
+</feed>
View
36 plugins/category_generator.rb
@@ -48,6 +48,35 @@ def initialize(site, base, category_dir, category)
end
+ # The CategoryFeed class creates an Atom feed for the specified category.
+ class CategoryFeed < Page
+
+ # Initializes a new CategoryFeed.
+ #
+ # +base+ is the String path to the <source>.
+ # +category_dir+ is the String path between <source> and the category folder.
+ # +category+ is the category currently being processed.
+ def initialize(site, base, category_dir, category)
+ @site = site
+ @base = base
+ @dir = category_dir
+ @name = 'atom.xml'
+ self.process(@name)
+ # Read the YAML data from the layout page.
+ self.read_yaml(File.join(base, '_includes/custom'), 'category_feed.xml')
+ self.data['category'] = category
+ # Set the title for this page.
+ title_prefix = site.config['category_title_prefix'] || 'Category: '
+ self.data['title'] = "#{title_prefix}#{category}"
+ # Set the meta-description for this page.
+ meta_description_prefix = site.config['category_meta_description_prefix'] || 'Category: '
+ self.data['description'] = "#{meta_description_prefix}#{category}"
+
+ # Set the correct feed URL.
+ self.data['feed_url'] = "#{category_dir}/#{name}"
+ end
+
+ end
# The Site class is a built-in Jekyll class with access to global site config information.
class Site
@@ -63,6 +92,13 @@ def write_category_index(category_dir, category)
index.write(self.dest)
# Record the fact that this page has been added, otherwise Site::cleanup will remove it.
self.pages << index
+
+ # Create an Atom-feed for each index.
+ feed = CategoryFeed.new(self, self.source, category_dir, category)
+ feed.render(self.layouts, site_payload)
+ feed.write(self.dest)
+ # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
+ self.pages << feed
end
# Loops through the list of category pages and processes each one.
Something went wrong with that request. Please try again.