Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve path normalization in liquid_renderer #8075

Merged
merged 4 commits into from May 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 18 additions & 15 deletions lib/jekyll/liquid_renderer.rb
Expand Up @@ -5,11 +5,6 @@

module Jekyll
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)
@site = site
Liquid::Template.error_mode = @site.config["liquid"]["error_mode"].to_sym
Expand All @@ -22,13 +17,7 @@ def reset
end

def file(filename)
filename.match(filename_regex)
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
filename = normalize_path(filename)
LiquidRenderer::File.new(self, filename).tap do
@stats[filename] ||= new_profile_hash
end
Expand Down Expand Up @@ -64,9 +53,23 @@ def cache

private

def filename_regex
@filename_regex ||= begin
%r!\A(#{Regexp.escape(source_dir)}/|#{Regexp.escape(theme_dir.to_s)}/|/*)(.*)!i
def normalize_path(filename)
@normalize_path ||= {}
@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(?<theme_repo>#{Regexp.escape(theme_dir)}/)(?<rest>.*)!io
::File.join(::File.basename(Regexp.last_match(:theme_repo)), Regexp.last_match(:rest))
when %r!\A/(.*)!
Regexp.last_match(1)
else
filename
end
end
end

Expand Down
28 changes: 28 additions & 0 deletions test/test_liquid_renderer.rb
Expand Up @@ -26,5 +26,33 @@ class TestLiquidRenderer < JekyllUnitTest
assert_match regexp, output
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