lib/nanoc3/helpers/blogging.rb in nanoc3-3.1.9 vs lib/nanoc3/helpers/blogging.rb in nanoc3-3.2.0a1
- old
+ new
@@ -37,11 +37,12 @@
#
# @return [Array] A sorted array containing all articles
def sorted_articles
require 'time'
articles.sort_by do |a|
- attribute_to_time(a[:created_at])
+ time = a[:created_at]
+ time.is_a?(String) ? Time.parse(time) : time
end.reverse
end
# Returns a string representing the atom feed containing recent articles,
# sorted by descending creation date.
@@ -117,32 +118,32 @@
# @example Limiting the number of items in a feed
#
# <%= atom_feed :limit => 5 %>
#
# @option params [Number] :limit (5) The maximum number of articles to
- # show
+ # show
#
# @option params [Array] :articles (sorted_articles) A list of articles to
- # include in the feed
+ # include in the feed
#
# @option params [Proc] :content_proc (->{ |article|
- # article.compiled_content(:snapshot => :pre) }) A proc that returns the
- # content of the given article, which is passed as a parameter. This
- # function may not return nil.
+ # article.compiled_content(:snapshot => :pre) }) A proc that returns the
+ # content of the given article, which is passed as a parameter. This
+ # function may not return nil.
#
# @option params [proc] :excerpt_proc (->{ |article| article[:excerpt] })
- # A proc that returns the excerpt of the given article, passed as a
- # parameter. This function should return nil if there is no excerpt.
+ # A proc that returns the excerpt of the given article, passed as a
+ # parameter. This function should return nil if there is no excerpt.
#
# @option params [String] :title The feed’s title, if it is not given in
- # the item attributes.
+ # the item attributes.
#
# @option params [String] :author_name The name of the feed’s author, if
- # it is not given in the item attributes.
+ # it is not given in the item attributes.
#
# @option params [String] :author_uri The URI of the feed’s author, if it
- # is not given in the item attributes.
+ # is not given in the item attributes.
#
# @return [String] The generated feed content
def atom_feed(params={})
require 'builder'
require 'time'
@@ -180,11 +181,12 @@
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 do |a|
- attribute_to_time(a[:created_at])
+ time = a[:created_at]
+ time.is_a?(String) ? Time.parse(time) : time
end.reverse.first(limit)
# Get most recent article
last_article = sorted_relevant_articles.first
@@ -200,11 +202,12 @@
# Add primary attributes
xml.id root_url
xml.title title
# Add date
- xml.updated(attribute_to_time(last_article[:created_at]).to_iso8601_time)
+ time = last_article[:created_at]
+ xml.updated((time.is_a?(String) ? Time.parse(time) : time).to_iso8601_time)
# Add links
xml.link(:rel => 'alternate', :href => root_url)
xml.link(:rel => 'self', :href => feed_url)
@@ -224,12 +227,14 @@
# Add primary attributes
xml.id atom_tag_for(a)
xml.title a[:title], :type => 'html'
# Add dates
- xml.published attribute_to_time(a[:created_at]).to_iso8601_time
- xml.updated attribute_to_time(a[:updated_at] || a[:created_at]).to_iso8601_time
+ create_time = a[:created_at]
+ update_time = a[:updated_at] || a[:created_at]
+ xml.published((create_time.is_a?(String) ? Time.parse(create_time) : create_time).to_iso8601_time)
+ xml.updated( (update_time.is_a?(String) ? Time.parse(update_time) : update_time).to_iso8601_time)
# Add link
xml.link(:rel => 'alternate', :href => url)
# Add content
@@ -290,25 +295,13 @@
def atom_tag_for(item)
require 'time'
hostname, base_dir = %r{^.+?://([^/]+)(.*)$}.match(@site.config[:base_url])[1..2]
- formatted_date = attribute_to_time(item[:created_at]).to_iso8601_date
+ time = item[:created_at]
+ formatted_date = (time.is_a?(String) ? Time.parse(time) : time).to_iso8601_date
'tag:' + hostname + ',' + formatted_date + ':' + base_dir + (item.path || item.identifier)
- end
-
- # Converts the given attribute (which can be a string, a Time or a Date)
- # into a Time.
- #
- # @param [String, Time, Date] time Something that contains time
- # information but is not necessarily a Time instance yet
- #
- # @return [Time] The Time instance corresponding to the given input
- def attribute_to_time(time)
- time = Time.local(time.year, time.month, time.day) if time.is_a?(Date)
- time = Time.parse(time) if time.is_a?(String)
- time
end
end
end