Sha256: 6284f8e697745ec969a6f4cb874d68eef108e6ee4a999a0448917cf89706410d

Contents?: true

Size: 1.19 KB

Versions: 12

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8

module Nanoc3::Helpers

  # Provides support for breadcrumbs, which allow the user to go up in the
  # page hierarchy.
  module Breadcrumbs

    # Creates a breadcrumb trail leading from the current item to its parent,
    # to its parent’s parent, etc, until the root item is reached. This
    # function does not require that each intermediate item exist; for
    # example, if there is no `/foo/` item, breadcrumbs for a `/foo/bar/` item
    # will contain a `nil` element.
    #
    # @return [Array] The breadcrumbs, starting with the root item and ending
    # with the item itself
    def breadcrumbs_trail
      breadcrumbs_for_identifier(@item.identifier)
    end

    def item_with_identifier(identifier)
      @identifier_cache ||= {}
      @identifier_cache[identifier] ||= begin
        @items.find { |i| i.identifier == identifier }
      end
    end

    def breadcrumbs_for_identifier(identifier)
      @breadcrumbs_cache ||= {}
      @breadcrumbs_cache[identifier] ||= begin
        head = (identifier == '/' ? [] :  breadcrumbs_for_identifier(identifier.sub(/[^\/]+\/$/, '')) )
        tail = [ item_with_identifier(identifier) ]

        head + tail
      end
    end

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
nanoc3-3.1.9 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.8 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.7 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.6 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.5 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.4 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.3 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.2 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.1 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.0 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.0rc1 lib/nanoc3/helpers/breadcrumbs.rb
nanoc3-3.1.0b2 lib/nanoc3/helpers/breadcrumbs.rb