Skip to content
This repository

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

Merged
merged 2 commits into from over 2 years ago

2 participants

Mikkel Høgh Frederic Hemberger
Mikkel Høgh

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 :)

Mikkel Høgh mikl commented on the diff October 04, 2011
.themes/classic/source/_includes/custom/category_feed.xml
((10 lines not shown))
  10
+  <link href="{{ site.url }}/"/>
  11
+  <updated>{{ site.time | date_to_xmlschema }}</updated>
  12
+  <id>{{ site.url }}/</id>
  13
+  <author>
  14
+    <name><![CDATA[{{ site.author | strip_html }}]]></name>
  15
+    {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
  16
+  </author>
  17
+  <generator uri="http://octopress.org/">Octopress</generator>
  18
+
  19
+  {% for post in site.categories[page.category] limit: 5 %}
  20
+  <entry>
  21
+    <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
  22
+    <link href="{{ site.url }}{{ post.url }}"/>
  23
+    <updated>{{ post.date | date_to_xmlschema }}</updated>
  24
+    <id>{{ site.url }}{{ post.id }}</id>
  25
+    <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | markdownify | cdata_escape }}]]></content>
1
Mikkel Høgh
mikl added a note October 04, 2011

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
Frederic Hemberger fhemberger merged commit d84daa2 into from October 16, 2011
Frederic Hemberger fhemberger closed this October 16, 2011
Frederic Hemberger

Cool feature, thanks!

Lars De Richter lars-derichter referenced this pull request from a commit May 03, 2012
Commit has since been removed from the repository and is no longer available.
Y0rTz yortz referenced this pull request from a commit July 06, 2012
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

Showing 2 unique commits by 1 author.

Oct 05, 2011
Mikkel Høgh First attempt at creating per-category Atom feeds. 66916f5
Oct 06, 2011
Mikkel Høgh Fixed duplicate title. e1d680f
This page is out of date. Refresh to see the latest.
27  .themes/classic/source/_includes/custom/category_feed.xml
... ...
@@ -0,0 +1,27 @@
  1
+---
  2
+layout: nil
  3
+---
  4
+<?xml version="1.0" encoding="utf-8"?>
  5
+<feed xmlns="http://www.w3.org/2005/Atom">
  6
+
  7
+  <title><![CDATA[{{ page.title }} | {{ site.title }}]]></title>
  8
+  <link href="{{ site.url }}/{{ page.feed_url }}" rel="self"/>
  9
+  <link href="{{ site.url }}/"/>
  10
+  <updated>{{ site.time | date_to_xmlschema }}</updated>
  11
+  <id>{{ site.url }}/</id>
  12
+  <author>
  13
+    <name><![CDATA[{{ site.author | strip_html }}]]></name>
  14
+    {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
  15
+  </author>
  16
+  <generator uri="http://octopress.org/">Octopress</generator>
  17
+
  18
+  {% for post in site.categories[page.category] limit: 5 %}
  19
+  <entry>
  20
+    <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
  21
+    <link href="{{ site.url }}{{ post.url }}"/>
  22
+    <updated>{{ post.date | date_to_xmlschema }}</updated>
  23
+    <id>{{ site.url }}{{ post.id }}</id>
  24
+    <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | markdownify | cdata_escape }}]]></content>
  25
+  </entry>
  26
+  {% endfor %}
  27
+</feed>
36  plugins/category_generator.rb
@@ -48,6 +48,35 @@ def initialize(site, base, category_dir, category)
48 48
 
49 49
   end
50 50
 
  51
+  # The CategoryFeed class creates an Atom feed for the specified category.
  52
+  class CategoryFeed < Page
  53
+
  54
+    # Initializes a new CategoryFeed.
  55
+    #
  56
+    #  +base+         is the String path to the <source>.
  57
+    #  +category_dir+ is the String path between <source> and the category folder.
  58
+    #  +category+     is the category currently being processed.
  59
+    def initialize(site, base, category_dir, category)
  60
+      @site = site
  61
+      @base = base
  62
+      @dir  = category_dir
  63
+      @name = 'atom.xml'
  64
+      self.process(@name)
  65
+      # Read the YAML data from the layout page.
  66
+      self.read_yaml(File.join(base, '_includes/custom'), 'category_feed.xml')
  67
+      self.data['category']    = category
  68
+      # Set the title for this page.
  69
+      title_prefix             = site.config['category_title_prefix'] || 'Category: '
  70
+      self.data['title']       = "#{title_prefix}#{category}"
  71
+      # Set the meta-description for this page.
  72
+      meta_description_prefix  = site.config['category_meta_description_prefix'] || 'Category: '
  73
+      self.data['description'] = "#{meta_description_prefix}#{category}"
  74
+
  75
+      # Set the correct feed URL.
  76
+      self.data['feed_url'] = "#{category_dir}/#{name}"
  77
+    end
  78
+
  79
+  end
51 80
 
52 81
   # The Site class is a built-in Jekyll class with access to global site config information.
53 82
   class Site
@@ -63,6 +92,13 @@ def write_category_index(category_dir, category)
63 92
       index.write(self.dest)
64 93
       # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
65 94
       self.pages << index
  95
+
  96
+      # Create an Atom-feed for each index.
  97
+      feed = CategoryFeed.new(self, self.source, category_dir, category)
  98
+      feed.render(self.layouts, site_payload)
  99
+      feed.write(self.dest)
  100
+      # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
  101
+      self.pages << feed
66 102
     end
67 103
 
68 104
     # Loops through the list of category pages and processes each one.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.