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
@server ||= Middleman.server
@server.set :show_exceptions, false
@server.activate :asset_host
@server.set :asset_host do |asset|
"http://assets%d.example.com" % (asset.hash % 4)

View file

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

View file

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

View file

@ -20,6 +20,7 @@ with_layout false do
page "/slim.html"
page "/data.html"
page "/data2.html"
page "/liquid_master.html"
page "/page-classes.html"
page "/sub1/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 %}

View file

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

View file

@ -69,11 +69,12 @@ module Middleman
# Custom Renderers
module Renderers
autoload :Haml, "middleman/renderers/haml"
autoload :Sass, "middleman/renderers/sass"
autoload :Slim, "middleman/renderers/slim"
autoload :Markdown, "middleman/renderers/markdown"
autoload :Haml, "middleman/renderers/haml"
autoload :Sass, "middleman/renderers/sass"
autoload :Slim, "middleman/renderers/slim"
autoload :Markdown, "middleman/renderers/markdown"
autoload :CoffeeScript, "middleman/renderers/coffee_script"
autoload :Liquid, "middleman/renderers/liquid"
end
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
class << self
def registered(app)
# Explicitly require json support
require "i18n"
require "active_support"
require "active_support/json"
app.extend ClassMethods
app.send :include, InstanceMethods
@ -43,6 +44,9 @@ module Middleman::Base
# Activate custom features
app.register Middleman::CoreExtensions::Features
# Activate Yaml Data package
app.register Middleman::CoreExtensions::Data
# Setup custom rendering
app.register Middleman::CoreExtensions::Rendering
@ -58,9 +62,6 @@ module Middleman::Base
# Activate built-in helpers
app.register Middleman::CoreExtensions::DefaultHelpers
# Activate Yaml Data package
app.register Middleman::CoreExtensions::Data
# with_layout and page routing
app.register Middleman::CoreExtensions::Routing
@ -86,14 +87,8 @@ module Middleman::Base
end
# See if Tilt cannot handle this file
app.before_processing do
if !settings.views.include?(settings.root)
settings.set :views, File.join(settings.root, settings.views)
end
app.before_processing(:base) do |result|
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}")
if result && !should_be_ignored
@ -132,16 +127,16 @@ module Middleman::Base
super(option, value, &nil)
end
def before_processing(&block)
def before_processing(name=:unnamed, idx=-1, &block)
@before_processes ||= []
@before_processes << block
@before_processes.insert(idx, [name, block])
end
def execute_before_processing!(inst)
def execute_before_processing!(inst, resolved_template)
@before_processes ||= []
@before_processes.all? do |block|
inst.instance_eval(&block)
@before_processes.all? do |name, block|
inst.instance_exec(resolved_template, &block)
end
end
@ -152,14 +147,24 @@ module Middleman::Base
module InstanceMethods
# Internal method to look for templates and evaluate them if found
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'] || {})
old_layout = settings.layout
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)$/
false
else
@ -169,23 +174,24 @@ module Middleman::Base
false
end
render_options = { :layout => layout }
render_options = { :layout => local_layout }
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 = {}
locals[:data] = data.to_h if engine == :liquid
path, engine = found_template
locals = request['custom_locals'] || {}
result = render(engine, path, render_options, locals)
settings.set :layout, old_layout
begin
result = render(engine, path, render_options, locals)
if result
content_type mime_type(File.extname(request_path)), :charset => 'utf-8'
status 200
body result
else
status 404
if result
content_type mime_type(File.extname(request_path)), :charset => 'utf-8'
status 200
body result
end
# rescue
# status(404)
ensure
settings.set :layout, old_layout
end
end
end

View file

@ -17,7 +17,7 @@ module Middleman
request_path = destination.sub(/^#{SHARED_SERVER.build_dir}/, "")
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")
response = Middleman::Builder.shared_rack.get(request_path)

View file

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

View file

@ -28,10 +28,7 @@ module Middleman::CoreExtensions::FrontMatter
::Tilt.prefer(HamlTemplate)
app.after_configuration do
app.before_processing do
request_path = request.path_info.gsub("%20", " ")
result = resolve_template(request_path, :raise_exceptions => false)
app.before_processing(:front_matter) do |result|
if result && Tilt.mappings.has_key?(result[1].to_s)
extensionless_path, template_engine = result
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::Markdown
app.register Middleman::Renderers::CoffeeScript
app.register Middleman::Renderers::Liquid
end
alias :included :registered
end

View file

@ -9,12 +9,6 @@ module Middleman::CoreExtensions::Routing
app.build_reroute do |destination, request_path|
throw if app.settings.excluded_paths.include?(request_path)
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
alias :included :registered
end

View file

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