Module: Nanoc::Toolbox::Helpers::TaggingExtra
- Includes:
- Helpers::Blogging
- Defined in:
- lib/nanoc/toolbox/helpers/tagging_extra.rb
Overview
NANOC Helper for added tagging functions
This module contains functions for …
Instance Method Summary collapse
-
#count_tags(items = nil) ⇒ Hash
Count the tags in a given collection of items.
-
#create_tag_pages(items = nil, options = {}) ⇒ Object
Creates in-memory tag pages from tags of the passed items or form all items.
-
#has_tag?(item, tag) ⇒ Boolean
Return true if an item has a specified tag.
-
#items_with_tag(tag, items = nil) ⇒ Object
Finds all the items having a specified tag.
-
#rank_tags(n, items = nil) ⇒ Object
Sort the tags of an item collection (defaults to all site items) in ‘n’ classes of rank.
-
#tag_links_for(item, omit_tags = [], options = {}) ⇒ Array<String>
Returns an Array of links to tags in the item, optionally omits the given tags from the selection.
-
#tag_set(items = nil) ⇒ Array<String>
Returns all the tags present in a collection of items.
Instance Method Details
#count_tags(items = nil) ⇒ Hash
Count the tags in a given collection of items. By default, the method counts tags in all the site items. The result is an hash such as: { tag => count }.
50 51 52 53 54 |
# File 'lib/nanoc/toolbox/helpers/tagging_extra.rb', line 50 def (items=nil) items ||= @items = items.map { |i| i[:tags] }.flatten.delete_if{|t| t.nil?} .inject(Hash.new(0)) {|h,i| h[i] += 1; h } end |
#create_tag_pages(items = nil, options = {}) ⇒ Object
Creates in-memory tag pages from tags of the passed items or form all items
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/nanoc/toolbox/helpers/tagging_extra.rb', line 120 def create_tag_pages(items=nil, ={}) [:tag_pattern] ||= "%%tag%%" [:title] ||= [:tag_pattern] [:identifier] ||= "/tags/#{[:tag_pattern]}/" [:template] ||= "tag" tag_set(items).each do |tagname| raw_content = "<%= render('#{[:template]}', :tag => '#{tagname}') %>" attributes = { :title => [:title].gsub([:tag_pattern], tagname) } identifier = [:identifier].gsub([:tag_pattern], tagname) @items << Nanoc::Item.new(raw_content, attributes, identifier, :binary => false) end end |
#has_tag?(item, tag) ⇒ Boolean
Return true if an item has a specified tag
27 28 29 30 |
# File 'lib/nanoc/toolbox/helpers/tagging_extra.rb', line 27 def has_tag?(item, tag) return false if item[:tags].nil? item[:tags].include? tag end |
#items_with_tag(tag, items = nil) ⇒ Object
Finds all the items having a specified tag. By default the method search in all the site items. Alternatively, an item collection can be passed as second (optional) parameter, to restrict the search in the collection.
39 40 41 42 |
# File 'lib/nanoc/toolbox/helpers/tagging_extra.rb', line 39 def items_with_tag(tag, items=nil) items = sorted_articles if items.nil? items.select { |item| has_tag?( item, tag ) } end |
#rank_tags(n, items = nil) ⇒ Object
Sort the tags of an item collection (defaults to all site items) in ‘n’ classes of rank. The rank 0 corresponds to the most frequent tags. The rank ‘n-1’ to the least frequents. The result is a hash such as: { tag => rank }
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nanoc/toolbox/helpers/tagging_extra.rb', line 63 def (n, items=nil) raise ArgumentError, 'the number of ranks should be > 1' if n < 2 items = @items if items.nil? count = ( items ) min, max = count.values.minmax ranker = lambda { |num| n - 1 - (num - min) / (max - min) } Hash[count.map {|tag,value| [tag, ranker.call(value) ] }] end |
#tag_links_for(item, omit_tags = [], options = {}) ⇒ Array<String>
Returns an Array of links to tags in the item, optionally omits the given tags from the selection
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/nanoc/toolbox/helpers/tagging_extra.rb', line 94 def tag_links_for(item, =[], ={}) = [] return unless item[:tags] [:tag_pattern] ||= "%%tag%%" [:title] ||= [:tag_pattern] [:file_extension] ||= ".html" [:url_format] ||= "/tags/#{[:tag_pattern]}#{[:file_extension]}" = item[:tags] - .map! do |tag| title = [:title].gsub([:tag_pattern], tag.downcase) url = [:url_format].gsub([:tag_pattern], tag.downcase) content_tag('a', title, {:href => url}) end end |
#tag_set(items = nil) ⇒ Array<String>
Returns all the tags present in a collection of items. The tags are only present once in the returned value. When called whithout parameters, all the site items are considered.
17 18 19 20 |
# File 'lib/nanoc/toolbox/helpers/tagging_extra.rb', line 17 def tag_set(items=nil) items ||= @items items.map { |i| i[:tags] }.flatten.uniq.delete_if{|t| t.nil?} end |