From e136fab77ca0a1cd204f04bc574878c01618c226 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 4 Feb 2012 23:07:02 -0800 Subject: [PATCH] Use a page_by_destination lookup to make rerouting work both ways, in build and during preview server. --- .../features/wildcard_page_helper.feature | 2 +- middleman-core/lib/middleman-core/base.rb | 12 ++--- .../extensions/directory_indexes.rb | 52 ++----------------- .../lib/middleman-core/sitemap/page.rb | 5 +- .../lib/middleman-core/sitemap/store.rb | 9 +++- 5 files changed, 22 insertions(+), 58 deletions(-) diff --git a/middleman-core/features/wildcard_page_helper.feature b/middleman-core/features/wildcard_page_helper.feature index baa75556..5ac7a25e 100644 --- a/middleman-core/features/wildcard_page_helper.feature +++ b/middleman-core/features/wildcard_page_helper.feature @@ -15,5 +15,5 @@ Feature: Wildcards in Page helper Then I should see "Normal Layout" When I go to "/admin/" Then I should see "Admin Layout" - When I go to "/admin/page.html" + When I go to "/admin/page/" Then I should see "Admin Layout" \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/base.rb b/middleman-core/lib/middleman-core/base.rb index e7269e0c..7ab77383 100644 --- a/middleman-core/lib/middleman-core/base.rb +++ b/middleman-core/lib/middleman-core/base.rb @@ -370,13 +370,13 @@ class Middleman::Base # Run before callbacks run_hook :before - - # Return 404 if not in sitemap - return not_found unless sitemap.exists?(@request_path) - + # Get the page object for this path - sitemap_page = sitemap.page(@request_path) - + sitemap_page = sitemap.page_by_destination(@request_path) + + # Return 404 if not in sitemap + return not_found unless sitemap_page + # Return 404 if this path is specifically ignored return not_found if sitemap_page.ignored? diff --git a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb index 963e89d7..4c591c9d 100644 --- a/middleman-core/lib/middleman-core/extensions/directory_indexes.rb +++ b/middleman-core/lib/middleman-core/extensions/directory_indexes.rb @@ -12,52 +12,8 @@ module Middleman::Extensions # Include methods app.send :include, InstanceMethods - # TODO: unify these by replacing the "before" thing with a - # lookup by destination_path - - # Before requests - app.before do - prefix = @original_path.sub(/\/$/, "") - indexed_path = prefix + "/" + index_file - extensioned_path = prefix + File.extname(index_file) - - is_ignored = false - fm_ignored = false - - # If the sitemap knows about the path - if sitemap.exists?(@original_path) - # Inspect frontmatter - d = sitemap.page(@original_path).data - - # Allow the frontmatter to ignore a directory index - if !d.nil? && d.has_key?("directory_index") && d["directory_index"] == false - fm_ignored = true - else - next - end - else - # Otherwise check this extension for list of ignored indexes - if sitemap.exists?(extensioned_path) - is_ignored = ignored_directory_indexes.include?(sitemap.page(extensioned_path)) - end - end - - # If we're going to remap to a directory index - if !sitemap.exists?(indexed_path) && !is_ignored && !fm_ignored - parts = @original_path.split("/") - last_part = parts.last || '' - last_part_ext = File.extname(last_part) - - # Change the request - if last_part_ext.blank? - # This is a folder, redirect to index - @request_path = extensioned_path - end - end - end - app.after_configuration do - # Basically does the same as above, but in build mode + # Register a reroute transform that turns regular paths into indexed paths sitemap.reroute do |destination, page| new_index_path = "/#{index_file}" frontmatter_ignore = false @@ -71,14 +27,14 @@ module Middleman::Extensions index_ext = File.extname(index_file) # Only reroute if not ignored - request_path = page.request_path + path = page.path if ignored_directory_indexes.include? page destination - elsif request_path == index_file || request_path.end_with?(new_index_path) + elsif path == index_file || path.end_with?(new_index_path) destination elsif frontmatter_ignore destination - elsif index_ext != File.extname(request_path) + elsif index_ext != File.extname(path) destination else destination.chomp(File.extname(index_file)) + new_index_path diff --git a/middleman-core/lib/middleman-core/sitemap/page.rb b/middleman-core/lib/middleman-core/sitemap/page.rb index 48d20c24..b14faca7 100644 --- a/middleman-core/lib/middleman-core/sitemap/page.rb +++ b/middleman-core/lib/middleman-core/sitemap/page.rb @@ -43,9 +43,9 @@ module Middleman::Sitemap # @return [String] def request_path if proxy? - store.page(proxied_to).path + store.page(proxied_to).destination_path else - path + destination_path end end @@ -192,6 +192,7 @@ module Middleman::Sitemap # This path can be affected by proxy callbacks. # @return [String] def destination_path + # TODO: memoize this value store.reroute_callbacks.inject(self.path) do |destination, callback| callback.call(destination, self) end diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index e69cfd3c..b14dac67 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -77,6 +77,13 @@ module Middleman::Sitemap path = normalize_path(path) @pages.fetch(path) { @pages[path] = ::Middleman::Sitemap::Page.new(self, path) } end + + # Find a page given its destination path + def page_by_destination(destination_path) + # TODO: memoize this + destination_path = normalize_path(destination_path) + @pages.values.find {|p| p.destination_path == destination_path } + end # Loop over known pages # @yield [path, page] @@ -86,7 +93,7 @@ module Middleman::Sitemap yield k, v end end - + # Get all known paths # @return [Array] def all_paths