Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better index filename handling in filesystem data source #330

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/nanoc/data_sources/filesystem_unified.rb
Expand Up @@ -36,7 +36,7 @@ module Nanoc::DataSources
#
# (`allow_periods_in_identifiers` set to true)
# foo.entry.html → /foo.entry/
#
#
# (`allow_periods_in_identifiers` set to false)
# foo.html.erb → /foo/
#
Expand Down Expand Up @@ -108,8 +108,8 @@ def filename_for(base_filename, ext)
# Returns the identifier derived from the given filename, first stripping
# the given directory name off the filename.
def identifier_for_filename(filename)
if filename =~ /(^|\/)index\.[^\/]+$/
regex = ((@config && @config[:allow_periods_in_identifiers]) ? /\/?index\.[^\/\.]+$/ : /\/?index\.[^\/]+$/)
if filename =~ /(^|\/)index(\.[^\/]+)?$/
regex = ((@config && @config[:allow_periods_in_identifiers]) ? /\/?(index)?(\.[^\/\.]+)?$/ : /\/?index(\.[^\/]+)?$/)
else
regex = ((@config && @config[:allow_periods_in_identifiers]) ? /\.[^\/\.]+$/ : /\.[^\/]+$/)
end
Expand Down
40 changes: 40 additions & 0 deletions test/data_sources/test_filesystem_unified.rb
Expand Up @@ -238,6 +238,46 @@ def test_identifier_for_filename_with_subfilename_disallowing_periods_in_identif
end
end

def test_identifier_for_filename_with_index_filenames_allowing_periods_in_identifier
expected = {
'/index.html.erb' => '/index.html/',
'/index.html' => '/',
'/index' => '/',
'/foo/index.html.erb' => '/foo/index.html/',
'/foo/index.html' => '/foo/',
'/foo/index' => '/foo/',
}

data_source = new_data_source(:allow_periods_in_identifiers => true)
expected.each_pair do |input, expected_output|
actual_output = data_source.send(:identifier_for_filename, input)
assert_equal(
expected_output, actual_output,
"identifier_for_filename(#{input.inspect}) should equal #{expected_output.inspect}, not #{actual_output.inspect}"
)
end
end

def test_identifier_for_filename_with_index_filenames_disallowing_periods_in_identifier
expected = {
'/index.html.erb' => '/',
'/index.html' => '/',
'/index' => '/',
'/foo/index.html.erb' => '/foo/',
'/foo/index.html' => '/foo/',
'/foo/index' => '/foo/',
}

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

def test_load_objects_allowing_periods_in_identifiers
# Create data source
data_source = new_data_source(:allow_periods_in_identifiers => true)
Expand Down