不停地疯

Work as a hacker, hack as a artist.

给Octopress的RSS输出中添加文章作者信息

| Comments

Octopress自带的RSS模版中没有包含文章作者信息,而是只包含了网站作者信息。考虑到博客经常只是一个人写作,同时文章作者随RSS显示,更常用一些,所以我对默认的RSS模版做了一点改动,让它支持了文章作者信息的输出。其实改动相当简单,默认情况下就一个 atom.xml 文件,该文件位于 source/ 目录下。改动如下:

atom.xml输出作者信息 (atom_author.diff) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
diff --git a/source/atom.xml b/source/atom.xml
index 83af3f8..c20506b 100644
--- a/source/atom.xml
+++ b/source/atom.xml
@@ -9,10 +9,6 @@ layout: nil
   <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.posts limit: 20 %}
@@ -20,8 +16,17 @@ layout: nil
     <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
     <link href="{{ site.url }}{{ post.url }}"/>
     <updated>{{ post.date | date_to_xmlschema }}</updated>
+    <author>
+      <name><![CDATA[{{ site.author | strip_html }}]]></name>
+      {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
+    </author>
     <id>{{ site.url }}{{ post.id }}</id>
     <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | cdata_escape }}]]></content>
   </entry>
   {% endfor %}
 </feed>
其实很简单,就是将全局的作者设定移到文章条目的设定中,这样设置的作者信息就成为了文章的属性。 对于曾经给Octopress添加过RSS2.0支持的站点1,还需要相应修改一下 source/ 目录下 rss.xml 文件:
rss.xml输出作者信息 (rss_author.diff) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
diff --git a/source/rss.xml b/source/rss.xml
index 435f17f..044e52f 100644
--- a/source/rss.xml
+++ b/source/rss.xml
@@ -3,27 +3,31 @@ layout: nil
 ---
 <?xml version="1.0" encoding="UTF-8"?>
 <rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <description><![CDATA[{{ site.title }}]]></description>
    <title><![CDATA[{{ site.title }}]]></title>
    <link>{{ site.url }}/</link>
    <pubDate>{{ site.time | date_to_xmlschema }}</pubDate>

    {% for post in site.posts limit: 20 %}
    <item>
      <description>
	<![CDATA[
		 {{ post.content | expand_urls: site.url | cdata_escape }}
	]]>
      </description>
      <title><![CDATA[{{ post.title | cdata_escape }}]]></title>
      <link>{{ site.url }}{{ post.url }}</link>
      <pubDate>{{ post.date | date_to_xmlschema }}</pubDate>
      <guid isPermaLink="false">{{ site.url }}{{ post.id }}</guid>
      <source url="{{ site.url }}/rss.xml"><![CDATA[{{ site.title }}]]></source>
+      <author>
+	<name><![CDATA[{{ site.author | strip_html }}]]></name>
+	{% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
+      </author>
    </item>
    {% endfor %}
  </channel>
 </rss>
同样,只需在文章描述段中加入作者信息的属性即可。修改好文件保存之后,再次运行 rake generate 就会发现rss文件已经正确更新了。

另外,有人还想在RSS输出的文章中加入相应版权声明,但又不想将版权声明嵌到文章里(比如像我这样)。办法也是有的,其实还是修改RSS模版文件。 首先在 source/_includes/post/ 目录中添加一个叫 copyright.html 新文件,内容如下,也可以是自己自定的一些版权内容:

版权声明文件 (copyright.diff) download
1
2
3
4
5
6
7
8
9
10
11
12
13
diff --git a/source/_includes/post/copyright.html b/source/_includes/post/copyright.html
index 2e57b7e..9547c7b 100644
--- a/source/_includes/post/copyright.html
+++ b/source/_includes/post/copyright.html
@@ -1,2 +1,8 @@
+<p class='post-footer'>
+  <h1>License</h1>
+  <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh"><img alt="知识共享许可协议" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png" /></a><br />本博作品采用<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.zh">知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议</a>进行许可。<br/>
+  Original link:
+  <a href='{{ site.url }}{{ post.url }}'>{{ site.url }}{{ post.url }}</a><br/>
+  &nbsp;written by <a href='{{ site.url }}'>{{ site.author }}</a>
+  &nbsp;posted at <a href='{{ site.url }}'>{{ site.url }}</a>
+</p>
我的版权声明模版中加入了CC许可,文章原始链接,作者名称以及个人站点信息。然后分别修改 atom.xmlrss.xml 文件如下:
atom.xml添加版权声明 (atom_copyright.diff) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
diff --git a/source/atom.xml b/source/atom.xml
index 83af3f8..c20506b 100644
--- a/source/atom.xml
+++ b/source/atom.xml
@@ -20,8 +16,17 @@ layout: nil
     <title type="html"><![CDATA[{{ post.title | cdata_escape }}]]></title>
     <link href="{{ site.url }}{{ post.url }}"/>
     <updated>{{ post.date | date_to_xmlschema }}</updated>
    <author>
      <name><![CDATA[{{ site.author | strip_html }}]]></name>
      {% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
    </author>
     <id>{{ site.url }}{{ post.id }}</id>
-    <content type="html"><![CDATA[{{ post.content | expand_urls: site.url | cdata_escape }}]]></content>
+    <content type="html">
+        <![CDATA[
+        {{ post.content | expand_urls: site.url | cdata_escape }}
+		{% include post/copyright.html %}
+        ]]>
+    </content>
   </entry>
   {% endfor %}
 </feed>
就添加一句 include post/copyright.html ,不过要注意是在CDATA段中。 rss.xml 一样处理:
rss.xml添加版权声明 (rss_copyright.diff) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
diff --git a/source/rss.xml b/source/rss.xml
index 435f17f..044e52f 100644
--- a/source/rss.xml
+++ b/source/rss.xml
@@ -3,27 +3,31 @@ layout: nil
 ---
 <?xml version="1.0" encoding="UTF-8"?>
 <rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <description><![CDATA[{{ site.title }}]]></description>
    <title><![CDATA[{{ site.title }}]]></title>
    <link>{{ site.url }}/</link>
    <pubDate>{{ site.time | date_to_xmlschema }}</pubDate>

    {% for post in site.posts limit: 20 %}
    <item>
      <description>
	<![CDATA[
		 {{ post.content | expand_urls: site.url | cdata_escape }}
+		 {% include post/copyright.html %}
	]]>
      </description>
      <title><![CDATA[{{ post.title | cdata_escape }}]]></title>
      <link>{{ site.url }}{{ post.url }}</link>
      <pubDate>{{ post.date | date_to_xmlschema }}</pubDate>
      <guid isPermaLink="false">{{ site.url }}{{ post.id }}</guid>

      <source url="{{ site.url }}/rss.xml"><![CDATA[{{ site.title }}]]></source>
      <author>
	<name><![CDATA[{{ site.author | strip_html }}]]></name>
	{% if site.email %}<email><![CDATA[{{ site.email }}]]></email>{% endif %}
      </author>
    </item>
    {% endfor %}
  </channel>
 </rss>
轻松搞定,效果就是我博客现在的效果。

Octopress

« 在Android编译时建立符号链接 推荐使用Vitamin-R »

Comments