Module: Nanoc2::Helpers::Tagging

Defined in:
lib/nanoc2/helpers/tagging.rb

Overview

Nanoc2::Helpers::Tagging provides some support for managing tags added to pages. To add tags to pages, set the tags page attribute to an array of tags that should be applied to the page. For example:

tags: [ 'foo', 'bar', 'baz' ]

To activate this helper, include it, like this:

include Nanoc2::Helpers::Tagging

Instance Method Summary collapse

Instance Method Details

Returns a link to to the specified tag. The link is marked up using the rel-tag microformat.

tag

The name of the tag, which should consist of letters and numbers (no spaces, slashes, or other special characters).

base_url

The URL to which the tag will be appended to construct the link URL. This URL must have a trailing slash.



50
51
52
# File 'lib/nanoc2/helpers/tagging.rb', line 50

def link_for_tag(tag, base_url)
  %[<a href="#{base_url}#{tag}" rel="tag">#{tag}</a>]
end

#pages_with_tag(tag) ⇒ Object

Returns all pages with the given tag.



38
39
40
# File 'lib/nanoc2/helpers/tagging.rb', line 38

def pages_with_tag(tag)
  @pages.select { |p| (p.tags || []).include?(tag) }
end

#tags_for(page, params = {}) ⇒ Object

Returns a formatted list of tags for the given page as a string. Several parameters allow customization:

:base_url

The URL to which the tag will be appended to construct the link URL. This URL must have a trailing slash. Defaults to “technorati.com/tag/”.

:none_text

The text to display when the page has no tags. Defaults to “(none)”.

:separator

The separator to put between tags. Defaults to “, ”.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nanoc2/helpers/tagging.rb', line 25

def tags_for(page, params={})
  base_url  = params[:base_url]  || 'http://technorati.com/tag/'
  none_text = params[:none_text] || '(none)'
  separator = params[:separator] || ', '

  if page.tags.nil? or page.tags.empty?
    none_text
  else
    page.tags.collect { |tag| link_for_tag(tag, base_url) }.join(separator)
  end
end