allow partial lookups without a current_resource (#1912)
current_resource is only needed for relative lookups, so for lookups from source allow them to run without a current resource.
This commit is contained in:
parent
ccc1cc1288
commit
5de9e86a55
1 changed files with 24 additions and 18 deletions
|
@ -127,30 +127,36 @@ module Middleman
|
||||||
# @return [String]
|
# @return [String]
|
||||||
Contract String, Maybe[Bool] => Maybe[IsA['Middleman::SourceFile']]
|
Contract String, Maybe[Bool] => Maybe[IsA['Middleman::SourceFile']]
|
||||||
def locate_partial(partial_path, try_static=true)
|
def locate_partial(partial_path, try_static=true)
|
||||||
return unless resource = sitemap.find_resource_by_destination_path(current_path)
|
|
||||||
|
|
||||||
# Look for partials relative to the current path
|
|
||||||
current_dir = resource.file_descriptor[:relative_path].dirname
|
|
||||||
non_root = partial_path.to_s.sub(/^\//, '')
|
|
||||||
relative_dir = current_dir + Pathname(non_root)
|
|
||||||
|
|
||||||
non_root_no_underscore = non_root.sub(/^_/, '').sub(/\/_/, '/')
|
|
||||||
relative_dir_no_underscore = current_dir + Pathname(non_root_no_underscore)
|
|
||||||
|
|
||||||
partial_file = nil
|
partial_file = nil
|
||||||
|
lookup_stack = []
|
||||||
|
non_root = partial_path.to_s.sub(/^\//, '')
|
||||||
|
non_root_no_underscore = non_root.sub(/^_/, '').sub(/\/_/, '/')
|
||||||
|
|
||||||
[
|
if resource = current_resource
|
||||||
[relative_dir.to_s, { preferred_engine: resource.file_descriptor[:relative_path].extname[1..-1].to_sym }],
|
current_dir = resource.file_descriptor[:relative_path].dirname
|
||||||
[non_root],
|
relative_dir = current_dir + Pathname(non_root)
|
||||||
[non_root, { try_static: try_static }],
|
relative_dir_no_underscore = current_dir + Pathname(non_root_no_underscore)
|
||||||
[relative_dir_no_underscore.to_s, { try_static: try_static }],
|
end
|
||||||
[non_root_no_underscore, { try_static: try_static }]
|
|
||||||
].each do |args|
|
|
||||||
|
lookup_stack.push [ relative_dir.to_s,
|
||||||
|
{ preferred_engine: resource.file_descriptor[:relative_path]
|
||||||
|
.extname[1..-1].to_sym }] if relative_dir
|
||||||
|
lookup_stack.push [ non_root ]
|
||||||
|
lookup_stack.push [ non_root,
|
||||||
|
{ try_static: try_static }]
|
||||||
|
lookup_stack.push [ relative_dir_no_underscore.to_s,
|
||||||
|
{ try_static: try_static }] if relative_dir_no_underscore
|
||||||
|
lookup_stack.push [ non_root_no_underscore,
|
||||||
|
{ try_static: try_static }]
|
||||||
|
|
||||||
|
|
||||||
|
lookup_stack.each do |args|
|
||||||
partial_file = ::Middleman::TemplateRenderer.resolve_template(@app, *args)
|
partial_file = ::Middleman::TemplateRenderer.resolve_template(@app, *args)
|
||||||
break if partial_file
|
break if partial_file
|
||||||
end
|
end
|
||||||
|
|
||||||
partial_file || nil
|
partial_file
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_path
|
def current_path
|
||||||
|
|
Loading…
Reference in a new issue