remove instance variable and page block support

This commit is contained in:
Thomas Reynolds 2014-01-03 15:49:54 -08:00
parent 614d69dc18
commit 98e3c8aa79
20 changed files with 54 additions and 139 deletions

View file

@ -1,6 +1,11 @@
master master
=== ===
* Remove `page` template local. Use `current_resource` instead.
* Dropped support for `page` & `proxy` blocks.
* Dropped support for instance variables inside templates.
* Moved all rendering into `TemplateRenderer` and `FileRenderer`
* Placed all template evaluation inside the `TemplateContext` class
* Remove deprecated `request` instance * Remove deprecated `request` instance
* Remove old module-style extension support * Remove old module-style extension support
* Placed all `config.rb` evaluation inside the `ConfigContext` class * Placed all `config.rb` evaluation inside the `ConfigContext` class

View file

@ -7,7 +7,7 @@
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"> <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<!-- Use title if it's in the page YAML frontmatter --> <!-- Use title if it's in the page YAML frontmatter -->
<title><%= current_page.data.title || "The Middleman" %></title> <title><%= data.page.title || "The Middleman" %></title>
<%= stylesheet_link_tag "normalize", "all" %> <%= stylesheet_link_tag "normalize", "all" %>
<%= javascript_include_tag "all" %> <%= javascript_include_tag "all" %>

View file

@ -1,9 +0,0 @@
Feature: Instance Variables
Scenario: A dynamic page template using instance variables
Given the Server is running at "instance-vars-app"
When I go to "/a.html"
Then I should see "A: 'set'"
Then I should see "B: ''"
When I go to "/b.html"
Then I should see "A: ''"
Then I should see "B: 'set'"

View file

@ -15,8 +15,6 @@ Feature: Proxy Pages (using proxy rather than page)
| fake2/two.html | | fake2/two.html |
| fake3/one.html | | fake3/one.html |
| fake3/two.html | | fake3/two.html |
| fake4/one.html |
| fake4/two.html |
| target_ignore.html | | target_ignore.html |
| target_ignore2.html | | target_ignore2.html |
| target_ignore3.html | | target_ignore3.html |
@ -35,8 +33,6 @@ Feature: Proxy Pages (using proxy rather than page)
Then I should see "I am real" Then I should see "I am real"
When I go to "/fake3.html" When I go to "/fake3.html"
Then I should see "I am real" Then I should see "I am real"
When I go to "/fake4.html"
Then I should see "I am real"
Scenario: Preview proxy with variable one Scenario: Preview proxy with variable one
Given the Server is running at "proxy-pages-app" Given the Server is running at "proxy-pages-app"
@ -49,9 +45,6 @@ Feature: Proxy Pages (using proxy rather than page)
When I go to "/fake3/one.html" When I go to "/fake3/one.html"
Then I should see "I am real: one" Then I should see "I am real: one"
When I go to "/fake4/one.html"
Then I should see "I am real: one"
Scenario: Preview proxy with variable two Scenario: Preview proxy with variable two
Given the Server is running at "proxy-pages-app" Given the Server is running at "proxy-pages-app"
When I go to "/fake/two.html" When I go to "/fake/two.html"
@ -63,9 +56,6 @@ Feature: Proxy Pages (using proxy rather than page)
When I go to "/fake3/two.html" When I go to "/fake3/two.html"
Then I should see "I am real: two" Then I should see "I am real: two"
When I go to "/fake4/two.html"
Then I should see "I am real: two"
Scenario: Build proxy with variable one Scenario: Build proxy with variable one
Given a successfully built app at "proxy-pages-app" Given a successfully built app at "proxy-pages-app"
When I cd to "build" When I cd to "build"

View file

@ -15,38 +15,16 @@ page "target_ignore3.html", :proxy => "should_be_ignored7.html", :ignore => true
page "/target_ignore4.html", :proxy => "should_be_ignored8.html", :ignore => true page "/target_ignore4.html", :proxy => "should_be_ignored8.html", :ignore => true
%w(one two).each do |num| %w(one two).each do |num|
page "/fake/#{num}.html", :proxy => "/real/index.html", :ignore => true do page "/fake/#{num}.html", :proxy => "/real/index.html", :ignore => true, :locals => { :num => num }
@num = num page "fake2/#{num}.html", :proxy => "/real/index.html", :ignore => true, :locals => { :num => num }
end page "fake3/#{num}.html", :proxy => "real/index.html", :ignore => true, :locals => { :num => num }
page "fake2/#{num}.html", :proxy => "/real/index.html", :ignore => true do page "/fake4/#{num}.html", :proxy => "real/index.html", :ignore => true, :locals => { :num => num }
@num = num
end
page "fake3/#{num}.html", :proxy => "real/index.html", :ignore => true do
@num = num
end
page "/fake4/#{num}.html", :proxy => "real/index.html", :ignore => true do
@num = num
end
end end
page "明日がある.html", :proxy => "/real.html", :layout => false page "明日がある.html", :proxy => "/real.html", :layout => false
page "f*/*" do page "f*/*", :locals => { :all_glob => "I am all glob" }
@all_glob = "I am all glob" page "fake/*", :locals => { :glob_var => "I am one glob" }
end page "fake2/*", :locals => { :glob_var => "I am two glob" }
page "fake3/*", :locals => { :glob_var => "I am three glob" }
page "fake/*" do page "fake4/*", :locals => { :glob_var => "I am four glob" }
@glob_var = "I am one glob"
end
page "fake2/*" do
@glob_var = "I am two glob"
end
page "fake3/*" do
@glob_var = "I am three glob"
end
page "fake4/*" do
@glob_var = "I am four glob"
end

View file

@ -2,8 +2,8 @@
layout: false layout: false
--- ---
I am real: <%= @num %> I am real: <%= num %>
Global: <%= @glob_var %> Global: <%= glob_var %>
All: <%= @all_glob %> All: <%= all_glob %>

View file

@ -1,7 +1,5 @@
[:en, :es].each do |locale| [:en, :es].each do |locale|
proxy "/#{locale}/index.html", "index.html", :ignore => true do proxy "/#{locale}/index.html", "index.html", :ignore => true, :lang => locale
::I18n.locale = locale
end
end end
proxy "/fr/index.html", "index.html", :lang => :fr proxy "/fr/index.html", "index.html", :lang => :fr

View file

@ -1,7 +0,0 @@
page "a.html", :proxy => 'content.html', :ignore => true do
@a = "set"
end
page "b.html", :proxy => 'content.html', :ignore => true do
@b = "set"
end

View file

@ -1,2 +0,0 @@
A: '<%= @a %>'
B: '<%= @b %>'

View file

@ -1 +0,0 @@
<%= yield %>

View file

@ -13,9 +13,6 @@ proxy "/target_ignore4.html", "should_be_ignored8.html", :ignore => true
proxy "/fake/#{num}.html", "/real/index.html", :ignore => true, :locals => { :num => num } proxy "/fake/#{num}.html", "/real/index.html", :ignore => true, :locals => { :num => num }
proxy "fake2/#{num}.html", "/real/index.html", :ignore => true, :locals => { :num => num } proxy "fake2/#{num}.html", "/real/index.html", :ignore => true, :locals => { :num => num }
proxy "fake3/#{num}.html", "real/index.html", :ignore => true, :locals => { :num => num } proxy "fake3/#{num}.html", "real/index.html", :ignore => true, :locals => { :num => num }
proxy "/fake4/#{num}.html", "real/index-ivars.html", :ignore => true do
@num = num
end
end end
proxy "明日がある.html", "/real.html", :layout => false proxy "明日がある.html", "/real.html", :layout => false

View file

@ -1,6 +0,0 @@
---
layout: false
---
I am real: <%= @num %>

View file

@ -1 +1,5 @@
---
layout: false
---
Hola Mundo Hola Mundo

View file

@ -1 +0,0 @@
<%= yield %>

View file

@ -12,9 +12,9 @@ module Middleman
@layout_name = layout_name @layout_name = layout_name
end end
def page(url, opts={}, &block) def page(url, opts={})
opts[:layout] ||= @layout_name opts[:layout] ||= @layout_name
@scope.page(url, opts, &block) @scope.page(url, opts)
end end
delegate :proxy, :to => :scope delegate :proxy, :to => :scope
@ -41,10 +41,12 @@ module Middleman
# @param [String] url # @param [String] url
# @param [Hash] opts # @param [Hash] opts
# @return [void] # @return [void]
def page(url, opts={}, &block) def page(url, opts={})
options = opts.dup
# Default layout # Default layout
opts[:layout] = @app.config[:layout] if opts[:layout].nil? options[:layout] = @app.config[:layout] if options[:layout].nil?
metadata = { :options => opts, :blocks => Array(block) } metadata = { :options => options, :locals => options.delete(:locals) || {} }
# If the url is a regexp # If the url is a regexp
unless url.is_a?(Regexp) || url.include?('*') unless url.is_a?(Regexp) || url.include?('*')
@ -55,11 +57,11 @@ module Middleman
end end
# Setup proxy # Setup proxy
if target = opts.delete(:proxy) if target = options.delete(:proxy)
# TODO: deprecate proxy through page? # TODO: deprecate proxy through page?
@app.proxy(url, target, opts, &block) @app.proxy(url, target, opts.dup)
return return
elsif opts.delete(:ignore) elsif options.delete(:ignore)
# TODO: deprecate ignore through page? # TODO: deprecate ignore through page?
@app.ignore(url) @app.ignore(url)
end end

View file

@ -23,13 +23,14 @@ module Middleman
# @param [Hash] opts options to apply to the proxy, including things like # @param [Hash] opts options to apply to the proxy, including things like
# :locals, :ignore to hide the proxy target, :layout, and :directory_indexes. # :locals, :ignore to hide the proxy target, :layout, and :directory_indexes.
# @return [void] # @return [void]
def create_proxy(path, target, opts={}, &block) def create_proxy(path, target, opts={})
metadata = { :options => {}, :locals => {}, :blocks => [] } options = opts.dup
metadata[:blocks] << block if block_given?
metadata[:locals] = opts.delete(:locals) || {}
@app.ignore(target) if opts.delete(:ignore) metadata = { :options => {}, :locals => {} }
metadata[:options] = opts metadata[:locals] = options.delete(:locals) || {}
@app.ignore(target) if options.delete(:ignore)
metadata[:options] = options
@proxy_configs << ProxyConfiguration.new(:path => path, :target => target, :metadata => metadata) @proxy_configs << ProxyConfiguration.new(:path => path, :target => target, :metadata => metadata)
@ -45,6 +46,7 @@ module Middleman
config.path config.path
) )
p.proxy_to(config.target) p.proxy_to(config.target)
p.add_metadata(config.metadata) p.add_metadata(config.metadata)
p p
end end
@ -65,7 +67,7 @@ module Middleman
@target = ::Middleman::Util.normalize_path(t) @target = ::Middleman::Util.normalize_path(t)
end end
# Additional metadata like blocks and locals to apply to the proxy # Additional metadata like locals to apply to the proxy
attr_accessor :metadata attr_accessor :metadata
# Create a new proxy configuration from hash options # Create a new proxy configuration from hash options

View file

@ -48,7 +48,7 @@ module Middleman
@source_file = source_file @source_file = source_file
@destination_path = @path @destination_path = @path
@local_metadata = { :options => {}, :locals => {}, :page => {}, :blocks => [] } @local_metadata = { :options => {}, :locals => {}, :page => {}}
end end
# Whether this resource has a template file # Whether this resource has a template file
@ -64,30 +64,18 @@ module Middleman
result = store.metadata_for_path(path).dup result = store.metadata_for_path(path).dup
file_meta = store.metadata_for_file(source_file).dup file_meta = store.metadata_for_file(source_file).dup
if file_meta.has_key?(:blocks)
result[:blocks] += file_meta.delete(:blocks)
end
result.deep_merge!(file_meta) result.deep_merge!(file_meta)
local_meta = @local_metadata.dup local_meta = @local_metadata.dup
if local_meta.has_key?(:blocks)
result[:blocks] += local_meta.delete(:blocks)
end
result.deep_merge!(local_meta) result.deep_merge!(local_meta)
result[:blocks] = result[:blocks].flatten.compact
result result
end end
# Merge in new metadata specific to this resource. # Merge in new metadata specific to this resource.
# @param [Hash] metadata A metadata block like provides_metadata_for_path takes # @param [Hash] metadata A metadata block like provides_metadata_for_path takes
def add_metadata(metadata={}, &block) def add_metadata(meta={})
metadata = metadata.dup @local_metadata.deep_merge!(meta.dup)
if metadata.has_key?(:blocks)
@local_metadata[:blocks] += metadata.delete(:blocks)
end
@local_metadata.deep_merge!(metadata)
@local_metadata[:blocks] += [ block ] if block_given?
end end
# The output/preview URL for this resource # The output/preview URL for this resource
@ -106,7 +94,7 @@ module Middleman
# Render this resource # Render this resource
# @return [String] # @return [String]
def render(opts={}, locs={}, &block) def render(opts={}, locs={})
if !template? if !template?
return ::Middleman::FileRenderer.new(@app, source_file).get_template_data_for_file return ::Middleman::FileRenderer.new(@app, source_file).get_template_data_for_file
end end
@ -132,9 +120,6 @@ module Middleman
app.data.store('page', md[:page]) app.data.store('page', md[:page])
end end
blocks = Array(md[:blocks]).dup
blocks << block if block_given?
locs[:current_path] ||= self.destination_path locs[:current_path] ||= self.destination_path
# Certain output file types don't use layouts # Certain output file types don't use layouts
@ -143,7 +128,7 @@ module Middleman
end end
renderer = ::Middleman::TemplateRenderer.new(@app, source_file) renderer = ::Middleman::TemplateRenderer.new(@app, source_file)
renderer.render(locs, opts, blocks) renderer.render(locs, opts)
end end
end end

View file

@ -137,18 +137,12 @@ module Middleman
# @param [String] source_file # @param [String] source_file
# @return [Hash] # @return [Hash]
def metadata_for_file(source_file) def metadata_for_file(source_file)
blank_metadata = { :options => {}, :locals => {}, :page => {}, :blocks => [] } blank_metadata = { :options => {}, :locals => {}, :page => {} }
provides_metadata.inject(blank_metadata) do |result, (callback, matcher)| provides_metadata.inject(blank_metadata) do |result, (callback, matcher)|
next result if matcher && !source_file.match(matcher) next result if matcher && !source_file.match(matcher)
metadata = callback.call(source_file).dup metadata = callback.call(source_file).dup
if metadata.has_key?(:blocks)
result[:blocks] << metadata[:blocks]
metadata.delete(:blocks)
end
result.deep_merge(metadata) result.deep_merge(metadata)
end end
end end
@ -171,7 +165,7 @@ module Middleman
def metadata_for_path(request_path) def metadata_for_path(request_path)
return @_cached_metadata[request_path] if @_cached_metadata[request_path] return @_cached_metadata[request_path] if @_cached_metadata[request_path]
blank_metadata = { :options => {}, :locals => {}, :page => {}, :blocks => [] } blank_metadata = { :options => {}, :locals => {}, :page => {} }
@_cached_metadata[request_path] = provides_metadata_for_path.inject(blank_metadata) do |result, (callback, matcher)| @_cached_metadata[request_path] = provides_metadata_for_path.inject(blank_metadata) do |result, (callback, matcher)|
case matcher case matcher
@ -183,8 +177,6 @@ module Middleman
metadata = callback.call(request_path).dup metadata = callback.call(request_path).dup
result[:blocks] += Array(metadata.delete(:blocks))
result.deep_merge(metadata) result.deep_merge(metadata)
end end
end end

View file

@ -26,7 +26,7 @@ module Middleman
# @param [Hash] locs # @param [Hash] locs
# @param [Hash] opts # @param [Hash] opts
# @return [String] # @return [String]
def render(locs={}, opts={}, blocks=[]) def render(locs={}, opts={})
path = @path.dup path = @path.dup
extension = File.extname(path) extension = File.extname(path)
engine = extension[1..-1].to_sym engine = extension[1..-1].to_sym
@ -43,10 +43,6 @@ module Middleman
context.init_haml_helpers context.init_haml_helpers
end end
blocks.each do |block|
context.instance_eval(&block)
end
# Keep rendering template until we've used up all extensions. This # Keep rendering template until we've used up all extensions. This
# handles cases like `style.css.sass.erb` # handles cases like `style.css.sass.erb`
content = nil content = nil

View file

@ -124,19 +124,11 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
lang = @mount_at_root lang = @mount_at_root
end end
instance_vars = Proc.new do {
@lang = lang :locals => {
@page_id = page_id
end
locals = {
:lang => lang, :lang => lang,
:page_id => page_id :page_id => page_id
} },
{
:blocks => [instance_vars],
:locals => locals,
:options => { :lang => lang } :options => { :lang => lang }
} }
end end