Refactor `EntryFilter#glob_include?`
This commit is contained in:
parent
13d31c4c8b
commit
3002aa58f5
|
@ -86,48 +86,24 @@ module Jekyll
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# --
|
# Check if an entry matches a specific pattern.
|
||||||
# Check if an entry matches a specific pattern and return true,false.
|
# Returns true if path matches against any glob pattern, else false.
|
||||||
# Returns true if path matches against any glob pattern.
|
def glob_include?(enumerator, entry)
|
||||||
# --
|
entry_with_source = site.in_source_dir(entry)
|
||||||
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.
|
|
||||||
|
|
||||||
if exp.is_a?(Regexp)
|
enumerator.any? do |pattern|
|
||||||
entry_path =~ exp
|
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
|
else
|
||||||
item = source_path.join(exp)
|
false
|
||||||
|
|
||||||
# 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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def source_path
|
|
||||||
@source_path ||= Pathutil.new(site.in_source_dir)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -147,5 +147,11 @@ class TestEntryFilter < JekyllUnitTest
|
||||||
assert @filter.glob_include?(data, "/vendor/bundle")
|
assert @filter.glob_include?(data, "/vendor/bundle")
|
||||||
assert @filter.glob_include?(data, "vendor/bundle")
|
assert @filter.glob_include?(data, "vendor/bundle")
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue