Sha256: a76306070015fdcd3d4bae0bb1736c9a5f672ff61fc389e5e75e7e110062b0ff

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module Nanoc2::Helpers

  # 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
  module Tagging

    # 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
    #             "http://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 ", ".
    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

    # Returns all pages with the given tag.
    def pages_with_tag(tag)
      @pages.select { |p| (p.tags || []).include?(tag) }
    end

    # 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.
    def link_for_tag(tag, base_url)
      %[<a href="#{base_url}#{tag}" rel="tag">#{tag}</a>]
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nanoc2-2.2.3 lib/nanoc2/helpers/tagging.rb