lib/nanoc3/helpers/blogging.rb in nanoc3-3.1.4 vs lib/nanoc3/helpers/blogging.rb in nanoc3-3.1.5
- old
+ new
@@ -37,12 +37,11 @@
#
# @return [Array] A sorted array containing all articles
def sorted_articles
require 'time'
articles.sort_by do |a|
- time = a[:created_at]
- time.is_a?(String) ? Time.parse(time) : time
+ attribute_to_time(a[:created_at])
end.reverse
end
# Returns a string representing the atom feed containing recent articles,
# sorted by descending creation date.
@@ -181,12 +180,11 @@
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|
- time = a[:created_at]
- time.is_a?(String) ? Time.parse(time) : time
+ attribute_to_time(a[:created_at])
end.reverse.first(limit)
# Get most recent article
last_article = sorted_relevant_articles.first
@@ -202,12 +200,11 @@
# Add primary attributes
xml.id root_url
xml.title title
# Add date
- time = last_article[:created_at]
- xml.updated((time.is_a?(String) ? Time.parse(time) : time).to_iso8601_time)
+ xml.updated(attribute_to_time(last_article[:created_at]).to_iso8601_time)
# Add links
xml.link(:rel => 'alternate', :href => root_url)
xml.link(:rel => 'self', :href => feed_url)
@@ -227,14 +224,12 @@
# Add primary attributes
xml.id atom_tag_for(a)
xml.title a[:title], :type => 'html'
# Add dates
- 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)
+ 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
# Add link
xml.link(:rel => 'alternate', :href => url)
# Add content
@@ -295,13 +290,25 @@
def atom_tag_for(item)
require 'time'
hostname, base_dir = %r{^.+?://([^/]+)(.*)$}.match(@site.config[:base_url])[1..2]
- time = item[:created_at]
- formatted_date = (time.is_a?(String) ? Time.parse(time) : time).to_iso8601_date
+ formatted_date = attribute_to_time(item[:created_at]).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