Sha256: d5a433581a50f3c4dec9ce27df590d5fa091f24545c1ee2144eee891e0404c7c

Contents?: true

Size: 1.32 KB

Versions: 12

Compression:

Stored size: 1.32 KB

Contents

module Nanoc::Helpers
  # Provides support for breadcrumbs, which allow the user to go up in the
  # page hierarchy.
  module Breadcrumbs
    class CannotGetBreadcrumbsForNonLegacyItem < Nanoc::Int::Errors::Generic
      def initialize(identifier)
        super("You cannot build a breadcrumbs trail for an item that has a “full” identifier (#{identifier}). Doing so is only possible for items that have a legacy identifier.")
      end
    end

    # 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
      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
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
nanoc-4.2.4 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.2.3 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.2.2 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.2.1 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.2.0 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.1.6 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.2.0b1 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.1.5 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.1.4 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.1.3 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.1.2 lib/nanoc/helpers/breadcrumbs.rb
nanoc-4.1.1 lib/nanoc/helpers/breadcrumbs.rb