From f8b7f410a6f060ab8af78a3a2d52af540024fe82 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Fri, 22 Apr 2011 11:48:48 -0700 Subject: [PATCH] Make custom page/layout rendering handle all possible directory/index permutations. closes #37 --- features/page_alias_and_layouts.feature | 27 +++++++++++++++++ .../views/custom-layout-dir/index.html.haml | 1 + lib/middleman/server.rb | 29 +++++++++++++------ lib/middleman/version.rb | 2 +- 4 files changed, 49 insertions(+), 10 deletions(-) create mode 100755 fixtures/test-app/views/custom-layout-dir/index.html.haml diff --git a/features/page_alias_and_layouts.feature b/features/page_alias_and_layouts.feature index c092b966..28f5e9c3 100644 --- a/features/page_alias_and_layouts.feature +++ b/features/page_alias_and_layouts.feature @@ -9,4 +9,31 @@ Feature: Custom layouts Scenario: Using with_layout block Given "/custom-layout.html" with_layout block has layout "custom" When I go to "/custom-layout.html" + Then I should see "Custom Layout" + + Scenario: Using custom :layout attribute with folders + Given page "/custom-layout-dir/" has layout "custom" + When I go to "/custom-layout-dir" + Then I should see "Custom Layout" + When I go to "/custom-layout-dir/" + Then I should see "Custom Layout" + When I go to "/custom-layout-dir/index.html" + Then I should see "Custom Layout" + + Scenario: Using custom :layout attribute with folders + Given page "/custom-layout-dir" has layout "custom" + When I go to "/custom-layout-dir" + Then I should see "Custom Layout" + When I go to "/custom-layout-dir/" + Then I should see "Custom Layout" + When I go to "/custom-layout-dir/index.html" + Then I should see "Custom Layout" + + Scenario: Using custom :layout attribute with folders + Given page "/custom-layout-dir/index.html" has layout "custom" + When I go to "/custom-layout-dir" + Then I should see "Custom Layout" + When I go to "/custom-layout-dir/" + Then I should see "Custom Layout" + When I go to "/custom-layout-dir/index.html" Then I should see "Custom Layout" \ No newline at end of file diff --git a/fixtures/test-app/views/custom-layout-dir/index.html.haml b/fixtures/test-app/views/custom-layout-dir/index.html.haml new file mode 100755 index 00000000..0582dd93 --- /dev/null +++ b/fixtures/test-app/views/custom-layout-dir/index.html.haml @@ -0,0 +1 @@ +%h1 Welcome \ No newline at end of file diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index 6c2fe23d..e4a21927 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -101,12 +101,20 @@ module Middleman # page "/about.html", :layout => false # page "/", :layout => :homepage_layout def self.page(url, options={}, &block) - url << settings.index_file if url.match(%r{/$}) + url = url.gsub(%r{#{settings.index_file}$}, "") + url = url.gsub(%r{(\/)$}, "") if url.length > 1 + + paths = [url] + paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1 + paths << "/#{path_to_index(url)}" options[:layout] = current_layout if options[:layout].nil? - get(url) do - return yield if block_given? - process_request(options) + + paths.each do |p| + get(p) do + return yield if block_given? + process_request(options) + end end end @@ -116,15 +124,18 @@ module Middleman end private - # Internal method to look for templates and evaluate them if found - def process_request(options={}) - # Normalize the path and add index if we're looking at a directory - path = request.path + def self.path_to_index(path) parts = path ? path.split('/') : [] if parts.last.nil? || parts.last.split('.').length == 1 path = File.join(path, settings.index_file) end - path.gsub!(%r{^/}, '') + path.gsub(%r{^/}, '') + end + + # Internal method to look for templates and evaluate them if found + def process_request(options={}) + # Normalize the path and add index if we're looking at a directory + path = self.class.path_to_index(request.path) static_path = File.join(Middleman::Server.public, path) # if File.exists? static_path diff --git a/lib/middleman/version.rb b/lib/middleman/version.rb index 0b7930f6..428ed570 100644 --- a/lib/middleman/version.rb +++ b/lib/middleman/version.rb @@ -1,3 +1,3 @@ module Middleman - VERSION = "1.1.5" + VERSION = "1.1.6" end