1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

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
Given the Server is running
When I go to "/page-class.html"
Then I should see "page-class"
When I go to "/page-classes.html"
Then I should see "page-classes"
Scenario: Viewing a tier-1 path
Given the Server is running
When I go to "/sub1/page-class.html"
Then I should see "sub1 sub1_page-class"
When I go to "/sub1/page-classes.html"
Then I should see "sub1 sub1_page-classes"
Scenario: Viewing a tier-2 path
Given the Server is running
When I go to "/sub1/sub2/page-class.html"
Then I should see "sub1 sub1_sub2 sub1_sub2_page-class"
When I go to "/sub1/sub2/page-classes.html"
Then I should see "sub1 sub1_sub2 sub1_sub2_page-classes"

View file

@ -4,19 +4,9 @@ with_layout false do
page "/inline-coffeescript.html"
page "/slim.html"
page "/data.html"
end
get "/page-class.html" do
haml :"page-classes.html", :layout => false
end
get "/sub1/page-class.html" do
haml :"page-classes.html", :layout => false
end
get "/sub1/sub2/page-class.html" do
haml :"page-classes.html", :layout => false
end
page "/page-classes.html"
page "/sub1/page-classes.html"
page "/sub1/sub2/page-classes.html"
%w{
/auto-css.html
@ -25,9 +15,7 @@ end
/auto-css/auto-css.html
/auto-css/sub/auto-css.html
}.each do |path|
get path do
haml :"auto-css.html", :layout => false
end
page path
end
%w{
@ -37,8 +25,6 @@ end
/auto-js/auto-js.html
/auto-js/sub/auto-js.html
}.each do |path|
get path do
haml :"auto-js.html", :layout => false
page path
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
class << self
def registered(app)
# Disable Padrino cache buster until explicitly enabled
app.set :asset_stamp, false
app.extend ClassMethods
app.helpers Helpers

View file

@ -10,6 +10,21 @@ module Middleman::CoreExtensions::FrontMatter
::Tilt::register RedClothTemplate, 'textile'
::Tilt::register ERBTemplate, 'erb', 'rhtml'
::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
alias :included :registered
end

View file

@ -2,6 +2,11 @@ module Middleman::CoreExtensions::Routing
class << self
def registered(app)
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
alias :included :registered
end
@ -11,6 +16,14 @@ module Middleman::CoreExtensions::Routing
@layout
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
# with_layout :admin do
# page "/admin/"
@ -34,7 +47,7 @@ module Middleman::CoreExtensions::Routing
paths = [url]
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?

View file

@ -23,11 +23,10 @@ module Middleman
set :build_dir, "build" # Which folder are builds output to
set :http_prefix, nil # During build, add a prefix for absolute paths
# Pass all request to Middleman, even "static" files
set :static, false
set :views, "source"
# Disable Padrino cache buster until explicitly enabled
set :asset_stamp, false
set :views, "source"
# Activate custom features
register Middleman::CoreExtensions::Features
@ -72,66 +71,44 @@ module Middleman
process_request
end
private
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)
# See if Tilt cannot handle this file
before do
result = resolve_template(request.path_info, :raise_exceptions => false)
if result
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
path.gsub(%r{^/}, '')
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 = 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
settings.layout(options[:layout]) if !options[:layout].nil?
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_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)
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
return result
end
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