Skip to content

Commit

Permalink
Merge pull request #1011 from nanoc/gh-1010-non-legacy-breadcrumbs
Browse files Browse the repository at this point in the history
Support full identifiers in breadcrumbs
  • Loading branch information
denisdefreyne committed Dec 17, 2016
2 parents e4bf58f + 752c340 commit 60187e9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 59 deletions.
36 changes: 15 additions & 21 deletions lib/nanoc/helpers/breadcrumbs.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
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']
components = item.identifier.components
prefixes = components.inject(['']) { |acc, elem| acc + [acc.last + '/' + elem] }

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]
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

trail
end
end
end
135 changes: 97 additions & 38 deletions spec/nanoc/helpers/breadcrumbs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,126 @@
describe '#breadcrumbs_trail' do
subject { helper.breadcrumbs_trail }

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

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

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

it 'returns an array with the item' do
expect(subject).to eql([ctx.items['/']])
context 'root and direct child' do
before do
ctx.create_item('child', {}, Nanoc::Identifier.new('/foo/', type: :legacy))
ctx.create_item('root', {}, Nanoc::Identifier.new('/', type: :legacy))

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

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

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

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

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

it 'returns an array with the items' do
expect(subject).to eql([ctx.items['/'], ctx.items['/foo/']])
context 'root, missing child and grandchild' do
before do
ctx.create_item('grandchild', {}, Nanoc::Identifier.new('/foo/bar/', type: :legacy))
ctx.create_item('root', {}, Nanoc::Identifier.new('/', type: :legacy))

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

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

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

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

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

context 'root, missing child and grandchild' do
before do
ctx.create_item('grandchild', {}, Nanoc::Identifier.new('/foo/bar/', type: :legacy))
ctx.create_item('root', {}, Nanoc::Identifier.new('/', type: :legacy))
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/bar/']
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

it 'returns an array with the items' do
expect(subject).to eql([ctx.items['/'], nil, ctx.items['/foo/bar/']])
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['/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
end

context 'non-legacy identifiers' do
before do
ctx.create_item('root', {}, '/index.md')
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['/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
Expand Down

0 comments on commit 60187e9

Please sign in to comment.