rearchitect some callbacks, add liquid partial support. closes #115

This commit is contained in:
Thomas Reynolds 2011-09-08 23:06:22 -07:00
parent 78e7a118e5
commit 68f2527923
18 changed files with 104 additions and 57 deletions

6
features/liquid.feature Normal file
View file

@ -0,0 +1,6 @@
Feature: Support liquid partials
Scenario: Rendering liquid
Given the Server is running at "test-app"
When I go to "/liquid_master.html"
Then I should see "Greetings"

View file

@ -1,5 +1,6 @@
Given /^I am using an asset host$/ do Given /^I am using an asset host$/ do
@server ||= Middleman.server @server ||= Middleman.server
@server.set :show_exceptions, false
@server.activate :asset_host @server.activate :asset_host
@server.set :asset_host do |asset| @server.set :asset_host do |asset|
"http://assets%d.example.com" % (asset.hash % 4) "http://assets%d.example.com" % (asset.hash % 4)

View file

@ -1,5 +1,6 @@
Given /^"([^\"]*)" feature is "([^\"]*)"$/ do |feature, state| Given /^"([^\"]*)" feature is "([^\"]*)"$/ do |feature, state|
@server ||= Middleman.server @server ||= Middleman.server
@server.set :show_exceptions, false
if state == "enabled" if state == "enabled"
@server.activate(feature.to_sym) @server.activate(feature.to_sym)
end end
@ -8,6 +9,7 @@ end
Given /^"([^\"]*)" is set to "([^\"]*)"$/ do |variable, value| Given /^"([^\"]*)" is set to "([^\"]*)"$/ do |variable, value|
@server ||= Middleman.server @server ||= Middleman.server
@server.set :show_exceptions, false
@server.set variable.to_sym, value @server.set variable.to_sym, value
end end
@ -17,6 +19,7 @@ end
Given /^the Server is running at "([^\"]*)"$/ do |app_path| Given /^the Server is running at "([^\"]*)"$/ do |app_path|
@server ||= Middleman.server @server ||= Middleman.server
@server.set :show_exceptions, false
root = File.dirname(File.dirname(File.dirname(__FILE__))) root = File.dirname(File.dirname(File.dirname(__FILE__)))
@server.set :root, File.join(root, "fixtures", app_path) @server.set :root, File.join(root, "fixtures", app_path)
@browser = Rack::Test::Session.new(Rack::MockSession.new(@server.new)) @browser = Rack::Test::Session.new(Rack::MockSession.new(@server.new))

View file

@ -1,10 +1,12 @@
Given /^page "([^\"]*)" has layout "([^\"]*)"$/ do |url, layout| Given /^page "([^\"]*)" has layout "([^\"]*)"$/ do |url, layout|
@server ||= Middleman.server @server ||= Middleman.server
@server.set :show_exceptions, false
@server.page(url, :layout => layout.to_sym) @server.page(url, :layout => layout.to_sym)
end end
Given /^"([^\"]*)" with_layout block has layout "([^\"]*)"$/ do |url, layout| Given /^"([^\"]*)" with_layout block has layout "([^\"]*)"$/ do |url, layout|
@server ||= Middleman.server @server ||= Middleman.server
@server.set :show_exceptions, false
@server.with_layout(layout.to_sym) do @server.with_layout(layout.to_sym) do
page(url) page(url)
end end

View file

@ -20,6 +20,7 @@ with_layout false do
page "/slim.html" page "/slim.html"
page "/data.html" page "/data.html"
page "/data2.html" page "/data2.html"
page "/liquid_master.html"
page "/page-classes.html" page "/page-classes.html"
page "/sub1/page-classes.html" page "/sub1/page-classes.html"
page "/sub1/sub2/page-classes.html" page "/sub1/sub2/page-classes.html"

View file

@ -0,0 +1 @@
Greetings

View file

@ -1 +1,2 @@
{{data}}
{% for item in data.test %}{{item.title}}{% endfor %} {% for item in data.test %}{{item.title}}{% endfor %}

View file

@ -0,0 +1 @@
{% include "liquid_partial" %}

View file

@ -69,11 +69,12 @@ module Middleman
# Custom Renderers # Custom Renderers
module Renderers module Renderers
autoload :Haml, "middleman/renderers/haml" autoload :Haml, "middleman/renderers/haml"
autoload :Sass, "middleman/renderers/sass" autoload :Sass, "middleman/renderers/sass"
autoload :Slim, "middleman/renderers/slim" autoload :Slim, "middleman/renderers/slim"
autoload :Markdown, "middleman/renderers/markdown" autoload :Markdown, "middleman/renderers/markdown"
autoload :CoffeeScript, "middleman/renderers/coffee_script" autoload :CoffeeScript, "middleman/renderers/coffee_script"
autoload :Liquid, "middleman/renderers/liquid"
end end
module CoreExtensions module CoreExtensions

View file

@ -1,11 +1,12 @@
require "i18n"
require "active_support"
require "active_support/json"
require "active_support/core_ext/class/attribute_accessors"
module Middleman::Base module Middleman::Base
class << self class << self
def registered(app) def registered(app)
# Explicitly require json support
require "i18n"
require "active_support"
require "active_support/json"
app.extend ClassMethods app.extend ClassMethods
app.send :include, InstanceMethods app.send :include, InstanceMethods
@ -43,6 +44,9 @@ module Middleman::Base
# Activate custom features # Activate custom features
app.register Middleman::CoreExtensions::Features app.register Middleman::CoreExtensions::Features
# Activate Yaml Data package
app.register Middleman::CoreExtensions::Data
# Setup custom rendering # Setup custom rendering
app.register Middleman::CoreExtensions::Rendering app.register Middleman::CoreExtensions::Rendering
@ -58,9 +62,6 @@ module Middleman::Base
# Activate built-in helpers # Activate built-in helpers
app.register Middleman::CoreExtensions::DefaultHelpers app.register Middleman::CoreExtensions::DefaultHelpers
# Activate Yaml Data package
app.register Middleman::CoreExtensions::Data
# with_layout and page routing # with_layout and page routing
app.register Middleman::CoreExtensions::Routing app.register Middleman::CoreExtensions::Routing
@ -86,14 +87,8 @@ module Middleman::Base
end end
# See if Tilt cannot handle this file # See if Tilt cannot handle this file
app.before_processing do app.before_processing(:base) do |result|
if !settings.views.include?(settings.root)
settings.set :views, File.join(settings.root, settings.views)
end
request_path = request.path_info.gsub("%20", " ") request_path = request.path_info.gsub("%20", " ")
result = resolve_template(request_path, :raise_exceptions => false)
should_be_ignored = !(request["is_proxy"]) && settings.excluded_paths.include?("/#{request_path}") should_be_ignored = !(request["is_proxy"]) && settings.excluded_paths.include?("/#{request_path}")
if result && !should_be_ignored if result && !should_be_ignored
@ -132,16 +127,16 @@ module Middleman::Base
super(option, value, &nil) super(option, value, &nil)
end end
def before_processing(&block) def before_processing(name=:unnamed, idx=-1, &block)
@before_processes ||= [] @before_processes ||= []
@before_processes << block @before_processes.insert(idx, [name, block])
end end
def execute_before_processing!(inst) def execute_before_processing!(inst, resolved_template)
@before_processes ||= [] @before_processes ||= []
@before_processes.all? do |block| @before_processes.all? do |name, block|
inst.instance_eval(&block) inst.instance_exec(resolved_template, &block)
end end
end end
@ -152,14 +147,24 @@ module Middleman::Base
module InstanceMethods module InstanceMethods
# 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={})
return unless settings.execute_before_processing!(self) if !settings.views.include?(settings.root)
settings.set :views, File.join(settings.root, settings.views)
end
# Normalize the path and add index if we're looking at a directory
request.path_info = self.class.path_to_index(request.path)
request_path = request.path_info.gsub("%20", " ")
found_template = resolve_template(request_path, :raise_exceptions => false)
return status(404) unless found_template
return unless settings.execute_before_processing!(self, found_template)
options.merge!(request['custom_options'] || {}) options.merge!(request['custom_options'] || {})
old_layout = settings.layout old_layout = settings.layout
settings.set :layout, options[:layout] if !options[:layout].nil? settings.set :layout, options[:layout] if !options[:layout].nil?
layout = if settings.layout local_layout = if settings.layout
if options[:layout] == false || request.path_info =~ /\.(css|js)$/ if options[:layout] == false || request.path_info =~ /\.(css|js)$/
false false
else else
@ -169,23 +174,24 @@ module Middleman::Base
false false
end end
render_options = { :layout => layout } render_options = { :layout => local_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
request_path = request.path_info.gsub("%20", " ")
path, engine = resolve_template(request_path)
locals = {} path, engine = found_template
locals[:data] = data.to_h if engine == :liquid locals = request['custom_locals'] || {}
result = render(engine, path, render_options, locals) begin
settings.set :layout, old_layout result = render(engine, path, render_options, locals)
if result if result
content_type mime_type(File.extname(request_path)), :charset => 'utf-8' content_type mime_type(File.extname(request_path)), :charset => 'utf-8'
status 200 status 200
body result body result
else end
status 404 # rescue
# status(404)
ensure
settings.set :layout, old_layout
end end
end end
end end

View file

@ -17,7 +17,7 @@ module Middleman
request_path = destination.sub(/^#{SHARED_SERVER.build_dir}/, "") request_path = destination.sub(/^#{SHARED_SERVER.build_dir}/, "")
begin begin
destination, request_page = SHARED_SERVER.reroute_builder(destination, request_path) destination, request_path = SHARED_SERVER.reroute_builder(destination, request_path)
request_path.gsub!(/\s/, "%20") request_path.gsub!(/\s/, "%20")
response = Middleman::Builder.shared_rack.get(request_path) response = Middleman::Builder.shared_rack.get(request_path)

View file

@ -53,6 +53,9 @@ module Middleman::CoreExtensions::Data
def to_h def to_h
data = {} data = {}
@@local_sources ||= {}
@@callback_sources ||= {}
(@@local_sources || {}).each do |k, v| (@@local_sources || {}).each do |k, v|
data[k] = data_for_path(k) data[k] = data_for_path(k)
end end

View file

@ -28,10 +28,7 @@ module Middleman::CoreExtensions::FrontMatter
::Tilt.prefer(HamlTemplate) ::Tilt.prefer(HamlTemplate)
app.after_configuration do app.after_configuration do
app.before_processing do app.before_processing(:front_matter) do |result|
request_path = request.path_info.gsub("%20", " ")
result = resolve_template(request_path, :raise_exceptions => false)
if result && Tilt.mappings.has_key?(result[1].to_s) if result && Tilt.mappings.has_key?(result[1].to_s)
extensionless_path, template_engine = result extensionless_path, template_engine = result
full_file_path = "#{extensionless_path}.#{template_engine}" full_file_path = "#{extensionless_path}.#{template_engine}"

View file

@ -12,6 +12,7 @@ module Middleman::CoreExtensions::Rendering
app.register Middleman::Renderers::Sass app.register Middleman::Renderers::Sass
app.register Middleman::Renderers::Markdown app.register Middleman::Renderers::Markdown
app.register Middleman::Renderers::CoffeeScript app.register Middleman::Renderers::CoffeeScript
app.register Middleman::Renderers::Liquid
end end
alias :included :registered alias :included :registered
end end

View file

@ -9,12 +9,6 @@ module Middleman::CoreExtensions::Routing
app.build_reroute do |destination, request_path| app.build_reroute do |destination, request_path|
throw if app.settings.excluded_paths.include?(request_path) throw if app.settings.excluded_paths.include?(request_path)
end end
# Normalize the path and add index if we're looking at a directory
app.before_processing do
request.path_info = self.class.path_to_index(request.path)
true
end
end end
alias :included :registered alias :included :registered
end end

View file

@ -17,7 +17,7 @@ module Middleman::Features::DirectoryIndexes
else else
[ [
destination.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path), destination.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path),
request_path.gsub(/#{index_ext.gsub(".", "\\.")}$/, new_index_path) request_path
] ]
end end
end end

View file

@ -0,0 +1,29 @@
module Middleman::Renderers::Liquid
class << self
def registered(app)
# Liquid is not included in the default gems,
# but we'll support it if necessary.
begin
require "liquid"
app.after_configuration do
full_path = app.views
full_path = File.join(app.root, app.views) unless app.views.include?(app.root)
Liquid::Template.file_system = Liquid::LocalFileSystem.new(full_path)
app.before_processing(:liquid) do |result|
if result && result[1] == :liquid
request['custom_locals'] ||= {}
request['custom_locals'][:data] = data.to_h
end
true
end
end
rescue LoadError
end
end
alias :included :registered
end
end