Skip to content

Commit 9ca0fb4

Browse files
committed
Added feed.xml generator for authors and tags so that these also get atom feed.xml.
1 parent 41f8625 commit 9ca0fb4

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

_layouts/feed.xml

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
layout: null
3+
---
4+
<?xml version="1.0" encoding="utf-8"?>
5+
{% if page.xsl %}
6+
<?xml-stylesheet type="text/xml" href="{{ '/feed.xslt.xml' | absolute_url }}"?>
7+
{% endif %}
8+
<feed xmlns="http://www.w3.org/2005/Atom" {% if site.lang %}xml:lang="{{ site.lang }}"{% endif %}>
9+
<generator uri="https://jekyllrb.com/" version="{{ jekyll.version }}">Jekyll</generator>
10+
<link href="{{ page.url | absolute_url }}" rel="self" type="application/atom+xml" />
11+
<link href="{{ '/' | absolute_url }}" rel="alternate" type="text/html" {% if site.lang %}hreflang="{{ site.lang }}" {% endif %}/>
12+
<updated>{{ site.time | date_to_xmlschema }}</updated>
13+
<id>{{ page.url | absolute_url | xml_escape }}</id>
14+
15+
{% assign title = site.title | default: site.name %}
16+
{% if page.collection != "posts" %}
17+
{% assign collection = page.collection | capitalize %}
18+
{% assign title = title | append: " | " | append: collection %}
19+
{% endif %}
20+
{% if page.category %}
21+
{% assign category = page.category | capitalize %}
22+
{% assign title = title | append: " | " | append: category %}
23+
{% endif %}
24+
25+
{% if title %}
26+
<title type="html">{{ title | smartify | xml_escape }}</title>
27+
{% endif %}
28+
29+
{% if site.description %}
30+
<subtitle>{{ site.description | xml_escape }}</subtitle>
31+
{% endif %}
32+
33+
{% if site.author %}
34+
<author>
35+
<name>{{ site.author.name | default: site.author | xml_escape }}</name>
36+
{% if site.author.email %}
37+
<email>{{ site.author.email | xml_escape }}</email>
38+
{% endif %}
39+
{% if site.author.uri %}
40+
<uri>{{ site.author.uri | xml_escape }}</uri>
41+
{% endif %}
42+
</author>
43+
{% endif %}
44+
45+
{% if site.data.authors %}
46+
{% for author in site.data.authors %}
47+
{% if author[1].username == post.author %}
48+
<author>{{ author[1].name | strip_html }}</author>
49+
<author>
50+
<name>{{ author[1].name | default: author[1].name | xml_escape }}</name>
51+
{% if author[1].email %}
52+
<email>{{ author[1].email | xml_escape }}</email>
53+
{% endif %}
54+
{% if author[1].uri %}
55+
<uri>{{ author[1].uri | xml_escape }}</uri>
56+
{% endif %}
57+
</author>
58+
{% endif %}
59+
{% endfor %}
60+
{% endif %}
61+
62+
{% assign posts = page.posts %}
63+
{% if page.category %}
64+
{% assign posts = posts | where: "category",page.category %}
65+
{% endif %}
66+
67+
{% for post in posts limit: 10 %}
68+
<entry{% if post.lang %}{{" "}}xml:lang="{{ post.lang }}"{% endif %}>
69+
<title type="html">{{ post.title | smartify | strip_html | normalize_whitespace | xml_escape }}</title>
70+
<link href="{{ post.url | absolute_url }}" rel="alternate" type="text/html" title="{{ post.title | xml_escape }}" />
71+
<published>{{ post.date | date_to_xmlschema }}</published>
72+
<updated>{{ post.last_modified_at | default: post.date | date_to_xmlschema }}</updated>
73+
<id>{{ post.id | absolute_url | xml_escape }}</id>
74+
<content type="html" xml:base="{{ post.url | absolute_url | xml_escape }}">{{ post.content | strip | xml_escape }}</content>
75+
76+
{% assign post_author = post.author | default: post.authors[0] | default: site.author %}
77+
{% assign post_author = site.data.authors[post_author] | default: post_author %}
78+
{% assign post_author_email = post_author.email | default: nil %}
79+
{% assign post_author_uri = post_author.uri | default: nil %}
80+
{% assign post_author_name = post_author.name | default: post_author %}
81+
82+
<author>
83+
<name>{{ post_author_name | default: "" | xml_escape }}</name>
84+
{% if post_author_email %}
85+
<email>{{ post_author_email | xml_escape }}</email>
86+
{% endif %}
87+
{% if post_author_uri %}
88+
<uri>{{ post_author_uri | xml_escape }}</uri>
89+
{% endif %}
90+
</author>
91+
92+
{% if post.category %}
93+
<category term="{{ post.category | xml_escape }}" />
94+
{% endif %}
95+
96+
{% for tag in post.tags %}
97+
<category term="{{ tag | xml_escape }}" />
98+
{% endfor %}
99+
100+
{% if post.excerpt and post.excerpt != empty %}
101+
<summary type="html">{{ post.excerpt | strip_html | normalize_whitespace | xml_escape }}</summary>
102+
{% endif %}
103+
104+
{% assign post_image = post.image.path | default: post.image %}
105+
{% if post_image %}
106+
{% unless post_image contains "://" %}
107+
{% assign post_image = post_image | absolute_url %}
108+
{% endunless %}
109+
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="{{ post_image | xml_escape }}" />
110+
{% endif %}
111+
</entry>
112+
{% endfor %}
113+
</feed>

_plugins/jekyll-autgenerator.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ def generate(site)
1313

1414
def build_subpages(site, type, posts)
1515
posts[1] = posts[1].sort_by { |p| -p.date.to_f }
16+
atomize(site, type, posts)
1617
paginate(site, type, posts)
1718
end
1819

20+
def atomize(site, type, posts)
21+
path = "/author/#{posts[0]}"
22+
atom = AtomPageAuthor.new(site, site.source, path, type, posts[0], posts[1])
23+
site.pages << atom
24+
end
25+
1926
def paginate(site, type, posts)
2027
pages = Jekyll::Paginate::Pager.calculate_pages(posts[1], site.config['paginate'].to_i)
2128
(1..pages).each do |num_page|
@@ -51,4 +58,19 @@ def initialize(site, base, dir, type, val)
5158
self.data[type] = val
5259
end
5360
end
61+
62+
class AtomPageAuthor < Page
63+
def initialize(site, base, dir, type, val, posts)
64+
@site = site
65+
@base = base
66+
@dir = dir
67+
@name = 'feed.xml'
68+
69+
self.process(@name)
70+
self.read_yaml(File.join(base, '_layouts'), "feed.xml")
71+
self.data[type] = val
72+
self.data["grouptype"] = type
73+
self.data["posts"] = posts[0..9]
74+
end
75+
end
5476
end

_plugins/jekyll-tagsgenerator.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@ def generate(site)
1212

1313
def build_subpages(site, type, posts)
1414
posts[1] = posts[1].sort_by { |p| -p.date.to_f }
15+
atomize(site, type, posts)
1516
paginate(site, type, posts)
1617
end
1718

19+
def atomize(site, type, posts)
20+
path = "/tag/#{posts[0]}".gsub(' ', '-').downcase
21+
atom = AtomPageTags.new(site, site.source, path, type, posts[0], posts[1])
22+
site.pages << atom
23+
end
24+
1825
def paginate(site, type, posts)
1926
pages = Jekyll::Paginate::Pager.calculate_pages(posts[1], site.config['paginate'].to_i)
2027
(1..pages).each do |num_page|
@@ -44,4 +51,19 @@ def initialize(site, base, dir, type, val)
4451
self.data[type] = val
4552
end
4653
end
54+
55+
class AtomPageTags < Page
56+
def initialize(site, base, dir, type, val, posts)
57+
@site = site
58+
@base = base
59+
@dir = dir
60+
@name = 'feed.xml'
61+
62+
self.process(@name)
63+
self.read_yaml(File.join(base, '_layouts'), "feed.xml")
64+
self.data[type] = val
65+
self.data["grouptype"] = type
66+
self.data["posts"] = posts[0..9]
67+
end
68+
end
4769
end

0 commit comments

Comments
 (0)