lib/nanoc3/helpers/blogging.rb in nanoc3-3.0.7 vs lib/nanoc3/helpers/blogging.rb in nanoc3-3.0.8
- old
+ new
@@ -30,11 +30,14 @@
# Returns a list of articles, sorted by descending creation date (so newer
# articles appear first).
def sorted_articles
require 'time'
- articles.sort_by { |a| Time.parse(a[:created_at]) }.reverse
+ articles.sort_by do |a|
+ 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. +params+ is a hash where the
# following keys can be set:
@@ -140,11 +143,14 @@
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| Time.parse(a[:created_at]) }.reverse.first(limit)
+ sorted_relevant_articles = relevant_articles.sort_by do |a|
+ 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
# Create builder
@@ -152,19 +158,22 @@
xml = Builder::XmlMarkup.new(:target => buffer, :indent => 2)
# Build feed
xml.instruct!
xml.feed(:xmlns => 'http://www.w3.org/2005/Atom') do
+ root_url = @site.config[:base_url] + '/'
+
# Add primary attributes
- xml.id @site.config[:base_url] + '/'
+ xml.id root_url
xml.title @item[:title]
# Add date
- xml.updated Time.parse(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 => @site.config[:base_url])
+ xml.link(:rel => 'alternate', :href => root_url)
xml.link(:rel => 'self', :href => feed_url)
# Add author information
xml.author do
xml.name @item[:author_name]
@@ -181,11 +190,12 @@
# Add primary attributes
xml.id atom_tag_for(a)
xml.title a[:title], :type => 'html'
# Add dates
- xml.published Time.parse(a[:created_at]).to_iso8601_time
+ time = a[:created_at]
+ xml.published (time.is_a?(String) ? Time.parse(time) : time).to_iso8601_time
xml.updated a.mtime.to_iso8601_time
# Add link
xml.link(:rel => 'alternate', :href => url)
@@ -232,13 +242,15 @@
# created using a procedure suggested by Mark Pilgrim in this blog post:
# http://diveintomark.org/archives/2004/05/28/howto-atom-id.
def atom_tag_for(item)
require 'time'
- hostname = @site.config[:base_url].sub(/.*:\/\/(.+?)\/?$/, '\1')
- formatted_date = Time.parse(item[:created_at]).to_iso8601_date
+ hostname, base_dir = %r{^.+?://([^/]+)(.*)$}.match(@site.config[:base_url])[1..2]
- 'tag:' + hostname + ',' + formatted_date + ':' + (item.reps[0].path || item.identifier)
+ time = item[:created_at]
+ formatted_date = (time.is_a?(String) ? Time.parse(time) : time).to_iso8601_date
+
+ 'tag:' + hostname + ',' + formatted_date + ':' + base_dir + (item.reps[0].path || item.identifier)
end
end
end