Jekyll 博客 RSS 订阅
利用 Jekyll 官方提供的插件 rssgenerator.rb 即可实现博客的订阅输出。
使用方法
首先将 rssgenerator.rb 插件置入站点根目录中的 _plugins 目录中,然后在配置文件 _config.yml 添加相关配置内容即可。
name
:博客名称url
:博客地址description
:[可选] 博客描述,如果没有指定则根据name
生成描述author
:[可选] 博客作者,如果没有指定则为空copyright
:[可选] 博客版权,如果没有指定则为空rss_path
:[可选] RSS 文件输出目录,如果没有指定则设置为更目录/
rss_name
:[可选] RSS 文件输出名称,如果没有指定则定义为 “rss.xml”rss_post_limit
:[可选] RSS 文件输出文章的数目
这样,在每次重新生成博客内容的时候,就会在博客生成目录中重新生成一份 rss.xml 文件。
插件源代码
1 # Jekyll plugin for generating an rss 2.0 feed for posts
2 #
3 # Usage: place this file in the _plugins directory and set the required configuration
4 # attributes in the _config.yml file
5 #
6 # Uses the following attributes in _config.yml:
7 # name - the name of the site
8 # url - the url of the site
9 # description - (optional) a description for the feed (if not specified will be generated from name)
10 # author - (optional) the author of the site (if not specified will be left blank)
11 # copyright - (optional) the copyright of the feed (if not specified will be left blank)
12 # rss_path - (optional) the path to the feed (if not specified "/" will be used)
13 # rss_name - (optional) the name of the rss file (if not specified "rss.xml" will be used)
14 # rss_post_limit - (optional) the number of posts in the feed
15 #
16 # Author: Assaf Gelber <assaf.gelber@gmail.com>
17 # Site: http://agelber.com
18 # Source: http://github.com/agelber/jekyll-rss
19 #
20 # Distributed under the MIT license
21 # Copyright Assaf Gelber 2013
22
23 module Jekyll
24 class RssFeed < Page; end
25
26 class RssGenerator < Generator
27 priority :low
28 safe true
29
30 # Generates an rss 2.0 feed
31 #
32 # site - the site
33 #
34 # Returns nothing
35 def generate(site)
36 require 'rss'
37
38 # Create the rss with the help of the RSS module
39 rss = RSS::Maker.make("2.0") do |maker|
40 maker.channel.title = site.config['name']
41 maker.channel.link = site.config['url']
42 maker.channel.description = site.config['description'] || "RSS feed for #{site.config['name']}"
43 maker.channel.author = site.config["author.name"]
44 maker.channel.updated = site.posts.map { |p| p.date }.max
45 maker.channel.copyright = site.config['copyright']
46
47 post_limit = (site.config['rss_post_limit'] - 1 rescue site.posts.count)
48
49 site.posts.reverse[0..post_limit].each do |post|
50 post.render(site.layouts, site.site_payload)
51 maker.items.new_item do |item|
52 link = "#{site.config['url']}#{post.url}"
53 item.guid.content = link
54 item.title = post.title
55 item.link = link
56 item.description = post.excerpt
57 item.updated = post.date
58 end
59 end
60 end
61
62 # File creation and writing
63 rss_path = ensure_slashes(site.config['rss_path'] || "/")
64 rss_name = site.config['rss_name'] || "rss.xml"
65 full_path = File.join(site.dest, rss_path)
66 ensure_dir(full_path)
67 File.open("#{full_path}#{rss_name}", "w") { |f| f.write(rss) }
68
69 # Add the feed page to the site pages
70 site.pages << Jekyll::RssFeed.new(site, site.dest, rss_path, rss_name)
71 end
72
73 private
74
75 # Ensures the given path has leading and trailing slashes
76 #
77 # path - the string path
78 #
79 # Return the path with leading and trailing slashes
80 def ensure_slashes(path)
81 ensure_leading_slash(ensure_trailing_slash(path))
82 end
83
84 # Ensures the given path has a leading slash
85 #
86 # path - the string path
87 #
88 # Returns the path with a leading slash
89 def ensure_leading_slash(path)
90 path[0] == "/" ? path : "/#{path}"
91 end
92
93 # Ensures the given path has a trailing slash
94 #
95 # path - the string path
96 #
97 # Returns the path with a trailing slash
98 def ensure_trailing_slash(path)
99 path[-1] == "/" ? path : "#{path}/"
100 end
101
102 # Ensures the given directory exists
103 #
104 # path - the string path of the directory
105 #
106 # Returns nothing
107 def ensure_dir(path)
108 FileUtils.mkdir_p(path)
109 end
110
111 end
112 end
相关修改
因为插件的一些配置属性与自己在 _config.yml 文件中定义的一些属性变量不统一,因此根据自身特点进行一些细微的修改。
比如博客名称的属性变量为 title
,博客作者的属性变量为 author.name
和 author.email
,因此,将插件源文件中使用 name
变量的都修改为 title
,使用 author
变量的都修改为 author.name
等。
注意事项
利用此插件生成的 RSS 订阅输出的内容只是文章的摘要部分,要想生成全文输出,则需要将源代码中的
item.description = post.excerpt
修改为
item.description = post.content
参考资料
上一篇: Jekyll 博客分页显示
下一篇:理发师的故事