lib/nanoc/helpers/blogging.rb in nanoc-3.8.0 vs lib/nanoc/helpers/blogging.rb in nanoc-4.0.0a1
- old
+ new
@@ -50,11 +50,11 @@
end
class AtomFeedBuilder
include Nanoc::Helpers::Blogging
- attr_accessor :site
+ attr_accessor :config
attr_accessor :limit
attr_accessor :relevant_articles
attr_accessor :preserve_order
attr_accessor :content_proc
@@ -63,12 +63,12 @@
attr_accessor :author_name
attr_accessor :author_uri
attr_accessor :icon
attr_accessor :logo
- def initialize(site, item)
- @site = site
+ def initialize(config, item)
+ @config = config
@item = item
end
def validate
validate_config
@@ -98,47 +98,47 @@
def last_article
sorted_relevant_articles.first
end
def validate_config
- if @site.config[:base_url].nil?
- raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: site configuration has no base_url')
+ if @config[:base_url].nil?
+ raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: site configuration has no base_url')
end
end
def validate_feed_item
if title.nil?
- raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no title in params, item or site config')
+ raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no title in params, item or site config')
end
if author_name.nil?
- raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no author_name in params, item or site config')
+ raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no author_name in params, item or site config')
end
if author_uri.nil?
- raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no author_uri in params, item or site config')
+ raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no author_uri in params, item or site config')
end
end
def validate_articles
if relevant_articles.empty?
- raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: no articles')
+ raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: no articles')
end
if relevant_articles.any? { |a| a[:created_at].nil? }
- raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: one or more articles lack created_at')
+ raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: one or more articles lack created_at')
end
end
def build_for_feed(xml)
xml.instruct!
xml.feed(xmlns: 'http://www.w3.org/2005/Atom') do
- root_url = @site.config[:base_url] + '/'
+ root_url = @config[:base_url] + '/'
# Add primary attributes
xml.id root_url
xml.title title
# Add date
- xml.updated(attribute_to_time(last_article[:created_at]).to_iso8601_time)
+ xml.updated(attribute_to_time(last_article[:created_at]).__nanoc_to_iso8601_time)
# Add links
xml.link(rel: 'alternate', href: root_url)
xml.link(rel: 'self', href: feed_url)
@@ -168,12 +168,12 @@
# 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
+ xml.published attribute_to_time(a[:created_at]).__nanoc_to_iso8601_time
+ xml.updated attribute_to_time(a[:updated_at] || a[:created_at]).__nanoc_to_iso8601_time
# Add specific author information
if a[:author_name] || a[:author_uri]
xml.author do
xml.name a[:author_name] || author_name
@@ -306,21 +306,21 @@
# @return [String] The generated feed content
def atom_feed(params = {})
require 'builder'
# Create builder
- builder = AtomFeedBuilder.new(@site, @item)
+ builder = AtomFeedBuilder.new(@config, @item)
# Fill builder
builder.limit = params[:limit] || 5
builder.relevant_articles = params[:articles] || articles || []
builder.preserve_order = params.fetch(:preserve_order, false)
builder.content_proc = params[:content_proc] || ->(a) { a.compiled_content(snapshot: :pre) }
builder.excerpt_proc = params[:excerpt_proc] || ->(a) { a[:excerpt] }
- builder.title = params[:title] || @item[:title] || @site.config[:title]
- builder.author_name = params[:author_name] || @item[:author_name] || @site.config[:author_name]
- builder.author_uri = params[:author_uri] || @item[:author_uri] || @site.config[:author_uri]
+ builder.title = params[:title] || @item[:title] || @config[:title]
+ builder.author_name = params[:author_name] || @item[:author_name] || @config[:author_name]
+ builder.author_uri = params[:author_uri] || @item[:author_uri] || @config[:author_uri]
builder.icon = params[:icon]
builder.logo = params[:logo]
# Run
builder.validate
@@ -328,56 +328,56 @@
end
# Returns the URL for the given item. It will return the URL containing
# the custom path in the feed if possible, otherwise the normal path.
#
- # @param [Nanoc::Item] item The item for which to fetch the URL.
+ # @param [Nanoc::Int::Item] item The item for which to fetch the URL.
#
# @return [String] The URL of the given item
def url_for(item)
# Check attributes
- if @site.config[:base_url].nil?
- raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: site configuration has no base_url')
+ if @config[:base_url].nil?
+ raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: site configuration has no base_url')
end
# Build URL
if item[:custom_url_in_feed]
item[:custom_url_in_feed]
elsif item[:custom_path_in_feed]
- @site.config[:base_url] + item[:custom_path_in_feed]
+ @config[:base_url] + item[:custom_path_in_feed]
elsif item.path
- @site.config[:base_url] + item.path
+ @config[:base_url] + item.path
end
end
# Returns the URL of the feed. It will return the custom feed URL if set,
# or otherwise the normal feed URL.
#
# @return [String] The URL of the feed
def feed_url
# Check attributes
- if @site.config[:base_url].nil?
- raise Nanoc::Errors::GenericTrivial.new('Cannot build Atom feed: site configuration has no base_url')
+ if @config[:base_url].nil?
+ raise Nanoc::Int::Errors::GenericTrivial.new('Cannot build Atom feed: site configuration has no base_url')
end
- @item[:feed_url] || @site.config[:base_url] + @item.path
+ @item[:feed_url] || @config[:base_url] + @item.path
end
# Returns an URI containing an unique ID for the given item. This will be
# used in the Atom feed to uniquely identify articles. These IDs are
# created using a procedure suggested by Mark Pilgrim and described in his
# ["How to make a good ID in Atom" blog post]
# (http://web.archive.org/web/20110915110202/http://diveintomark.org/archives/2004/05/28/howto-atom-id).
#
- # @param [Nanoc::Item] item The item for which to create an atom tag
+ # @param [Nanoc::Int::Item] item The item for which to create an atom tag
#
# @return [String] The atom tag for the given item
def atom_tag_for(item)
- hostname, base_dir = %r{^.+?://([^/]+)(.*)$}.match(@site.config[:base_url])[1..2]
+ hostname, base_dir = %r{^.+?://([^/]+)(.*)$}.match(@config[:base_url])[1..2]
- formatted_date = attribute_to_time(item[:created_at]).to_iso8601_date
+ formatted_date = attribute_to_time(item[:created_at]).__nanoc_to_iso8601_date
- 'tag:' + hostname + ',' + formatted_date + ':' + base_dir + (item.path || item.identifier)
+ 'tag:' + hostname + ',' + formatted_date + ':' + base_dir + (item.path || item.identifier.to_s)
end
# Converts the given attribute (which can be a string, a Time or a Date)
# into a Time.
#