Refactor `EntryFilter#glob_include?`

This commit is contained in:
Ashwin Maroli 2019-06-09 15:35:39 +05:30
parent 13d31c4c8b
commit 3002aa58f5
2 changed files with 19 additions and 37 deletions

View File

@ -86,48 +86,24 @@ module Jekyll
)
end
# --
# Check if an entry matches a specific pattern and return true,false.
# Returns true if path matches against any glob pattern.
# --
def glob_include?(enum, entry)
entry_path = source_path.join(entry)
enum.any? do |exp|
# Users who send a Regexp knows what they want to
# exclude, so let them send a Regexp to exclude files,
# we will not bother caring if it works or not, it's
# on them at this point.
# Check if an entry matches a specific pattern.
# Returns true if path matches against any glob pattern, else false.
def glob_include?(enumerator, entry)
entry_with_source = site.in_source_dir(entry)
if exp.is_a?(Regexp)
entry_path =~ exp
enumerator.any? do |pattern|
case pattern
when String
pattern_with_source = site.in_source_dir(pattern)
File.fnmatch?(pattern_with_source, entry_with_source) ||
entry_with_source.start_with?(pattern_with_source)
when Regexp
pattern.match?(entry_with_source)
else
item = source_path.join(exp)
# If it's a directory they want to exclude, AKA
# ends with a "/" then we will go on to check and
# see if the entry falls within that path and
# exclude it if that's the case.
if entry.end_with?("/")
entry_path.in_path?(
item
)
else
File.fnmatch?(item, entry_path) ||
entry_path.to_path.start_with?(
item
)
end
false
end
end
end
private
def source_path
@source_path ||= Pathutil.new(site.in_source_dir)
end
end
end

View File

@ -147,5 +147,11 @@ class TestEntryFilter < JekyllUnitTest
assert @filter.glob_include?(data, "/vendor/bundle")
assert @filter.glob_include?(data, "vendor/bundle")
end
should "match even if there is no trailing slash" do
data = ["/vendor/bundle/", "vendor/ruby"]
assert @filter.glob_include?(data, "vendor/bundle/jekyll/lib/page.rb")
assert @filter.glob_include?(data, "/vendor/ruby/lib/set.rb")
end
end
end