Make custom page/layout rendering handle all possible directory/index permutations. closes #37

This commit is contained in:
Thomas Reynolds 2011-04-22 11:48:48 -07:00
parent f5a009fe44
commit f8b7f410a6
4 changed files with 49 additions and 10 deletions

View file

@ -10,3 +10,30 @@ Feature: Custom layouts
Given "/custom-layout.html" with_layout block has layout "custom" Given "/custom-layout.html" with_layout block has layout "custom"
When I go to "/custom-layout.html" When I go to "/custom-layout.html"
Then I should see "Custom Layout" 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"

View file

@ -0,0 +1 @@
%h1 Welcome

View file

@ -101,12 +101,20 @@ module Middleman
# page "/about.html", :layout => false # page "/about.html", :layout => false
# page "/", :layout => :homepage_layout # page "/", :layout => :homepage_layout
def self.page(url, options={}, &block) 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? options[:layout] = current_layout if options[:layout].nil?
get(url) do
return yield if block_given? paths.each do |p|
process_request(options) get(p) do
return yield if block_given?
process_request(options)
end
end end
end end
@ -116,15 +124,18 @@ module Middleman
end end
private private
# Internal method to look for templates and evaluate them if found def self.path_to_index(path)
def process_request(options={})
# Normalize the path and add index if we're looking at a directory
path = request.path
parts = path ? path.split('/') : [] parts = path ? path.split('/') : []
if parts.last.nil? || parts.last.split('.').length == 1 if parts.last.nil? || parts.last.split('.').length == 1
path = File.join(path, settings.index_file) path = File.join(path, settings.index_file)
end 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) static_path = File.join(Middleman::Server.public, path)
# if File.exists? static_path # if File.exists? static_path

View file

@ -1,3 +1,3 @@
module Middleman module Middleman
VERSION = "1.1.5" VERSION = "1.1.6"
end end