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…
Instance Method Summary collapse
-
#act_as_post(item) ⇒ Nanoc::Item
Enable blog post behavior on an item.
-
#add_post_attributes ⇒ Object
Enable blog post behavior on all the items located in the post folder(s).
-
#posts_by_date ⇒ Hash
Retreive the list of existing articles grouped by years and months.
-
#recent_posts(count = 5, current_item = nil) ⇒ Object
Returns n number of recent posts, optionally omits the current one.
-
#slug_for(item) ⇒ String
Get an item’s slug if it has one, or use its filename sans extension.
Instance Method Details
#act_as_post(item) ⇒ Nanoc::Item
Enable blog post behavior on an 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_attributes ⇒ Object
Enable blog post behavior on all the items located in the post folder(s)
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_date ⇒ Hash
Retreive the list of existing articles grouped by years and months
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
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
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 |