page helper uses provides_metadata to apply options at last minute to final output path. closes #182

This commit is contained in:
Thomas Reynolds 2011-12-07 21:00:27 -08:00
parent 745ce14624
commit e2d5839874
6 changed files with 45 additions and 10 deletions

View file

@ -230,7 +230,7 @@ module Middleman::CoreExtensions::Rendering
::Tilt[path]
end
if found_path || File.exists?(on_disk_path)
if found_path || (File.exists?(on_disk_path) && !File.directory?(on_disk_path))
engine = found_path ? File.extname(found_path)[1..-1].to_sym : nil
[ found_path || on_disk_path, engine ]
else

View file

@ -25,6 +25,18 @@ module Middleman::CoreExtensions::Routing
# page "/about.html", :layout => false
# page "/", :layout => :homepage_layout
def page(url, opts={}, &block)
if url.include?("*")
url = Regexp.new(url.gsub("*", "(.*)").gsub(/^\//, "^"))
end
if url.is_a?(Regexp) && !opts.empty?
provides_metadata_for_path url do |url|
{ :options => opts }
end
return
end
opts[:layout] = layout if opts[:layout].nil?
url = full_path(url)

View file

@ -40,5 +40,11 @@ module Middleman::CoreExtensions::Sitemap
@_provides_metadata << [block, matcher] if block_given?
@_provides_metadata
end
def provides_metadata_for_path(matcher=nil, &block)
@_provides_metadata_for_path ||= []
@_provides_metadata_for_path << [block, matcher] if block_given?
@_provides_metadata_for_path
end
end
end

View file

@ -69,6 +69,7 @@ module Middleman::Sitemap
end
def touch
template.touch if template?
end
def custom_renderer(&block)

View file

@ -115,7 +115,9 @@ module Middleman::Sitemap
# @app.logger.debug :sitemap_update, Time.now, path if @app.logging?
# Add generic path
page(path).source_file = File.expand_path(file, @app.root)
p = page(path)
p.source_file = File.expand_path(file, @app.root)
p.touch
true
end

View file

@ -30,17 +30,31 @@ module Middleman::Sitemap
page.ext
end
def touch
app.cache.remove(:metadata, source_file)
end
def metadata
app.cache.fetch(:metadata, source_file) do
metadata = { :options => {}, :locals => {} }
metadata = app.cache.fetch(:metadata, source_file) do
data = { :options => {}, :locals => {} }
app.provides_metadata.each do |callback, matcher|
next if !matcher.nil? && !source_file.match(matcher)
result = app.instance_exec(source_file, &callback)
data = data.deep_merge(result)
end
data
end
app.provides_metadata_for_path.each do |callback, matcher|
next if !matcher.nil? && !path.match(matcher)
result = app.instance_exec(path, &callback)
metadata = metadata.deep_merge(result)
end
metadata
end
end
def render(opts={}, locs={}, &block)
opts = options.deep_merge(metadata[:options]).deep_merge(opts)