Module: Nanoc::Toolbox::Helpers::BloggingExtra

Includes:
Helpers::Blogging
Included in:
Disqus
Defined in:
lib/nanoc/toolbox/helpers/blogging_extra.rb

Overview

NANOC Helper for giving items extra blog post behavior.

This module contains features to the default Nanoc::Helpers::Blogging module, like tagging, slug, etc…

Author:

  • Anouar ADLANI

Instance Method Summary collapse

Instance Method Details

#act_as_post(item) ⇒ Nanoc::Item

Enable blog post behavior on an item

Examples:

items.each do |item|
  act_as_post(item)
end

Parameters:

  • item (Nanoc::Item)

    the item that will act as a post

Returns:

  • (Nanoc::Item)

    the adapted item



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nanoc/toolbox/helpers/blogging_extra.rb', line 39

def act_as_post(item)
  fname = slug_for(item)

  if fname =~ /^\d+-\d+-\d+-/
    # get the date from the filename
    item[:year], item[:month], item[:day] = fname.split("-", 4)[0..2]

    # Set creation date param from the values in the filename
    item[:created_at] = Time.local(item[:year], item[:month], item[:day])

    # Strip the date from the filename
    item[:slug] = fname.sub(/^\d+-\d+-\d+-/,'')
  else
    item[:created_at] ||= Time.now
    item[:created_at] = attribute_to_time(item[:created_at])

    # get the date from
    item[:year]  = item[:created_at].year
    item[:month] = item[:created_at].month
    item[:day]   = item[:created_at].day
  end

  # Add additional meta data
  item[:kind] = 'article'

  # Enable comments unless specifically turned off
  item[:comments] = true unless item[:comments] === false
  item
end

#add_post_attributesObject

Enable blog post behavior on all the items located in the post folder(s)

Examples:

# In your config.yaml
# post_dirs: [ 'posts' ]

# In your Rules file
preprocess do
  add_post_attributes
end


20
21
22
23
24
25
26
27
28
# File 'lib/nanoc/toolbox/helpers/blogging_extra.rb', line 20

def add_post_attributes
  @config[:post_dirs] ||= ['_posts', '_articles']
  items.each do |item|
    # check the item's parent directory against the post_dirs
    if (item[:filename] && @config[:post_dirs].include?(File.dirname(item[:filename]).split('/').last))
      act_as_post(item)
    end
  end
end

#posts_by_dateHash

Retreive the list of existing articles grouped by years and months

Examples:

posts_by_date # => { 2011 => { 12 => [item0, item1], 3 => [item0, item2]}, 2010 => {12 => [...]}}

Returns:

  • (Hash)

    Items grouped in a hash



100
101
102
103
# File 'lib/nanoc/toolbox/helpers/blogging_extra.rb', line 100

def posts_by_date
  Hash[sorted_articles.group_by{|item| item[:year]}.map{ |y, items|
    [y, items.group_by{|i| i[:month]}]}]
end

#recent_posts(count = 5, current_item = nil) ⇒ Object

Returns n number of recent posts, optionally omits the current one

#return [Array<Nanoc::Item>] an array of recent items

Parameters:

  • count (Integer) (defaults to: 5)

    the number of item to return

  • current_item (Nanoc::Item) (defaults to: nil)

    the current item to exclude from the list



91
92
93
# File 'lib/nanoc/toolbox/helpers/blogging_extra.rb', line 91

def recent_posts(count=5, current_item=nil)
  (sorted_articles - [current_item])[0, count]
end

#slug_for(item) ⇒ String

Get an item’s slug if it has one, or use its filename sans extension

Examples:

# item[:filename] = "abc def geh.html"
slug_for(item) # => "abc-def-geh"

Parameters:

  • item (Nanoc::Item)

    the item that will act as a post

Returns:

  • (String)

    either the slug specified in the item or generate one



77
78
79
80
81
82
83
# File 'lib/nanoc/toolbox/helpers/blogging_extra.rb', line 77

def slug_for(item)
  return item[:slug] if item[:slug]

  item[:slug] = File.basename(item[:filename], File.extname(item[:filename]))
  item[:slug].gsub!(/\s+/, '-')
  item[:slug]
end