lib/nanoc/helpers/blogging.rb in nanoc-2.1.6 vs lib/nanoc/helpers/blogging.rb in nanoc-2.2

- old
+ new

@@ -19,26 +19,40 @@ # To activate this helper, +include+ it, like this: # # include Nanoc::Helpers::Blogging module Blogging - # Returns the list of articles, sorted by descending creation date (so - # newer articles appear first). + # Returns an unsorted list of articles. + def articles + @pages.select { |page| page.kind == 'article' } + end + + # Returns a list of articles, sorted by descending creation date (so newer + # articles appear first). def sorted_articles - @pages.select do |page| - page.kind == 'article' - end.sort do |x,y| - y.created_at <=> x.created_at - end + articles.sort_by { |a| a.created_at }.reverse end # Returns a string representing the atom feed containing recent articles, # sorted by descending creation date. +params+ is a hash where the # following keys can be set: # # +limit+:: The maximum number of articles to show. Defaults to 5. # + # +articles+:: A list of articles to include in the feed. Defaults to the + # list of articles returned by the articles function. + # + # +content_proc+:: A proc that returns the content of the given article, + # passed as a parameter. By default, given the argument + # +article+, this proc will return +article.content+. + # This function may not return nil. + # + # +excerpt_proc+:: A proc that returns the excerpt of the given article, + # passed as a parameter. By default, given the argument + # +article+, this proc will return +article.excerpt+. + # This function may return nil. + # # The following attributes must be set on blog articles: # # * 'title', containing the title of the blog post. # # * all other attributes mentioned above. @@ -91,14 +105,42 @@ # <%= atom_feed %> def atom_feed(params={}) require 'builder' # Extract parameters - limit = params[:limit] || 5 + limit = params[:limit] || 5 + relevant_articles = params[:articles] || articles || [] + content_proc = params[:content_proc] || lambda { |a| a.content } + excerpt_proc = params[:excerpt_proc] || lambda { |a| a.excerpt } + # Check feed page attributes + if @page.base_url.nil? + raise RuntimeError.new('Cannot build Atom feed: feed page has no base_url') + end + if @page.title.nil? + raise RuntimeError.new('Cannot build Atom feed: feed page has no title') + end + if @page.author_name.nil? + raise RuntimeError.new('Cannot build Atom feed: feed page has no author_name') + end + if @page.author_uri.nil? + raise RuntimeError.new('Cannot build Atom feed: feed page has no author_uri') + end + + # Check article attributes + if relevant_articles.empty? + raise RuntimeError.new('Cannot build Atom feed: no articles') + end + if relevant_articles.any? { |a| a.created_at.nil? } + raise RuntimeError.new('Cannot build Atom feed: one or more articles lack created_at') + end + + # Get sorted relevant articles + sorted_relevant_articles = relevant_articles.sort_by { |a| a.created_at }.reverse.first(limit) + # Get most recent article - last_article = sorted_articles.first + last_article = sorted_relevant_articles.first # Create builder buffer = '' xml = Builder::XmlMarkup.new(:target => buffer, :indent => 2) @@ -121,11 +163,11 @@ xml.name @page.author_name xml.uri @page.author_uri end # Add articles - sorted_articles.first(limit).each do |a| + sorted_relevant_articles.each do |a| xml.entry do # Add primary attributes xml.id atom_tag_for(a) xml.title a.title, :type => 'html' @@ -135,11 +177,12 @@ # Add link xml.link(:rel => 'alternate', :href => url_for(a)) # Add content - xml.content a.content, :type => 'html' - xml.summary a.excerpt, :type => 'html' unless a.excerpt.nil? + summary = excerpt_proc.call(a) + xml.content content_proc.call(a), :type => 'html' + xml.summary summary, :type => 'html' unless summary.nil? end end end buffer