Move #glob_include? from core_ext to entry_filter

This commit is contained in:
Parker Moore 2014-01-08 20:24:21 -08:00
parent 3d67cdc150
commit 331c7adc08
4 changed files with 52 additions and 48 deletions

View File

@ -53,21 +53,3 @@ class Hash
dup.symbolize_keys!
end
end
module Enumerable
def ensure_leading_slash(path)
path[0..0] == "/" ? path : "/#{path}"
end
# Returns true if path matches against any glob pattern.
# Look for more detail about glob pattern in method File::fnmatch.
def glob_include?(e)
entry = ensure_leading_slash(e)
any? do |exp|
item = ensure_leading_slash(exp)
Jekyll.logger.debug "glob_include?(#{entry})"
Jekyll.logger.debug " ==> File.fnmatch?(#{item}, #{entry}) == #{File.fnmatch?(item, entry)}"
File.fnmatch?(item, entry)
end
end
end

View File

@ -31,7 +31,7 @@ module Jekyll
end
def included?(entry)
site.include.glob_include?(entry)
glob_include?(site.include, entry)
end
def special?(entry)
@ -43,7 +43,7 @@ module Jekyll
end
def excluded?(entry)
excluded = site.exclude.glob_include?(relative_to_source(entry))
excluded = glob_include?(site.exclude, relative_to_source(entry))
Jekyll.logger.debug "excluded?(#{relative_to_source(entry)}) ==> #{excluded}"
excluded
end
@ -51,5 +51,21 @@ module Jekyll
def symlink?(entry)
File.symlink?(entry) && site.safe
end
def ensure_leading_slash(path)
path[0..0] == "/" ? path : "/#{path}"
end
# Returns true if path matches against any glob pattern.
# Look for more detail about glob pattern in method File::fnmatch.
def glob_include?(enum, e)
entry = ensure_leading_slash(e)
enum.any? do |exp|
item = ensure_leading_slash(exp)
Jekyll.logger.debug "glob_include?(#{entry})"
Jekyll.logger.debug " ==> File.fnmatch?(#{item}, #{entry}) == #{File.fnmatch?(item, entry)}"
File.fnmatch?(item, entry)
end
end
end
end

View File

@ -63,32 +63,4 @@ class TestCoreExt < Test::Unit::TestCase
end
end
context "enumerable" do
context "glob_include?" do
should "return false with no glob patterns" do
assert ![].glob_include?("a.txt")
end
should "return false with all not match path" do
data = ["a*", "b?"]
assert !data.glob_include?("ca.txt")
assert !data.glob_include?("ba.txt")
end
should "return true with match path" do
data = ["a*", "b?", "**/a*"]
assert data.glob_include?("a.txt")
assert data.glob_include?("ba")
assert data.glob_include?("c/a/a.txt")
assert data.glob_include?("c/a/b/a.txt")
end
should "match even if there is no leading slash" do
data = ['vendor/bundle']
assert data.glob_include?('/vendor/bundle')
assert data.glob_include?('vendor/bundle')
end
end
end
end

View File

@ -71,4 +71,38 @@ class TestEntryFilter < Test::Unit::TestCase
assert_not_equal [], site.static_files
end
end
context "glob_include?" do
setup do
stub(Jekyll).configuration do
Jekyll::Configuration::DEFAULTS.merge({'source' => source_dir, 'destination' => dest_dir})
end
@site = Site.new(Jekyll.configuration)
@filter = EntryFilter.new(@site)
end
should "return false with no glob patterns" do
assert !@filter.glob_include?([], "a.txt")
end
should "return false with all not match path" do
data = ["a*", "b?"]
assert !@filter.glob_include?(data, "ca.txt")
assert !@filter.glob_include?(data, "ba.txt")
end
should "return true with match path" do
data = ["a*", "b?", "**/a*"]
assert @filter.glob_include?(data, "a.txt")
assert @filter.glob_include?(data, "ba")
assert @filter.glob_include?(data, "c/a/a.txt")
assert @filter.glob_include?(data, "c/a/b/a.txt")
end
should "match even if there is no leading slash" do
data = ['vendor/bundle']
assert @filter.glob_include?(data, '/vendor/bundle')
assert @filter.glob_include?(data, 'vendor/bundle')
end
end
end