Module: Nanoc::Helpers::Blogging

Included in:
AtomFeedBuilder
Defined in:
lib/nanoc/helpers/blogging.rb

Overview

Defined Under Namespace

Classes: AtomFeedBuilder

Instance Method Summary collapse

Instance Method Details

#articlesArray

Returns:

  • (Array)


7
8
9
10
11
12
13
14
# 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 = {}) ⇒ String

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :limit (Number)
  • :articles (Array)
  • :preserve_order (Boolean)
  • :content_proc (Proc)
  • :excerpt_proc (Proc)
  • :title_proc (Proc)
  • :alt_link (String)
  • :id (String)
  • :title (String)
  • :author_name (String)
  • :author_uri (String)
  • :icon (String)
  • :logo (String)

Returns:

  • (String)


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# 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.              = params[:logo]

  # Run
  builder.validate
  builder.build
end

#atom_tag_for(item) ⇒ String

Returns:

  • (String)


245
246
247
248
249
250
251
# 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) ⇒ Time

Parameters:

  • arg (String, Time, Date, DateTime)

Returns:



256
257
258
259
260
261
262
263
264
265
266
267
# 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_urlString

Returns:

  • (String)


235
236
237
238
239
240
241
242
# 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_articlesArray

Returns:

  • (Array)


17
18
19
20
21
22
23
24
25
# 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) ⇒ String

Returns:

  • (String)


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# 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