Module: Nanoc::Helpers::Breadcrumbs

Defined in:
lib/nanoc/helpers/breadcrumbs.rb

Overview

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

Defined Under Namespace

Classes: CannotGetBreadcrumbsForNonLegacyItem

Instance Method Summary collapse

Instance Method Details

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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nanoc/helpers/breadcrumbs.rb', line 19

def breadcrumbs_trail
  unless @item.identifier.legacy?
    raise CannotGetBreadcrumbsForNonLegacyItem.new(@item.identifier)
  end

  trail      = []
  idx_start  = 0

  loop do
    idx = @item.identifier.to_s.index('/', idx_start)
    break if idx.nil?

    idx_start = idx + 1
    identifier = @item.identifier.to_s[0..idx]
    trail << @items[identifier]
  end

  trail
end