Improve path normalization in liquid_renderer (#8075)
Merge pull request 8075
This commit is contained in:
parent
d51cd070ed
commit
48e6cb18d7
|
@ -5,11 +5,6 @@ require_relative "liquid_renderer/table"
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class LiquidRenderer
|
class LiquidRenderer
|
||||||
extend Forwardable
|
|
||||||
|
|
||||||
private def_delegator :@site, :in_source_dir, :source_dir
|
|
||||||
private def_delegator :@site, :in_theme_dir, :theme_dir
|
|
||||||
|
|
||||||
def initialize(site)
|
def initialize(site)
|
||||||
@site = site
|
@site = site
|
||||||
Liquid::Template.error_mode = @site.config["liquid"]["error_mode"].to_sym
|
Liquid::Template.error_mode = @site.config["liquid"]["error_mode"].to_sym
|
||||||
|
@ -22,13 +17,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def file(filename)
|
def file(filename)
|
||||||
filename.match(filename_regex)
|
filename = normalize_path(filename)
|
||||||
filename =
|
|
||||||
if Regexp.last_match(1) == theme_dir("")
|
|
||||||
::File.join(::File.basename(Regexp.last_match(1)), Regexp.last_match(2))
|
|
||||||
else
|
|
||||||
Regexp.last_match(2)
|
|
||||||
end
|
|
||||||
LiquidRenderer::File.new(self, filename).tap do
|
LiquidRenderer::File.new(self, filename).tap do
|
||||||
@stats[filename] ||= new_profile_hash
|
@stats[filename] ||= new_profile_hash
|
||||||
end
|
end
|
||||||
|
@ -64,9 +53,23 @@ module Jekyll
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def filename_regex
|
def normalize_path(filename)
|
||||||
@filename_regex ||= begin
|
@normalize_path ||= {}
|
||||||
%r!\A(#{Regexp.escape(source_dir)}/|#{Regexp.escape(theme_dir.to_s)}/|/*)(.*)!i
|
@normalize_path[filename] ||= begin
|
||||||
|
theme_dir = @site.theme&.root
|
||||||
|
case filename
|
||||||
|
when %r!\A(#{Regexp.escape(@site.source)}/)(?<rest>.*)!io
|
||||||
|
Regexp.last_match(:rest)
|
||||||
|
when %r!(/gems/.*)*/gems/(?<dirname>[^/]+)(?<rest>.*)!,
|
||||||
|
%r!(?<dirname>[^/]+/lib)(?<rest>.*)!
|
||||||
|
"#{Regexp.last_match(:dirname)}#{Regexp.last_match(:rest)}"
|
||||||
|
when theme_dir && %r!\A#{Regexp.escape(theme_dir)}/(?<rest>.*)!io
|
||||||
|
PathManager.join(@site.theme.basename, Regexp.last_match(:rest))
|
||||||
|
when %r!\A/(.*)!
|
||||||
|
Regexp.last_match(1)
|
||||||
|
else
|
||||||
|
filename
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,11 @@ module Jekyll
|
||||||
"or includes a symbolic link loop"
|
"or includes a symbolic link loop"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The name of theme directory
|
||||||
|
def basename
|
||||||
|
@basename ||= File.basename(root)
|
||||||
|
end
|
||||||
|
|
||||||
def includes_path
|
def includes_path
|
||||||
@includes_path ||= path_for "_includes"
|
@includes_path ||= path_for "_includes"
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,5 +26,33 @@ class TestLiquidRenderer < JekyllUnitTest
|
||||||
assert_match regexp, output
|
assert_match regexp, output
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "normalize paths of rendered items" do
|
||||||
|
site = fixture_site("theme" => "test-theme")
|
||||||
|
MockRenderer = Class.new(Jekyll::LiquidRenderer) { public :normalize_path }
|
||||||
|
renderer = MockRenderer.new(site)
|
||||||
|
|
||||||
|
assert_equal "feed.xml", renderer.normalize_path("/feed.xml")
|
||||||
|
assert_equal(
|
||||||
|
"_layouts/post.html",
|
||||||
|
renderer.normalize_path(site.in_source_dir("_layouts", "post.html"))
|
||||||
|
)
|
||||||
|
assert_equal(
|
||||||
|
"test-theme/_layouts/page.html",
|
||||||
|
renderer.normalize_path(site.in_theme_dir("_layouts", "page.html"))
|
||||||
|
)
|
||||||
|
assert_equal(
|
||||||
|
"my_plugin-0.1.0/lib/my_plugin/layout.html",
|
||||||
|
renderer.normalize_path(
|
||||||
|
"/users/jo/blog/vendor/bundle/ruby/2.4.0/gems/my_plugin-0.1.0/lib/my_plugin/layout.html"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert_equal(
|
||||||
|
"test_plugin-0.1.0/lib/test_plugin/layout.html",
|
||||||
|
renderer.normalize_path(
|
||||||
|
"C:/Ruby2.4/lib/ruby/gems/2.4.0/gems/test_plugin-0.1.0/lib/test_plugin/layout.html"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue