Make directory_indexes use provides_metadata_for_path instead of its own list of ignored paths, allowing it to support regexes/globs

This commit is contained in:
Ben Hollis 2012-03-15 00:29:50 -07:00
parent 73350d6d05
commit 5db1a9453a
4 changed files with 36 additions and 45 deletions

View file

@ -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"

View file

@ -1,2 +1,4 @@
activate :directory_indexes
page "/leave_me_alone.html", :directory_index => false
page "/leave_me_alone.html", :directory_index => false
page "/wildcard*", :directory_index => false

View file

@ -0,0 +1 @@
Stay away, wildcards!

View file

@ -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<Middleman::Sitemap::Page>]
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