module Nanoc::Helpers::Blogging

@see nanoc.app/doc/reference/helpers/#blogging

Public Instance Methods

articles() click to toggle source

@return [Array]

# File lib/nanoc/helpers/blogging.rb, line 7
def articles
  blk = -> { @items.select { |item| item[:kind] == 'article' } }
  if @items.frozen?
    @article_items ||= blk.call
  else
    blk.call
  end
end
atom_feed(params = {}) click to toggle source

@option params [Number] :limit @option params [Array] :articles @option params [Boolean] :preserve_order @option params [Proc] :content_proc @option params [Proc] :excerpt_proc @option params [Proc] :title_proc @option params [String] :alt_link @option params [String] :id @option params [String] :title @option params [String] :author_name @option params [String] :author_uri @option params [String] :icon @option params [String] :logo

@return [String]

# File lib/nanoc/helpers/blogging.rb, line 191
def atom_feed(params = {})
  require 'builder'

  # Create builder
  builder = AtomFeedBuilder.new(@config, @item)

  # Fill builder
  builder.alt_link          = params[:alt_link]
  builder.id                = params[:id]
  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_proc        = params[:title_proc] || ->(a) { a[:title] }
  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
  builder.build
end
atom_tag_for(item) click to toggle source

@return [String]

# File lib/nanoc/helpers/blogging.rb, line 245
def atom_tag_for(item)
  hostname, base_dir = %r{^.+?://([^/]+)(.*)$}.match(@config[:base_url])[1..2]

  formatted_date = attribute_to_time(item[:created_at]).__nanoc_to_iso8601_date

  'tag:' + hostname + ',' + formatted_date + ':' + base_dir + (item.path || item.identifier.to_s)
end
attribute_to_time(arg) click to toggle source

@param [String, Time, Date, DateTime] arg

@return [Time]

# File lib/nanoc/helpers/blogging.rb, line 256
def attribute_to_time(arg)
  case arg
  when DateTime
    arg.to_time
  when Date
    Time.utc(arg.year, arg.month, arg.day)
  when String
    Time.parse(arg)
  else
    arg
  end
end
feed_url() click to toggle source

@return [String]

# File lib/nanoc/helpers/blogging.rb, line 235
def feed_url
  # Check attributes
  if @config[:base_url].nil?
    raise Nanoc::Core::TrivialError.new('Cannot build Atom feed: site configuration has no base_url')
  end

  @item[:feed_url] || @config[:base_url] + @item.path
end
sorted_articles() click to toggle source

@return [Array]

# File lib/nanoc/helpers/blogging.rb, line 17
def sorted_articles
  blk = -> { articles.sort_by { |a| attribute_to_time(a[:created_at]) }.reverse }

  if @items.frozen?
    @sorted_article_items ||= blk.call
  else
    blk.call
  end
end
url_for(item) click to toggle source

@return [String]

# File lib/nanoc/helpers/blogging.rb, line 218
def url_for(item)
  # Check attributes
  if @config[:base_url].nil?
    raise Nanoc::Core::TrivialError.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]
    @config[:base_url] + item[:custom_path_in_feed]
  elsif item.path
    @config[:base_url] + item.path
  end
end