From ca1f3ddf833df43919170dce3945b663e5514d54 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Thu, 2 Feb 2012 21:47:54 -0800 Subject: [PATCH] Directory indexes now work during build, with the exception of .htaccess files. --- .../lib/middleman-core/cli/build.rb | 4 +- .../middleman-core/core_extensions/routing.rb | 4 +- .../middleman-core/core_extensions/sitemap.rb | 6 +-- .../extensions/directory_indexes.rb | 49 ++++++++++--------- .../lib/middleman-core/sitemap/page.rb | 2 +- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/middleman-core/lib/middleman-core/cli/build.rb b/middleman-core/lib/middleman-core/cli/build.rb index 45d61c35..c3bae1d2 100644 --- a/middleman-core/lib/middleman-core/cli/build.rb +++ b/middleman-core/lib/middleman-core/cli/build.rb @@ -101,10 +101,10 @@ module Middleman::Cli # @return [void] def tilt_template(page) build_dir = self.class.shared_instance.build_dir + output_file = File.join(self.class.shared_instance.build_dir, page.output_file_path) begin response = self.class.shared_rack.get(page.request_path.gsub(/\s/, "%20")) - output_file = File.join(self.class.shared_instance.build_dir, page.destination_path) create_file(output_file, response.body, { :force => true }) rescue say_status :error, destination, :red @@ -228,6 +228,8 @@ module Middleman::Cli page = @app.sitemap.page(file_source) + puts "DEST: #{page.destination_path}" + next if @config[:glob] && !File.fnmatch(@config[:glob], file_source) base.tilt_template(page) diff --git a/middleman-core/lib/middleman-core/core_extensions/routing.rb b/middleman-core/lib/middleman-core/core_extensions/routing.rb index d5ec1808..6be37409 100644 --- a/middleman-core/lib/middleman-core/core_extensions/routing.rb +++ b/middleman-core/lib/middleman-core/core_extensions/routing.rb @@ -64,7 +64,7 @@ module Middleman::CoreExtensions::Routing # Setup proxy if opts.has_key?(:proxy) - reroute(url, opts[:proxy]) + proxy(url, opts[:proxy]) if opts.has_key?(:ignore) && opts[:ignore] ignore(opts[:proxy]) @@ -89,4 +89,4 @@ module Middleman::CoreExtensions::Routing end end end -end \ No newline at end of file +end diff --git a/middleman-core/lib/middleman-core/core_extensions/sitemap.rb b/middleman-core/lib/middleman-core/core_extensions/sitemap.rb index a2c7e6b9..4366d976 100644 --- a/middleman-core/lib/middleman-core/core_extensions/sitemap.rb +++ b/middleman-core/lib/middleman-core/core_extensions/sitemap.rb @@ -74,10 +74,10 @@ module Middleman::CoreExtensions::Sitemap # @param [String] url # @param [String] target # @return [void] - def reroute(*args) + def proxy(*args) sitemap.proxy(*args) end - + # Register a handler to provide metadata on a file path # @param [Regexp] matcher # @return [Array>] @@ -96,4 +96,4 @@ module Middleman::CoreExtensions::Sitemap @_provides_metadata_for_path end end -end \ No newline at end of file +end diff --git a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb index 52264097..aa1d8e5f 100644 --- a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb +++ b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb @@ -12,6 +12,8 @@ module Middleman::Extensions # Include methods app.send :include, InstanceMethods + # TODO: unify these + # Before requests app.before do prefix = @original_path.sub(/\/$/, "") @@ -51,41 +53,40 @@ module Middleman::Extensions end end - # Basically does the same as above, but in build mode - app.build_reroute do |destination, request_path| - index_ext = File.extname(index_file) - new_index_path = "/#{index_file}" - frontmatter_ignore = false + app.after_configuration do + # Basically does the same as above, but in build mode + sitemap.reroute do |destination, page| + new_index_path = "/#{index_file}" + frontmatter_ignore = false - # Check for file and frontmatter - if sitemap.exists?(request_path) - p = sitemap.page(request_path) - d = p.data - if !d.nil? + # Check for file and frontmatter + d = page.data + if !page.data.nil? frontmatter_ignore = d.has_key?("directory_index") && d["directory_index"] == false end - end - # Only reroute if not ignored - if ignored_directory_indexes.include?(request_path) - false - elsif request_path =~ /#{new_index_path}$/ - false - elsif frontmatter_ignore - false - else - destination.sub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path), + # Only reroute if not ignored + request_path = page.request_path + if ignored_directory_indexes.include? page + destination + elsif request_path.end_with? new_index_path + destination + elsif frontmatter_ignore + destination + else + index_ext = File.extname(index_file) + destination.chomp(File.extname(index_file)) + new_index_path + end end end end - + alias :included :registered end - # Directory indexes instance methods module InstanceMethods # A list of pages which will not use directory indexes - # @return [Array] + # @return [Array] def ignored_directory_indexes @_ignored_directory_indexes ||= [] end @@ -97,7 +98,7 @@ module Middleman::Extensions # @return [void] def page(url, options={}, &block) if options.has_key?(:directory_index) && !options["directory_index"] - ignored_directory_indexes << url + ignored_directory_indexes << sitemap.page(url) else super end diff --git a/middleman-core/lib/middleman-core/sitemap/page.rb b/middleman-core/lib/middleman-core/sitemap/page.rb index 2fc1c9b2..48d20c24 100644 --- a/middleman-core/lib/middleman-core/sitemap/page.rb +++ b/middleman-core/lib/middleman-core/sitemap/page.rb @@ -193,7 +193,7 @@ module Middleman::Sitemap # @return [String] def destination_path store.reroute_callbacks.inject(self.path) do |destination, callback| - callback.call(destination) + callback.call(destination, self) end end