Skip to content
Permalink
Browse files

Support full identifiers in #breadcrumbs_trail

  • Loading branch information...
ddfreyne committed Dec 17, 2016
1 parent 6d8c227 commit eac9bfa5359e4adff67c909300b3be5b4faad703
Showing with 75 additions and 16 deletions.
  1. +13 −11 lib/nanoc/helpers/breadcrumbs.rb
  2. +62 −5 spec/nanoc/helpers/breadcrumbs_spec.rb
@@ -1,24 +1,26 @@
module Nanoc::Helpers
# @see http://nanoc.ws/doc/reference/helpers/#breadcrumbs
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

# @return [Array]
def breadcrumbs_trail
unless @item.identifier.legacy?
raise CannotGetBreadcrumbsForNonLegacyItem.new(@item.identifier)
end

# e.g. ['', '/foo', '/foo/bar']
prefixes =
item.identifier.components
.inject(['']) { |acc, elem| acc + [acc.last + '/' + elem] }

prefixes.map { |pr| @items[Nanoc::Identifier.new('/' + pr, type: :legacy)] }
if @item.identifier.legacy?
prefixes.map { |pr| @items[Nanoc::Identifier.new('/' + pr, type: :legacy)] }
else
prefixes
.reject { |pr| pr =~ /^\/index\./ }
.map do |pr|
if pr == ''
@items['/index.*']
else
@items[Nanoc::Identifier.new(pr).without_ext + '.*']
end
end
end
end
end
end
@@ -62,14 +62,71 @@
end

context 'non-legacy identifiers' do
before do
ctx.create_item('root', {}, '/index.md')
context 'root' do
before do
ctx.create_item('root', {}, Nanoc::Identifier.new('/index.md'))

ctx.item = ctx.items['/index.md']
end

it 'returns an array with the item' do
expect(subject).to eql([ctx.items['/index.md']])
end
end

context 'root and direct child' do
before do
ctx.create_item('child', {}, Nanoc::Identifier.new('/foo.md'))
ctx.create_item('root', {}, Nanoc::Identifier.new('/index.md'))

ctx.item = ctx.items['/foo.md']
end

it 'returns an array with the items' do
expect(subject).to eql([ctx.items['/index.md'], ctx.items['/foo.md']])
end
end

context 'root, child and grandchild' do
before do
ctx.create_item('grandchild', {}, Nanoc::Identifier.new('/foo/bar.md'))
ctx.create_item('child', {}, Nanoc::Identifier.new('/foo.md'))
ctx.create_item('root', {}, Nanoc::Identifier.new('/index.md'))

ctx.item = ctx.items['/index.md']
ctx.item = ctx.items['/foo/bar.md']
end

it 'returns an array with the items' do
expect(subject).to eql([ctx.items['/index.md'], ctx.items['/foo.md'], ctx.items['/foo/bar.md']])
end
end

context 'root, missing child and grandchild' do
before do
ctx.create_item('grandchild', {}, Nanoc::Identifier.new('/foo/bar.md'))
ctx.create_item('root', {}, Nanoc::Identifier.new('/index.md'))

ctx.item = ctx.items['/foo/bar.md']
end

it 'returns an array with the items' do
expect(subject).to eql([ctx.items['/index.md'], nil, ctx.items['/foo/bar.md']])
end
end

it 'raises' do
expect { subject }.to raise_error(Nanoc::Helpers::Breadcrumbs::CannotGetBreadcrumbsForNonLegacyItem)
context 'index.md child' do
# No special handling of non-root index.* files.

before do
ctx.create_item('grandchild', {}, Nanoc::Identifier.new('/foo/index.md'))
ctx.create_item('root', {}, Nanoc::Identifier.new('/index.md'))

ctx.item = ctx.items['/foo/index.md']
end

it 'returns an array with the items' do
expect(subject).to eql([ctx.items['/index.md'], nil, ctx.items['/foo/index.md']])
end
end
end
end

0 comments on commit eac9bfa

Please sign in to comment.
You can’t perform that action at this time.