# Jekyll plugin for generating an rss 2.0 feed for posts # # Based on https://github.com/kikoval/jekyll-rss/ but heavily modified # # Usage: place this file in the _plugins directory and set the required configuration # attributes in the _config.yml file # # Uses the following attributes in _config.yml: # title - the title of the site # url - the url of the site # description - (optional) a description for the feed (if not specified will be generated from title) # author - (optional) the author of the site (if not specified will be left blank) # copyright - (optional) the copyright of the feed (if not specified will be left blank) # rss_path - (optional) the path to the feed (if not specified "/" will be used) # rss_name - (optional) the name of the rss file (if not specified "rss.xml" will be used) # rss_post_limit - (optional) the number of posts in the feed # # Author: Assaf Gelber # Site: http://agelber.com # Source: http://github.com/agelber/jekyll-rss # # Distributed under the MIT license # Copyright Assaf Gelber 2013 require "jekyll" require "rss" module RssFeed class Page < Jekyll::Page; end class Generator < Jekyll::Generator priority :low safe true attr_reader :site, :rss_content # Generates an rss 2.0 feed # # site - the site # # Returns nothing def generate(site) @site = site feed_items = [] # collect Data @rss_content = RSS::Maker.make('2.0') do |maker| maker.channel.title = @site.config['title'] maker.channel.link = @site.config['url'] maker.channel.description = @site.config['description'] || "RSS feed for #{site.config['title']}" maker.channel.author = @site.config['author'] maker.channel.updated = @site.posts.docs.map { |p| p.date }.max maker.channel.copyright = @site.config['copyright'] @site.collections.each do |name, meta| Jekyll.logger.info "muhh Feed:", "Adding #{name} to global feed" feed_items site.posts.docs.reverse[0..post_limit].each do |post| maker.items.new_item do |item| item.title = post.data["title"] item.link = "#{@site.config['url']}#{post.url}" item.description = post.data["excerpt"] item.updated = post.date end end end end create_file # Add the feed page to the site pages site.pages << RssFeed::Page.new(site, site.dest, rss_path, rss_name) end private def rss_path ensure_slashes(@site.config['rss_path'] || "/") end def rss_name @site.config['rss_name'] || "rss.xml" end def create_file full_path = File.join(@site.dest, rss_path) ensure_dir(full_path) File.open("#{full_path}#{rss_name}", "w") { |f| f.write(@rss_content) } end # Ensures the given path has leading and trailing slashes # # path - the string path # # Return the path with leading and trailing slashes def ensure_slashes(path) ensure_leading_slash(ensure_trailing_slash(path)) end # Ensures the given path has a leading slash # # path - the string path # # Returns the path with a leading slash def ensure_leading_slash(path) path[0] == "/" ? path : "/#{path}" end # Ensures the given path has a trailing slash # # path - the string path # # Returns the path with a trailing slash def ensure_trailing_slash(path) path[-1] == "/" ? path : "#{path}/" end # Ensures the given directory exists # # path - the string path of the directory # # Returns nothing def ensure_dir(path) FileUtils.mkdir_p(path) end def post_limit @site.config['rss_post_limit'] - 1 rescue @site.posts.count end end end