From 44822a252e2ce1142e3293f91285e0ced3ba2fe1 Mon Sep 17 00:00:00 2001 From: xiaojian cai Date: Sun, 6 Jan 2013 21:23:31 +0800 Subject: [PATCH 1/7] add regexp support for option 'include','exclude' --- lib/jekyll/site.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 7dbf1336..423d2e91 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -22,8 +22,8 @@ module Jekyll self.lsi = config['lsi'] self.pygments = config['pygments'] self.permalink_style = config['permalink'].to_sym - self.exclude = config['exclude'] || [] - self.include = config['include'] || [] + self.exclude = (config['exclude'] || []).map { |e| Regexp.new("^#{e}$") } + self.include = (config['include'] || []).map { |e| Regexp.new("^#{e}$") } self.future = config['future'] self.limit_posts = config['limit_posts'] || nil @@ -318,15 +318,19 @@ module Jekyll # Returns the Array of filtered entries. def filter_entries(entries) entries.reject do |e| - unless self.include.include?(e) + unless regexp_include?(self.include, e) ['.', '_', '#'].include?(e[0..0]) || e[-1..-1] == '~' || - self.exclude.include?(e) || + regexp_include?(self.exclude, e) || File.symlink?(e) end end end + def regexp_include?(exps, e) + !(exps.index { |exp| !exp.match(e).nil? }).nil? + end + # Get the implementation class for the given Converter. # # klass - The Class of the Converter to fetch. From 1e892678bcd01f419e59bd090d66148cc43ced0b Mon Sep 17 00:00:00 2001 From: xiaojian cai Date: Wed, 9 Jan 2013 16:06:43 +0800 Subject: [PATCH 2/7] Update lib/jekyll/site.rb --- lib/jekyll/site.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 423d2e91..b20c18c5 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -22,8 +22,8 @@ module Jekyll self.lsi = config['lsi'] self.pygments = config['pygments'] self.permalink_style = config['permalink'].to_sym - self.exclude = (config['exclude'] || []).map { |e| Regexp.new("^#{e}$") } - self.include = (config['include'] || []).map { |e| Regexp.new("^#{e}$") } + self.exclude = config['exclude'] || [] + self.include = config['include'] || [] self.future = config['future'] self.limit_posts = config['limit_posts'] || nil @@ -318,17 +318,17 @@ module Jekyll # Returns the Array of filtered entries. def filter_entries(entries) entries.reject do |e| - unless regexp_include?(self.include, e) + unless glob_include?(self.include, e) ['.', '_', '#'].include?(e[0..0]) || e[-1..-1] == '~' || - regexp_include?(self.exclude, e) || + glob_include?(self.exclude, e) || File.symlink?(e) end end end - def regexp_include?(exps, e) - !(exps.index { |exp| !exp.match(e).nil? }).nil? + def glob_include?(exps, e) + !(exps.index { |exp| File.fnmatch?(exp, e) }).nil? end # Get the implementation class for the given Converter. From 655cf3b3a8e4738d5bd3338b8158604360da3e84 Mon Sep 17 00:00:00 2001 From: xiaojian cai Date: Wed, 9 Jan 2013 16:23:58 +0800 Subject: [PATCH 3/7] Update test/test_site.rb --- test/test_site.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_site.rb b/test/test_site.rb index 03f1886e..fe4fab49 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -159,11 +159,11 @@ class TestSite < Test::Unit::TestCase end should "not filter entries within include" do - includes = %w[_index.html .htaccess] - files = %w[index.html _index.html .htaccess] + includes = %w[_index.html .htaccess include*] + files = %w[index.html _index.html .htaccess includeA] @site.include = includes - assert_equal files, @site.filter_entries(files) + assert_equal files, @site.filter_entries(files + ["includeA"]) end context 'error handling' do From adba1017340097cd8585bbf537aa968943e49632 Mon Sep 17 00:00:00 2001 From: xiaojian cai Date: Wed, 9 Jan 2013 16:44:53 +0800 Subject: [PATCH 4/7] update test for include,exclude glob support --- test/test_site.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_site.rb b/test/test_site.rb index fe4fab49..a5ca035b 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -154,8 +154,8 @@ class TestSite < Test::Unit::TestCase excludes = %w[README TODO] files = %w[index.html site.css .htaccess] - @site.exclude = excludes - assert_equal files, @site.filter_entries(excludes + files) + @site.exclude = excludes + ["exclude*"] + assert_equal files, @site.filter_entries(excludes + files + ["excludeA"]) end should "not filter entries within include" do @@ -163,7 +163,7 @@ class TestSite < Test::Unit::TestCase files = %w[index.html _index.html .htaccess includeA] @site.include = includes - assert_equal files, @site.filter_entries(files + ["includeA"]) + assert_equal files, @site.filter_entries(files) end context 'error handling' do From c627cd35197584007d454142761a3843e6361ba5 Mon Sep 17 00:00:00 2001 From: xiaojian cai Date: Thu, 10 Jan 2013 09:16:09 +0800 Subject: [PATCH 5/7] use any? instead --- lib/jekyll/site.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index b20c18c5..bdab3237 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -328,7 +328,7 @@ module Jekyll end def glob_include?(exps, e) - !(exps.index { |exp| File.fnmatch?(exp, e) }).nil? + exps.any? { |exp| File.fnmatch?(exp, e) }) end # Get the implementation class for the given Converter. From b3e27f2c5d8f2a15b557f931bb4c0725d6246462 Mon Sep 17 00:00:00 2001 From: xiaojian cai Date: Thu, 10 Jan 2013 19:41:11 +0800 Subject: [PATCH 6/7] fixed a syntax error --- lib/jekyll/site.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index bdab3237..8c28ed89 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -328,7 +328,7 @@ module Jekyll end def glob_include?(exps, e) - exps.any? { |exp| File.fnmatch?(exp, e) }) + exps.any? { |exp| File.fnmatch?(exp, e) } end # Get the implementation class for the given Converter. From 3cf29eeee2148cd72c648988c56517627fad3309 Mon Sep 17 00:00:00 2001 From: mccxj Date: Fri, 11 Jan 2013 15:33:11 +0800 Subject: [PATCH 7/7] add Enumerable#glob_include? --- lib/jekyll/core_ext.rb | 8 ++++++++ lib/jekyll/site.rb | 8 ++------ test/test_core_ext.rb | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/jekyll/core_ext.rb b/lib/jekyll/core_ext.rb index fc40cb38..dfc5bbf7 100644 --- a/lib/jekyll/core_ext.rb +++ b/lib/jekyll/core_ext.rb @@ -50,3 +50,11 @@ class Date strftime("%Y-%m-%dT%H:%M:%S%Z") end if RUBY_VERSION < '1.9' end + +module Enumerable + # Returns true if path matches against any glob pattern. + # Look for more detail about glob pattern in method File::fnmatch. + def glob_include?(e) + any? { |exp| File.fnmatch?(exp, e) } + end +end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 8c28ed89..eb5f9f0d 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -318,19 +318,15 @@ module Jekyll # Returns the Array of filtered entries. def filter_entries(entries) entries.reject do |e| - unless glob_include?(self.include, e) + unless self.include.glob_include?(e) ['.', '_', '#'].include?(e[0..0]) || e[-1..-1] == '~' || - glob_include?(self.exclude, e) || + self.exclude.glob_include?(e) || File.symlink?(e) end end end - def glob_include?(exps, e) - exps.any? { |exp| File.fnmatch?(exp, e) } - end - # Get the implementation class for the given Converter. # # klass - The Class of the Converter to fetch. diff --git a/test/test_core_ext.rb b/test/test_core_ext.rb index c3e62cb5..0b79f33f 100644 --- a/test/test_core_ext.rb +++ b/test/test_core_ext.rb @@ -63,4 +63,26 @@ 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 + end + end end