Module: Nanoc::Helpers::Tagging
- Includes:
- HTMLEscape
- Defined in:
- lib/nanoc/helpers/tagging.rb
Overview
Provides support for managing tags added to items.
To add tags to items, set the tags
attribute to an array of tags that
should be applied to the item.
Instance Method Summary collapse
-
#items_with_tag(tag) ⇒ Array
Find all items with the given tag.
-
#link_for_tag(tag, base_url) ⇒ String
Returns a link to to the specified tag.
-
#tags_for(item, base_url: nil, none_text: '(none)', separator: ', ') ⇒ String
Returns a formatted list of tags for the given item as a string.
Methods included from HTMLEscape
Methods included from Capturing
Instance Method Details
#items_with_tag(tag) ⇒ Array
Find all items with the given tag.
42 43 44 |
# File 'lib/nanoc/helpers/tagging.rb', line 42 def items_with_tag(tag) @items.select { |i| (i[:tags] || []).include?(tag) } end |
#link_for_tag(tag, base_url) ⇒ String
Returns a link to to the specified tag. The link is marked up using the
rel-tag microformat. The href
attribute of the link will be HTML-
escaped, as will the content of the a
element.
57 58 59 |
# File 'lib/nanoc/helpers/tagging.rb', line 57 def link_for_tag(tag, base_url) %(<a href="#{h base_url}#{h tag}" rel="tag">#{h tag}</a>) end |
#tags_for(item, base_url: nil, none_text: '(none)', separator: ', ') ⇒ String
Returns a formatted list of tags for the given item as a string. The tags will be linked using the #link_for_tag function; the HTML-escaping rules for #link_for_tag apply here as well.
29 30 31 32 33 34 35 |
# File 'lib/nanoc/helpers/tagging.rb', line 29 def (item, base_url: nil, none_text: '(none)', separator: ', ') if item[:tags].nil? || item[:tags].empty? none_text else item[:tags].map { |tag| base_url ? link_for_tag(tag, base_url) : tag }.join(separator) end end |