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

@ -171,11 +171,11 @@ module Middleman::CoreExtensions::Rendering
# @private # @private
def locate_layout(name, preferred_engine=nil) def locate_layout(name, preferred_engine=nil)
layout_path = false layout_path = false
if !preferred_engine.nil? if !preferred_engine.nil?
# Check root # Check root
layout_path, layout_engine = resolve_template(name, :preferred_engine => preferred_engine) layout_path, layout_engine = resolve_template(name, :preferred_engine => preferred_engine)
# Check layouts folder # Check layouts folder
if !layout_path if !layout_path
layout_path, layout_engine = resolve_template(File.join("layouts", name.to_s), :preferred_engine => preferred_engine) layout_path, layout_engine = resolve_template(File.join("layouts", name.to_s), :preferred_engine => preferred_engine)
@ -229,8 +229,8 @@ module Middleman::CoreExtensions::Rendering
found_path = Dir[path_with_ext].find do |path| found_path = Dir[path_with_ext].find do |path|
::Tilt[path] ::Tilt[path]
end 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 engine = found_path ? File.extname(found_path)[1..-1].to_sym : nil
[ found_path || on_disk_path, engine ] [ found_path || on_disk_path, engine ]
else else

View file

@ -25,6 +25,18 @@ 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)
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? opts[:layout] = layout if opts[:layout].nil?
url = full_path(url) url = full_path(url)

View file

@ -40,5 +40,11 @@ module Middleman::CoreExtensions::Sitemap
@_provides_metadata << [block, matcher] if block_given? @_provides_metadata << [block, matcher] if block_given?
@_provides_metadata @_provides_metadata
end 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
end end

View file

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

View file

@ -115,7 +115,9 @@ module Middleman::Sitemap
# @app.logger.debug :sitemap_update, Time.now, path if @app.logging? # @app.logger.debug :sitemap_update, Time.now, path if @app.logging?
# Add generic path # 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 true
end end

View file

@ -29,17 +29,31 @@ module Middleman::Sitemap
def ext def ext
page.ext page.ext
end end
def touch
app.cache.remove(:metadata, source_file)
end
def metadata def metadata
app.cache.fetch(:metadata, source_file) do metadata = app.cache.fetch(:metadata, source_file) do
metadata = { :options => {}, :locals => {} } data = { :options => {}, :locals => {} }
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)
result = app.instance_exec(source_file, &callback) result = app.instance_exec(source_file, &callback)
metadata = metadata.deep_merge(result) data = data.deep_merge(result)
end end
metadata
data
end 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) def render(opts={}, locs={}, &block)