support glob page instance vars. fixes #230

This commit is contained in:
Thomas Reynolds 2012-01-05 21:18:55 -08:00
parent 22adf3b516
commit 92319ebffc
7 changed files with 90 additions and 23 deletions

View file

@ -46,23 +46,45 @@ Feature: Dynamic Pages
Given the Server is running at "dynamic-pages-app" Given the Server is running at "dynamic-pages-app"
When I go to "/fake/one.html" When I go to "/fake/one.html"
Then I should see "I am real: one" Then I should see "I am real: one"
Then I should see "Global: I am one glob"
Then I should see "All: I am all glob"
When I go to "/fake2/one.html" When I go to "/fake2/one.html"
Then I should see "I am real: one" Then I should see "I am real: one"
Then I should see "Global: I am two glob"
Then I should see "All: I am all glob"
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"
Then I should see "Global: I am three glob"
Then I should see "All: I am all glob"
When I go to "/fake4/one.html" When I go to "/fake4/one.html"
Then I should see "I am real: one" Then I should see "I am real: one"
Then I should see "Global: I am four glob"
Then I should see "All: I am all glob"
Scenario: Preview proxy with variable two Scenario: Preview proxy with variable two
Given the Server is running at "dynamic-pages-app" Given the Server is running at "dynamic-pages-app"
When I go to "/fake/two.html" When I go to "/fake/two.html"
Then I should see "I am real: two" Then I should see "I am real: two"
Then I should see "Global: I am one glob"
Then I should see "All: I am all glob"
When I go to "/fake2/two.html" When I go to "/fake2/two.html"
Then I should see "I am real: two" Then I should see "I am real: two"
Then I should see "Global: I am two glob"
Then I should see "All: I am all glob"
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"
Then I should see "Global: I am three glob"
Then I should see "All: I am all glob"
When I go to "/fake4/two.html" When I go to "/fake4/two.html"
Then I should see "I am real: two" Then I should see "I am real: two"
Then I should see "Global: I am four glob"
Then I should see "All: I am all glob"
Scenario: Target ignore Scenario: Target ignore
Given the Server is running at "dynamic-pages-app" Given the Server is running at "dynamic-pages-app"

View file

@ -16,5 +16,3 @@ Feature: Instance Vars
When I go to "/instance-var-set.html" When I go to "/instance-var-set.html"
When I go to "/no-instance-var.html" When I go to "/no-instance-var.html"
Then I should see "No var..." Then I should see "No var..."

View file

@ -26,4 +26,24 @@ page "/target_ignore4.html", :proxy => "should_be_ignored8.html", :ignore => tru
page "/fake4/#{num}.html", :proxy => "real/index.html" do page "/fake4/#{num}.html", :proxy => "real/index.html" do
@num = num @num = num
end end
end
page "f*/*" do
@all_glob = "I am all glob"
end
page "fake/*" do
@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 end

View file

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

View file

@ -25,13 +25,15 @@ module Middleman::CoreExtensions::Routing
# page "/about.html", :layout => false # page "/about.html", :layout => false
# page "/", :layout => :homepage_layout # page "/", :layout => :homepage_layout
def page(url, opts={}, &block) def page(url, opts={}, &block)
a_block = block_given? ? block : nil
if url.include?("*") if url.include?("*")
url = Regexp.new(url.gsub("*", "(.*)").gsub(/^\//, "^")) url = Regexp.new(url.gsub("*", "(.*?)").gsub(/^\//, "^"))
end end
if url.is_a?(Regexp) && !opts.empty? if url.is_a?(Regexp)
provides_metadata_for_path url do |url| provides_metadata_for_path url do |url|
{ :options => opts } { :options => opts, :blocks => [a_block] }
end end
return return
@ -57,11 +59,9 @@ module Middleman::CoreExtensions::Routing
end end
end end
a_block = block_given? ? block : nil
if a_block || !opts.empty? if a_block || !opts.empty?
sitemap.page(url) do provides_metadata_for_path url do |url|
template.options = opts { :options => opts, :blocks => [a_block] }
template.blocks = [a_block]
end end
end end
end end

View file

@ -23,6 +23,14 @@ module Middleman::Sitemap
@source_file = src @source_file = src
end end
def request_path
if proxy?
store.page(proxied_to).path
else
path
end
end
def source_file def source_file
if proxy? if proxy?
store.page(proxied_to).source_file store.page(proxied_to).source_file
@ -83,24 +91,26 @@ module Middleman::Sitemap
if proxy? if proxy?
# Forward blocks # Forward blocks
forward_blocks = template.blocks.compact # forward_blocks = template.blocks.compact
forward_blocks << block if block_given? # forward_blocks << block if block_given?
store.page(proxied_to).template.render(*args) do t = store.page(proxied_to).template
forward_blocks.each do |block| t.request_path = path
instance_exec(&block) t.render(*args)
end # do
end # forward_blocks.each do |block|
# instance_exec(&block)
# end
# end
elsif !custom_renderer.nil? elsif !custom_renderer.nil?
params = args.dup params = args.dup
params << block if block_given? params << block if block_given?
instance_exec(*params, &custom_renderer) instance_exec(*params, &custom_renderer)
else else
template.request_path = path
template.render(*args, &block) template.render(*args, &block)
end end
end end
def directory_index? def directory_index?
path.include?(app.index_file) || path =~ /\/$/ || eponymous_directory? path.include?(app.index_file) || path =~ /\/$/ || eponymous_directory?
end end

View file

@ -1,7 +1,7 @@
module Middleman::Sitemap module Middleman::Sitemap
class Template class Template
attr_accessor :page, :options, :locals, :blocks#, :dependencies attr_accessor :page, :options, :locals, :blocks, :request_path
def initialize(page) def initialize(page)
@page = page @page = page
@ -36,7 +36,7 @@ module Middleman::Sitemap
def metadata def metadata
metadata = app.cache.fetch(:metadata, source_file) do metadata = app.cache.fetch(:metadata, source_file) do
data = { :options => {}, :locals => {}, :page => {} } data = { :options => {}, :locals => {}, :page => {}, :blocks => [] }
app.provides_metadata.each do |callback, matcher| app.provides_metadata.each do |callback, matcher|
next if !matcher.nil? && !source_file.match(matcher) next if !matcher.nil? && !source_file.match(matcher)
@ -48,8 +48,17 @@ module Middleman::Sitemap
end end
app.provides_metadata_for_path.each do |callback, matcher| app.provides_metadata_for_path.each do |callback, matcher|
next if !matcher.nil? && !path.match(matcher) if matcher.is_a? Regexp
result = app.instance_exec(path, &callback) next if !self.request_path.match(matcher)
elsif matcher.is_a? String
next if "/#{self.request_path}" != matcher
end
result = app.instance_exec(self.request_path, &callback)
if result.has_key?(:blocks)
metadata[:blocks] << result[:blocks]
result.delete(:blocks)
end
metadata = metadata.deep_merge(result) metadata = metadata.deep_merge(result)
end end
@ -72,6 +81,10 @@ module Middleman::Sitemap
app.instance_eval(&block) app.instance_eval(&block)
end end
md[:blocks].flatten.compact.each do |block|
app.instance_eval(&block)
end
app.instance_eval(&block) if block_given? app.instance_eval(&block) if block_given?
result = app.render_template(source_file, locs, opts) result = app.render_template(source_file, locs, opts)