use before hooks to simplify server and move other code into features and extensions

This commit is contained in:
Thomas Reynolds 2011-07-06 22:41:12 -07:00
parent efe7332617
commit 7ffd5ccea9
14 changed files with 90 additions and 88 deletions

View file

@ -3,15 +3,15 @@ Feature: Built-in page_classes view helper
Scenario: Viewing the root path Scenario: Viewing the root path
Given the Server is running Given the Server is running
When I go to "/page-class.html" When I go to "/page-classes.html"
Then I should see "page-class" Then I should see "page-classes"
Scenario: Viewing a tier-1 path Scenario: Viewing a tier-1 path
Given the Server is running Given the Server is running
When I go to "/sub1/page-class.html" When I go to "/sub1/page-classes.html"
Then I should see "sub1 sub1_page-class" Then I should see "sub1 sub1_page-classes"
Scenario: Viewing a tier-2 path Scenario: Viewing a tier-2 path
Given the Server is running Given the Server is running
When I go to "/sub1/sub2/page-class.html" When I go to "/sub1/sub2/page-classes.html"
Then I should see "sub1 sub1_sub2 sub1_sub2_page-class" Then I should see "sub1 sub1_sub2 sub1_sub2_page-classes"

View file

@ -4,41 +4,27 @@ with_layout false do
page "/inline-coffeescript.html" page "/inline-coffeescript.html"
page "/slim.html" page "/slim.html"
page "/data.html" page "/data.html"
end page "/page-classes.html"
page "/sub1/page-classes.html"
page "/sub1/sub2/page-classes.html"
get "/page-class.html" do %w{
haml :"page-classes.html", :layout => false /auto-css.html
end /auto-css
/auto-css/
/auto-css/auto-css.html
/auto-css/sub/auto-css.html
}.each do |path|
page path
end
get "/sub1/page-class.html" do %w{
haml :"page-classes.html", :layout => false /auto-js.html
/auto-js
/auto-js/
/auto-js/auto-js.html
/auto-js/sub/auto-js.html
}.each do |path|
page path
end
end end
get "/sub1/sub2/page-class.html" do
haml :"page-classes.html", :layout => false
end
%w{
/auto-css.html
/auto-css
/auto-css/
/auto-css/auto-css.html
/auto-css/sub/auto-css.html
}.each do |path|
get path do
haml :"auto-css.html", :layout => false
end
end
%w{
/auto-js.html
/auto-js
/auto-js/
/auto-js/auto-js.html
/auto-js/sub/auto-js.html
}.each do |path|
get path do
haml :"auto-js.html", :layout => false
end
end

View file

@ -0,0 +1 @@
= auto_stylesheet_link_tag

View file

@ -0,0 +1 @@
= auto_stylesheet_link_tag

View file

@ -0,0 +1 @@
= auto_stylesheet_link_tag

View file

@ -0,0 +1 @@
= auto_javascript_include_tag

View file

@ -0,0 +1 @@
= auto_javascript_include_tag

View file

@ -0,0 +1 @@
= auto_javascript_include_tag

View file

@ -0,0 +1 @@
= page_classes

View file

@ -0,0 +1 @@
= page_classes

View file

@ -1,6 +1,9 @@
module Middleman::CoreExtensions::Assets module Middleman::CoreExtensions::Assets
class << self class << self
def registered(app) def registered(app)
# Disable Padrino cache buster until explicitly enabled
app.set :asset_stamp, false
app.extend ClassMethods app.extend ClassMethods
app.helpers Helpers app.helpers Helpers

View file

@ -10,6 +10,21 @@ module Middleman::CoreExtensions::FrontMatter
::Tilt::register RedClothTemplate, 'textile' ::Tilt::register RedClothTemplate, 'textile'
::Tilt::register ERBTemplate, 'erb', 'rhtml' ::Tilt::register ERBTemplate, 'erb', 'rhtml'
::Tilt::register ErubisTemplate, 'erb', 'rhtml', 'erubis' ::Tilt::register ErubisTemplate, 'erb', 'rhtml', 'erubis'
#
# app.before do
# full_file_path = "#{extensionless_path}.#{template_engine}"
# system_path = File.join(settings.views, full_file_path)
# data, content = self.class.parse_front_matter(File.read(system_path))
#
# %w(layout layout_engine).each do |opt|
# if data.has_key?(opt)
# options[opt.to_sym] = data.delete(opt)
# end
# end
#
# # Forward remaining data to helpers
# self.class.data_content("page", data)
# end
end end
alias :included :registered alias :included :registered
end end

View file

@ -2,6 +2,11 @@ module Middleman::CoreExtensions::Routing
class << self class << self
def registered(app) def registered(app)
app.extend ClassMethods app.extend ClassMethods
# Normalize the path and add index if we're looking at a directory
app.before do
request.path_info = self.class.path_to_index(request.path)
end
end end
alias :included :registered alias :included :registered
end end
@ -11,6 +16,14 @@ module Middleman::CoreExtensions::Routing
@layout @layout
end end
def 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{^/}, '')
end
# Takes a block which allows many pages to have the same layout # Takes a block which allows many pages to have the same layout
# with_layout :admin do # with_layout :admin do
# page "/admin/" # page "/admin/"
@ -34,7 +47,7 @@ module Middleman::CoreExtensions::Routing
paths = [url] paths = [url]
paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1 paths << "#{url}/" if url.length > 1 && url.split("/").last.split('.').length <= 1
paths << "/#{path_to_index(url)}" paths << "#{path_to_index(url)}"
options[:layout] = current_layout if options[:layout].nil? options[:layout] = current_layout if options[:layout].nil?

View file

@ -23,11 +23,10 @@ module Middleman
set :build_dir, "build" # Which folder are builds output to set :build_dir, "build" # Which folder are builds output to
set :http_prefix, nil # During build, add a prefix for absolute paths set :http_prefix, nil # During build, add a prefix for absolute paths
# Pass all request to Middleman, even "static" files
set :static, false set :static, false
set :views, "source"
# Disable Padrino cache buster until explicitly enabled set :views, "source"
set :asset_stamp, false
# Activate custom features # Activate custom features
register Middleman::CoreExtensions::Features register Middleman::CoreExtensions::Features
@ -72,66 +71,44 @@ module Middleman
process_request process_request
end end
private # See if Tilt cannot handle this file
def self.path_to_index(path) before do
parts = path ? path.split('/') : [] result = resolve_template(request.path_info, :raise_exceptions => false)
if parts.last.nil? || parts.last.split('.').length == 1 if result
path = File.join(path, settings.index_file) extensionless_path, template_engine = result
# Return static files
if !::Tilt.mappings.has_key?(template_engine.to_s)
content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
status 200
send_file File.join(Middleman::Server.views, request.path_info)
end
else
$stderr.puts "File not found: #{request.path_info}"
status 404
end end
path.gsub(%r{^/}, '')
end end
private
# Internal method to look for templates and evaluate them if found # Internal method to look for templates and evaluate them if found
def process_request(options={}) 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)
extensionless_path, template_engine = resolve_template(path)
if !::Tilt.mappings.has_key?(template_engine.to_s)
content_type mime_type(File.extname(path)), :charset => 'utf-8'
status 200
send_file File.join(Middleman::Server.views, path)
return
end
full_file_path = "#{extensionless_path}.#{template_engine}"
system_path = File.join(settings.views, full_file_path)
data, content = self.class.parse_front_matter(File.read(system_path))
%w(layout layout_engine).each do |opt|
if data.has_key?(opt)
options[opt.to_sym] = data.delete(opt)
end
end
# Forward remaining data to helpers
self.class.data_content("page", data)
old_layout = settings.current_layout old_layout = settings.current_layout
settings.layout(options[:layout]) if !options[:layout].nil? settings.layout(options[:layout]) if !options[:layout].nil?
layout = settings.fetch_layout_path.to_sym layout = settings.fetch_layout_path.to_sym
layout = false if options[:layout] == false or path =~ /\.(css|js)$/ layout = false if options[:layout] == false or request.path_info =~ /\.(css|js)$/
render_options = { :layout => layout } render_options = { :layout => layout }
render_options[:layout_engine] = options[:layout_engine] if options.has_key? :layout_engine render_options[:layout_engine] = options[:layout_engine] if options.has_key? :layout_engine
result = render(path, render_options) result = render(request.path_info, render_options)
settings.layout(old_layout) settings.layout(old_layout)
if result if result
content_type mime_type(File.extname(path)), :charset => 'utf-8' content_type mime_type(File.extname(request.path_info)), :charset => 'utf-8'
status 200 status 200
return result return result
end end
status 404 status 404
rescue Padrino::Rendering::TemplateNotFound
if settings.environment == :development
raise $!
else
$stderr.puts "File not found: #{request.path}"
status 404
end
end end
end end
end end