Compute relative_path of pages using PathManager (#8408)

Merge pull request 8408
This commit is contained in:
Ashwin Maroli 2020-09-30 11:41:28 +05:30 committed by GitHub
parent 7d8a839a21
commit 7cb10df0b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 1 deletions

65
benchmark/path-manager.rb Normal file
View File

@ -0,0 +1,65 @@
# frozen_string_literal: true
require 'benchmark/ips'
require 'jekyll'
class FooPage
def initialize(dir:, name:)
@dir = dir
@name = name
end
def slow_path
File.join(*[@dir, @name].map(&:to_s).reject(&:empty?)).sub(%r!\A/!, "")
end
def fast_path
Jekyll::PathManager.join(@dir, @name).sub(%r!\A/!, "")
end
end
nil_page = FooPage.new(:dir => nil, :name => nil)
empty_page = FooPage.new(:dir => "", :name => "")
root_page = FooPage.new(:dir => "", :name => "ipsum.md")
nested_page = FooPage.new(:dir => "lorem", :name => "ipsum.md")
slashed_page = FooPage.new(:dir => "/lorem/", :name => "/ipsum.md")
if nil_page.slow_path == nil_page.fast_path
Benchmark.ips do |x|
x.report('nil_page slow') { nil_page.slow_path }
x.report('nil_page fast') { nil_page.fast_path }
x.compare!
end
end
if empty_page.slow_path == empty_page.fast_path
Benchmark.ips do |x|
x.report('empty_page slow') { empty_page.slow_path }
x.report('empty_page fast') { empty_page.fast_path }
x.compare!
end
end
if root_page.slow_path == root_page.fast_path
Benchmark.ips do |x|
x.report('root_page slow') { root_page.slow_path }
x.report('root_page fast') { root_page.fast_path }
x.compare!
end
end
if nested_page.slow_path == nested_page.fast_path
Benchmark.ips do |x|
x.report('nested_page slow') { nested_page.slow_path }
x.report('nested_page fast') { nested_page.fast_path }
x.compare!
end
end
if slashed_page.slow_path == slashed_page.fast_path
Benchmark.ips do |x|
x.report('slashed_page slow') { slashed_page.slow_path }
x.report('slashed_page fast') { slashed_page.fast_path }
x.compare!
end
end

View File

@ -121,6 +121,8 @@ module Jekyll
# NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`) # NOTE: `String#gsub` removes all trailing periods (in comparison to `String#chomp`)
# Returns nothing. # Returns nothing.
def process(name) def process(name)
return unless name
self.ext = File.extname(name) self.ext = File.extname(name)
self.basename = name[0..-ext.length - 1].gsub(%r!\.*\z!, "") self.basename = name[0..-ext.length - 1].gsub(%r!\.*\z!, "")
end end
@ -147,7 +149,7 @@ module Jekyll
# The path to the page source file, relative to the site source # The path to the page source file, relative to the site source
def relative_path def relative_path
@relative_path ||= File.join(*[@dir, @name].map(&:to_s).reject(&:empty?)).sub(%r!\A/!, "") @relative_path ||= PathManager.join(@dir, @name).sub(%r!\A/!, "")
end end
# Obtain destination path. # Obtain destination path.

View File

@ -28,6 +28,21 @@ class TestPageWithoutAFile < JekyllUnitTest
)) ))
end end
should "have non-frozen path and relative_path attributes" do
{
["foo", "bar.md"] => "foo/bar.md",
[nil, nil] => "",
["", ""] => "",
["/lorem/", "/ipsum"] => "lorem/ipsum",
%w(lorem ipsum) => "lorem/ipsum",
}.each do |(dir, name), result|
page = PageWithoutAFile.new(@site, @site.source, dir, name)
assert_equal result, page.path
assert_equal result, page.relative_path
refute page.relative_path.frozen?
end
end
context "with default site configuration" do context "with default site configuration" do
setup do setup do
@page = setup_page("properties.html") @page = setup_page("properties.html")