Browse code

Merge pull request #201 from mikl/category-feeds

Adds per-category Atom feeds.

Frederic Hemberger authored on 16/10/2011 at 11:58:14
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,27 @@
0
+---
1
+layout: nil
2
+---
3
+<?xml version="1.0" encoding="utf-8"?>
4
+<feed xmlns="http://www.w3.org/2005/Atom">
5
+
6
+  <title><![CDATA[{{ page.title }} | {{ site.title }}]]></title>
7
+  <link href="{{ site.url }}/{{ page.feed_url }}" rel="self"/>
8
+  <link href="{{ site.url }}/"/>
9
+  <updated>{{ site.time | date_to_xmlschema }}</updated>
10
+  <id>{{ site.url }}/</id>
11
+  <author>
12
+    <name><![CDATA[{{ site.author | strip_html }}]]></name>
13
+    {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
14
+  </author>
15
+  <generator uri="http://octopress.org/">Octopress</generator>
16
+
17
+  {% for post in site.categories[page.category] limit: 5 %}
18
+  <entry>
19
+    <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
20
+    <link href="{{ site.url }}{{ post.url }}"/>
21
+    <updated>{{ post.date | date_to_xmlschema }}</updated>
22
+    <id>{{ site.url }}{{ post.id }}</id>
23
+    <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | markdownify | cdata_escape }}]]></content>
24
+  </entry>
25
+  {% endfor %}
26
+</feed>
... ...
@@ -48,6 +48,35 @@ module Jekyll
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 @@ module Jekyll
63 63
       index.write(self.dest)
64 64
       # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
65 65
       self.pages << index
66
+
67
+      # Create an Atom-feed for each index.
68
+      feed = CategoryFeed.new(self, self.source, category_dir, category)
69
+      feed.render(self.layouts, site_payload)
70
+      feed.write(self.dest)
71
+      # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
72
+      self.pages << feed
66 73
     end
67 74
 
68 75
     # Loops through the list of category pages and processes each one.