Reapply changes from 477f87e98a to TemplateRenderer/TemplateContext

This commit is contained in:
Ben Hollis 2014-02-22 18:24:40 -08:00
parent c5b0ba17ea
commit e662b6433f
2 changed files with 43 additions and 71 deletions

View file

@ -68,43 +68,28 @@ module Middleman
locals = options[:locals] locals = options[:locals]
found_partial = false found_partial = false
engine = nil resolve_opts = { try_without_underscore: true }
# If the path is known to the sitemap # If the path is known to the sitemap
if resource = sitemap.find_resource_by_path(current_path) if resource = sitemap.find_resource_by_path(current_path)
current_dir = File.dirname(resource.source_file) current_dir = File.dirname(resource.source_file)
engine = File.extname(resource.source_file)[1..-1].to_sym resolve_opts[:preferred_engine] = File.extname(resource.source_file)[1..-1].to_sym
# Look for partials relative to the current path # Look for partials relative to the current path
relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(self.source_dir)}/?}, ''), data) relative_dir = File.join(current_dir.sub(%r{^#{Regexp.escape(self.source_dir)}/?}, ''), data)
# Try to use the current engine first found_partial = ::Middleman::TemplateRenderer.resolve_template(@app, relative_dir, resolve_opts)
found_partial, found_engine = ::Middleman::TemplateRenderer.resolve_template(@app, relative_dir, :preferred_engine => engine, :try_without_underscore => true)
# Fall back to any engine available
if !found_partial
found_partial, found_engine = ::Middleman::TemplateRenderer.resolve_template(@app, relative_dir, :try_without_underscore => true)
end
end end
# Look in the partials_dir for the partial with the current engine
partials_path = File.join(config[:partials_dir], data)
if !found_partial && !engine.nil?
found_partial, found_engine = ::Middleman::TemplateRenderer.resolve_template(@app, partials_path, :preferred_engine => engine, :try_without_underscore => true)
end
# Look in the root with any engine
if !found_partial if !found_partial
found_partial, found_engine = ::Middleman::TemplateRenderer.resolve_template(@app, partials_path, :try_without_underscore => true) partials_path = File.join(@app.config[:partials_dir], data)
found_partial = ::Middleman::TemplateRenderer.resolve_template(@app, partials_path, resolve_opts)
end end
# Render the partial if found, otherwide throw exception raise ::Middleman::TemplateRenderer::TemplateNotFound, "Could not locate partial: #{data}" unless found_partial
if found_partial
file_renderer = ::Middleman::FileRenderer.new(@app, found_partial) file_renderer = ::Middleman::FileRenderer.new(@app, found_partial)
file_renderer.render(locals, options, self, &block) file_renderer.render(locals, options, self, &block)
else
raise ::Middleman::TemplateRenderer::TemplateNotFound, "Could not locate partial: #{data}"
end
end end
end end
end end

View file

@ -55,7 +55,7 @@ module Middleman
path = File.basename(path, File.extname(path)) path = File.basename(path, File.extname(path))
rescue LocalJumpError rescue LocalJumpError
raise "Tried to render a layout (calls yield) at #{path} like it was a template. Non-default layouts need to be in #{source}/#{layout_dir}." raise "Tried to render a layout (calls yield) at #{path} like it was a template. Non-default layouts need to be in #{source}/#{@app.config[:layouts_dir]}."
end end
end end
@ -130,26 +130,14 @@ module Middleman
# Whether we've found the layout # Whether we've found the layout
layout_path = false layout_path = false
# If we prefer a specific engine resolve_opts = {}
if !preferred_engine.nil? resolve_opts[:preferred_engine] = preferred_engine if !preferred_engine.nil?
# Check root
layout_path, layout_engine = resolve_template(app, name, :preferred_engine => preferred_engine)
# Check layouts folder # Check layouts folder
if !layout_path layout_path = resolve_template(app, File.join(app.config[:layouts_dir], name.to_s), resolve_opts)
layout_path, layout_engine = resolve_template(app, File.join(app.config[:layouts_dir], name.to_s), :preferred_engine => preferred_engine)
end
end
# Check root, no preference # If we didn't find it, check root
if !layout_path layout_path = resolve_template(app, name, resolve_opts) unless layout_path
layout_path, layout_engine = resolve_template(app, name)
end
# Check layouts folder, no preference
if !layout_path
layout_path, layout_engine = resolve_template(app, File.join(app.config[:layouts_dir], name.to_s))
end
# Return the path # Return the path
layout_path layout_path
@ -165,8 +153,9 @@ module Middleman
# Find a template on disk given a output path # Find a template on disk given a output path
# @param [String] request_path # @param [String] request_path
# @param [Hash] options # @option options [Boolean] :preferred_engine If set, try this engine first, then fall back to any engine.
# @return [Array<String, Symbol>, Boolean] # @option options [Boolean] :try_without_underscore
# @return [String, Boolean] Either the path to the template, or false
def self.resolve_template(app, request_path, options={}) def self.resolve_template(app, request_path, options={})
# Find the path by searching or using the cache # Find the path by searching or using the cache
request_path = request_path.to_s request_path = request_path.to_s
@ -175,48 +164,46 @@ module Middleman
on_disk_path = File.expand_path(relative_path, app.source_dir) on_disk_path = File.expand_path(relative_path, app.source_dir)
# By default, any engine will do # By default, any engine will do
preferred_engine = '*' preferred_engines = ['*']
# Unless we're specifically looking for a preferred engine # If we're specifically looking for a preferred engine
if options.has_key?(:preferred_engine) if options.has_key?(:preferred_engine)
extension_class = ::Tilt[options[:preferred_engine]] extension_class = ::Tilt[options[:preferred_engine]]
matched_exts = [] matched_exts = []
# Get a list of extensions for a preferred engine # Get a list of extensions for a preferred engine
# TODO: Cache this matched_exts = ::Tilt.mappings.select do |ext, engines|
::Tilt.mappings.each do |ext, engines| engines.include? extension_class
next unless engines.include? extension_class end.keys
matched_exts << ext
end
# Change the glob to only look for the matched extensions # Prefer to look for the matched extensions
if matched_exts.length > 0 unless matched_exts.empty?
preferred_engine = '{' + matched_exts.join(',') + '}' preferred_engines.unshift('{' + matched_exts.join(',') + '}')
else
return false
end end
end end
# Look for files that match search_paths = preferred_engines.flat_map do |preferred_engine|
path_with_ext = on_disk_path + '.' + preferred_engine path_with_ext = on_disk_path + '.' + preferred_engine
paths = [path_with_ext]
found_path = Dir[path_with_ext].find do |path| if options[:try_without_underscore]
::Tilt[path] paths << path_with_ext.sub(relative_path, relative_path.sub(/^_/, '').sub(/\/_/, '/'))
end
paths
end end
if !found_path && options[:try_without_underscore] && found_path = nil
path_no_underscore = path_with_ext. search_paths.each do |path_with_ext|
sub(relative_path, relative_path.sub(/^_/, ''). found_path = Dir[path_with_ext].find do |path|
sub(/\/_/, '/'))
found_path = Dir[path_no_underscore].find do |path|
::Tilt[path] ::Tilt[path]
end end
break if found_path
end end
# If we found one, return it and the found engine # If we found one, return it
if found_path || app.files.exists?(on_disk_path) if found_path
engine = found_path ? File.extname(found_path)[1..-1].to_sym : nil found_path
[ found_path || on_disk_path, engine ] elsif File.exists?(on_disk_path)
on_disk_path
else else
false false
end end