Skip to content

Commit

Permalink
Merge pull request #1059 from nanoc/gh-1022
Browse files Browse the repository at this point in the history
Allow periods in identifiers for full identifiers
  • Loading branch information
denisdefreyne committed Jan 11, 2017
2 parents 83b0b7d + 4db95da commit 1f284ef
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/nanoc/data_sources/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ def identifier_for_filename(filename)

regex =
if filename =~ /(^|\/)index(\.[^\/]+)?$/
@config && @config[:allow_periods_in_identifiers] ? /\/?(index)?(\.[^\/\.]+)?$/ : /\/?index(\.[^\/]+)?$/
allow_periods_in_identifiers? ? /\/?(index)?(\.[^\/\.]+)?$/ : /\/?index(\.[^\/]+)?$/
else
@config && @config[:allow_periods_in_identifiers] ? /\.[^\/\.]+$/ : /\.[^\/]+$/
allow_periods_in_identifiers? ? /\.[^\/\.]+$/ : /\.[^\/]+$/
end
Nanoc::Identifier.new(filename.sub(regex, ''), type: :legacy)
end
Expand All @@ -329,13 +329,21 @@ def ext_of(filename)
#
# @return [Regex]
def extension_regex
if @config && @config[:allow_periods_in_identifiers]
if allow_periods_in_identifiers?
/(\.[^\/\.]+$)/
else
/(\.[^\/]+$)/
end
end

def allow_periods_in_identifiers?
if @config
@config[:allow_periods_in_identifiers] || @config[:identifier_type] == 'full'
else
false
end
end

# @return [ParseResult]
def parse(content_filename, meta_filename)
if meta_filename
Expand Down
24 changes: 24 additions & 0 deletions spec/nanoc/regressions/gh_1022_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe 'GH-1022', site: true, stdio: true do
before do
File.write('content/ubuntu-16.10-server-amd64.iso.txt', 'torrent contents')
File.write('content/ubuntu-16.10-server-amd64.iso.yaml', 'distro: Ubuntu')

File.write('layouts/default.erb', '<%= @item[:distro] %> / <%= yield %>')

File.write('Rules', <<EOS)
compile '/**/*' do
layout '/default.*'
write item.identifier
end
layout '/*.erb', :erb
EOS
end

it 'recompiles all reps of a changed item' do
Nanoc::CLI.run(%w(compile))

expect(File.file?('output/ubuntu-16.10-server-amd64.iso.txt')).to be
expect(File.read('output/ubuntu-16.10-server-amd64.iso.txt')).to eq('Ubuntu / torrent contents')
end
end
26 changes: 26 additions & 0 deletions test/data_sources/test_filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,32 @@ def test_all_split_files_in_with_multiple_content_files
end
end

def test_basename_of_with_full_style_identifiers
# Create data source
config = { identifier_type: 'full' }
data_source = Nanoc::DataSources::Filesystem.new(nil, nil, nil, config)

# Get input and expected output
expected = {
'/' => '/',
'/foo' => '/foo',
'/foo.html' => '/foo',
'/foo.xyz.html' => '/foo.xyz',
'/foo/bar' => '/foo/bar',
'/foo/bar.html' => '/foo/bar',
'/foo/bar.xyz.html' => '/foo/bar.xyz',
}

# Check
expected.each_pair do |input, expected_output|
actual_output = data_source.send(:basename_of, input)
assert_equal(
expected_output, actual_output,
"basename_of(#{input.inspect}) should equal #{expected_output.inspect}, not #{actual_output.inspect}"
)
end
end

def test_basename_of_allowing_periods_in_identifiers
# Create data source
data_source = Nanoc::DataSources::Filesystem.new(nil, nil, nil, allow_periods_in_identifiers: true)
Expand Down

0 comments on commit 1f284ef

Please sign in to comment.