From 692aa10c8b908e69b51a307b30447237bcaef827 Mon Sep 17 00:00:00 2001 From: Tim Bates Date: Thu, 12 Jul 2012 12:03:33 +0930 Subject: [PATCH 1/4] Add trailing_slash option for prettier urls "set :trailing_slash, false" will cause resource urls that match the index_file to have the trailing slash stripped off the directory URL, e.g. instead of "/dir/index.html" becoming "/dir/" it will be "/dir" --- middleman-core/lib/middleman-core/application.rb | 4 ++++ middleman-core/lib/middleman-core/sitemap/resource.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 093edc13..7a12cbfa 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -93,6 +93,10 @@ module Middleman # @return [String] set :index_file, "index.html" + # Whether to include a trailing slash on links to directory indexes + # @return [Boolean] + set :trailing_slash, true + # Location of javascripts within source. # @return [String] set :js_dir, "javascripts" diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 7d6975d3..481212f6 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -142,7 +142,7 @@ module Middleman # @return [String] def url File.join(app.respond_to?(:http_prefix) ? app.http_prefix : '/', - destination_path.sub(/\/#{Regexp.escape(app.index_file)}$/, '/')) + destination_path.sub(/\/#{Regexp.escape(app.index_file)}$/, app.trailing_slash ? '/' : '')) end end end From 2117cbac792d7fc4e8e416c7f8bad8d0951ac048 Mon Sep 17 00:00:00 2001 From: Tim Bates Date: Thu, 12 Jul 2012 15:02:48 +0930 Subject: [PATCH 2/4] Make directory_indexes check the destination_path If the destination_path matches the index_file but the original path does not (as can happen with proxied resources) then directory_indexes will add the index file a second time, e.g. resource.path = "myres.html" and resource.destination_path = "myres/index.html" then directory_indexes will convert this to resource.destination_path = "myres/index/index.html". This patch fixes that by making directory_indexes check the destination_path when determining whether to reroute. --- .../lib/middleman-more/extensions/directory_indexes.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/middleman-more/lib/middleman-more/extensions/directory_indexes.rb b/middleman-more/lib/middleman-more/extensions/directory_indexes.rb index 373df9ec..15d82428 100644 --- a/middleman-more/lib/middleman-more/extensions/directory_indexes.rb +++ b/middleman-more/lib/middleman-more/extensions/directory_indexes.rb @@ -34,8 +34,8 @@ module Middleman resources.each do |resource| # Check if it would be pointless to reroute - next if resource.path == index_file || - resource.path.end_with?(new_index_path) || + next if resource.destination_path == index_file || + resource.destination_path.end_with?(new_index_path) || File.extname(index_file) != resource.ext # Check if frontmatter turns directory_index off @@ -53,4 +53,4 @@ module Middleman end end end -end \ No newline at end of file +end From 3cbda0ee360d2676c35379306ac7e8f1fee58142 Mon Sep 17 00:00:00 2001 From: Tim Bates Date: Fri, 13 Jul 2012 10:57:05 +0930 Subject: [PATCH 3/4] Added strip_index_file option for configuring urls --- middleman-core/lib/middleman-core/application.rb | 6 +++++- middleman-core/lib/middleman-core/sitemap/resource.rb | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index 7a12cbfa..dcf9bf1e 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -93,7 +93,11 @@ module Middleman # @return [String] set :index_file, "index.html" - # Whether to include a trailing slash on links to directory indexes + # Whether to strip the index file name off links to directory indexes + # @return [Boolean] + set :strip_index_file, true + + # Whether to include a trailing slash when stripping the index file # @return [Boolean] set :trailing_slash, true diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 481212f6..da8e8f84 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -141,8 +141,12 @@ module Middleman # just foo. Best for linking. # @return [String] def url - File.join(app.respond_to?(:http_prefix) ? app.http_prefix : '/', - destination_path.sub(/\/#{Regexp.escape(app.index_file)}$/, app.trailing_slash ? '/' : '')) + url_path = destination_path + if app.strip_index_file + url_path = url_path.sub(/\/#{Regexp.escape(app.index_file)}$/, + app.trailing_slash ? '/' : '') + end + File.join(app.respond_to?(:http_prefix) ? app.http_prefix : '/', url_path) end end end From 0fa1bfe675636330cad372b736ae26bb59c2ec98 Mon Sep 17 00:00:00 2001 From: Tim Bates Date: Fri, 13 Jul 2012 16:00:53 +0930 Subject: [PATCH 4/4] Tests and a small bug fix to make them pass --- middleman-core/features/strip_url.feature | 40 +++++++++++++++++++ .../fixtures/strip-url-app/config.rb | 0 .../strip-url-app/source/index.html.erb | 1 + .../strip-url-app/source/other.html.erb | 1 + .../source/subdir/index.html.erb | 1 + .../lib/middleman-core/sitemap/resource.rb | 2 +- 6 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 middleman-core/features/strip_url.feature create mode 100644 middleman-core/fixtures/strip-url-app/config.rb create mode 100755 middleman-core/fixtures/strip-url-app/source/index.html.erb create mode 100755 middleman-core/fixtures/strip-url-app/source/other.html.erb create mode 100755 middleman-core/fixtures/strip-url-app/source/subdir/index.html.erb diff --git a/middleman-core/features/strip_url.feature b/middleman-core/features/strip_url.feature new file mode 100644 index 00000000..8a332d04 --- /dev/null +++ b/middleman-core/features/strip_url.feature @@ -0,0 +1,40 @@ +Feature: Strip the index_file from urls + + Scenario: Default behaviour, strip with trailing slash + Given the Server is running at "strip-url-app" + When I go to "/" + Then I should see "URL: '/'" + When I go to "/index.html" + Then I should see "URL: '/'" + When I go to "/other.html" + Then I should see "URL: '/other.html'" + When I go to "/subdir/index.html" + Then I should see "URL: '/subdir/'" + + Scenario: Trailing slash off + Given a fixture app "strip-url-app" + And a file named "config.rb" with: + """ + set :trailing_slash, false + """ + And the Server is running + When I go to "/" + Then I should see "URL: '/'" + When I go to "/other.html" + Then I should see "URL: '/other.html'" + When I go to "/subdir/index.html" + Then I should see "URL: '/subdir'" + + Scenario: Strip index off + Given a fixture app "strip-url-app" + And a file named "config.rb" with: + """ + set :strip_index_file, false + """ + And the Server is running + When I go to "/" + Then I should see "URL: '/index.html'" + When I go to "/other.html" + Then I should see "URL: '/other.html'" + When I go to "/subdir/index.html" + Then I should see "URL: '/subdir/index.html'" diff --git a/middleman-core/fixtures/strip-url-app/config.rb b/middleman-core/fixtures/strip-url-app/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/strip-url-app/source/index.html.erb b/middleman-core/fixtures/strip-url-app/source/index.html.erb new file mode 100755 index 00000000..f0fea13c --- /dev/null +++ b/middleman-core/fixtures/strip-url-app/source/index.html.erb @@ -0,0 +1 @@ +URL: '<%= current_page.url %>' diff --git a/middleman-core/fixtures/strip-url-app/source/other.html.erb b/middleman-core/fixtures/strip-url-app/source/other.html.erb new file mode 100755 index 00000000..f0fea13c --- /dev/null +++ b/middleman-core/fixtures/strip-url-app/source/other.html.erb @@ -0,0 +1 @@ +URL: '<%= current_page.url %>' diff --git a/middleman-core/fixtures/strip-url-app/source/subdir/index.html.erb b/middleman-core/fixtures/strip-url-app/source/subdir/index.html.erb new file mode 100755 index 00000000..f0fea13c --- /dev/null +++ b/middleman-core/fixtures/strip-url-app/source/subdir/index.html.erb @@ -0,0 +1 @@ +URL: '<%= current_page.url %>' diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index da8e8f84..28f86e10 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -143,7 +143,7 @@ module Middleman def url url_path = destination_path if app.strip_index_file - url_path = url_path.sub(/\/#{Regexp.escape(app.index_file)}$/, + url_path = url_path.sub(/(^|\/)#{Regexp.escape(app.index_file)}$/, app.trailing_slash ? '/' : '') end File.join(app.respond_to?(:http_prefix) ? app.http_prefix : '/', url_path)