diff --git a/middleman-core/features/directory_index.feature b/middleman-core/features/directory_index.feature index 9f74daac..b5a79000 100644 --- a/middleman-core/features/directory_index.feature +++ b/middleman-core/features/directory_index.feature @@ -8,6 +8,7 @@ Feature: Directory Index | needs_index/index.html | | a_folder/needs_index/index.html | | leave_me_alone.html | + | wildcard_leave_me_alone.html | | regular/index.html | | .htaccess | Then the following files should not exist: @@ -15,6 +16,7 @@ Feature: Directory Index | needs_index.html | | a_folder/needs_index.html | | leave_me_alone/index.html | + | wildcard_leave_me_alone/index.html | And the file "needs_index/index.html" should contain "Indexable" And the file "a_folder/needs_index/index.html" should contain "Indexable" And the file "leave_me_alone.html" should contain "Stay away" diff --git a/middleman-core/fixtures/indexable-app/config.rb b/middleman-core/fixtures/indexable-app/config.rb index d770e11f..54b79b5c 100644 --- a/middleman-core/fixtures/indexable-app/config.rb +++ b/middleman-core/fixtures/indexable-app/config.rb @@ -1,2 +1,4 @@ activate :directory_indexes -page "/leave_me_alone.html", :directory_index => false \ No newline at end of file +page "/leave_me_alone.html", :directory_index => false + +page "/wildcard*", :directory_index => false diff --git a/middleman-core/fixtures/indexable-app/source/wildcard_leave_me_alone.html b/middleman-core/fixtures/indexable-app/source/wildcard_leave_me_alone.html new file mode 100644 index 00000000..5ff5317a --- /dev/null +++ b/middleman-core/fixtures/indexable-app/source/wildcard_leave_me_alone.html @@ -0,0 +1 @@ +Stay away, wildcards! diff --git a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb index 4c591c9d..689fb7f6 100644 --- a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb +++ b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb @@ -9,63 +9,49 @@ module Middleman::Extensions # Once registered def registered(app) - # Include methods - app.send :include, InstanceMethods - app.after_configuration do # Register a reroute transform that turns regular paths into indexed paths sitemap.reroute do |destination, page| new_index_path = "/#{index_file}" - frontmatter_ignore = false - # Check for file and frontmatter - d = page.data - if !page.data.nil? - frontmatter_ignore = d.has_key?("directory_index") && d["directory_index"] == false - end - - index_ext = File.extname(index_file) - - # Only reroute if not ignored + # Check if it would be pointless to reroute path = page.path - if ignored_directory_indexes.include? page - destination - elsif path == index_file || path.end_with?(new_index_path) - destination - elsif frontmatter_ignore - destination - elsif index_ext != File.extname(path) - destination - else - destination.chomp(File.extname(index_file)) + new_index_path + page_already_index = path == index_file || path.end_with?(new_index_path) + if page_already_index || File.extname(index_file) != File.extname(path) + next destination end + + # Check if frontmatter turns directory_index off + d = page.data + next destination if d && d["directory_index"] == false + + # Check if file metadata (options set by "page" in config.rb) turns directory_index off + # TODO: This is crazy - we need a generic way to get metadata for paths + metadata_ignore = false + provides_metadata_for_path.each do |callback, matcher| + if matcher.is_a? Regexp + next if !path.match(matcher) + elsif matcher.is_a? String + next if !File.fnmatch("/" + matcher.sub(%r{^/}, ''), "/#{path}") + end + + result = instance_exec(path, &callback) + if result[:options] && result[:options][:directory_index] == false + metadata_ignore = true + break + end + end + + next destination if metadata_ignore + + # Not ignored, so reroute + destination.chomp(File.extname(index_file)) + new_index_path end end end alias :included :registered end - - module InstanceMethods - # A list of pages which will not use directory indexes - # @return [Array] - def ignored_directory_indexes - @_ignored_directory_indexes ||= [] - end - - # Override the page helper to accept a :directory_index option - # - # @param [String] url - # @param [Hash] options - # @return [void] - def page(url, options={}, &block) - if options.has_key?(:directory_index) && !options["directory_index"] - ignored_directory_indexes << sitemap.page(url) - else - super - end - end - end end # Register the extension