2011-07-27 22:59:38 -04:00
|
|
|
module Middleman::Features::DirectoryIndexes
|
|
|
|
class << self
|
|
|
|
def registered(app)
|
|
|
|
app.set :ignored_directory_indexes, []
|
|
|
|
app.extend ClassMethods
|
|
|
|
|
|
|
|
app.build_reroute do |destination, request_path|
|
2011-07-27 23:04:35 -04:00
|
|
|
index_ext = File.extname(app.settings.index_file)
|
|
|
|
new_index_path = "/#{app.settings.index_file}"
|
|
|
|
|
|
|
|
indexed_path = request_path.gsub(/\/$/, "") + index_ext
|
2011-07-27 22:59:38 -04:00
|
|
|
|
|
|
|
if app.settings.ignored_directory_indexes.include?(request_path)
|
|
|
|
false
|
2011-08-02 12:32:03 -04:00
|
|
|
elsif request_path =~ /#{new_index_path}$/
|
|
|
|
false
|
2011-07-27 22:59:38 -04:00
|
|
|
else
|
|
|
|
[
|
2011-07-27 23:04:35 -04:00
|
|
|
destination.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path),
|
|
|
|
request_path.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path)
|
2011-07-27 22:59:38 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
app.before do
|
2011-08-02 12:32:03 -04:00
|
|
|
indexed_path = request.path_info.gsub(/\/$/, "") + "/" + app.settings.index_file
|
|
|
|
indexed_exists = resolve_template(indexed_path, :raise_exceptions => false)
|
|
|
|
|
|
|
|
extensioned_path = request.path_info.gsub(/\/$/, "") + File.extname(app.settings.index_file)
|
|
|
|
is_ingored = settings.ignored_directory_indexes.include?(extensioned_path)
|
2011-07-27 22:59:38 -04:00
|
|
|
|
2011-08-02 12:32:03 -04:00
|
|
|
if !indexed_exists && !is_ingored
|
2011-07-27 22:59:38 -04:00
|
|
|
parts = request.path_info.split("/")
|
|
|
|
last_part = parts.last
|
|
|
|
last_part_ext = File.extname(last_part)
|
|
|
|
|
|
|
|
if last_part_ext.blank?
|
|
|
|
# This is a folder, redirect to index
|
2011-08-02 12:32:03 -04:00
|
|
|
request.path_info = extensioned_path
|
2011-07-27 22:59:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
alias :included :registered
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def page(url, options={}, &block)
|
|
|
|
if options.has_key?(:directory_index) && !options["directory_index"]
|
|
|
|
settings.ignored_directory_indexes << url
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|