From 3cf29eeee2148cd72c648988c56517627fad3309 Mon Sep 17 00:00:00 2001 From: mccxj Date: Fri, 11 Jan 2013 15:33:11 +0800 Subject: [PATCH] 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