diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 6e65e818..912c7ae3 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -96,13 +96,17 @@ module Middleman # to #match or #call, and returns whether or not the # given path matches that matcher. # - # @param matcher A matcher string/regexp/proc/etc - # @param path A path as a string + # @param [String, #match, #call] matcher A matcher String, RegExp, Proc, etc. + # @param [String] path A path as a string # @return [Boolean] Whether the path matches the matcher def path_match(matcher, path) - case + !!case when matcher.is_a?(String) - path.match(matcher) + if matcher.include? '*' + File.fnmatch(matcher, path) + else + path == matcher + end when matcher.respond_to?(:match) matcher.match(path) when matcher.respond_to?(:call) diff --git a/middleman-core/spec/middleman-core/path_match_spec.rb b/middleman-core/spec/middleman-core/path_match_spec.rb new file mode 100644 index 00000000..6bba18e9 --- /dev/null +++ b/middleman-core/spec/middleman-core/path_match_spec.rb @@ -0,0 +1,37 @@ +require 'middleman-core/util' + +describe "Middleman::Util#path_match" do + it "matches a literal string" do + expect(Middleman::Util.path_match '/index.html', '/index.html').to be true + end + + it "won't match a wrong string" do + expect(Middleman::Util.path_match '/foo.html', '/index.html').to be false + end + + it "won't match a partial string" do + expect(Middleman::Util.path_match 'ind', '/index.html').to be false + end + + it "works with a regex" do + expect(Middleman::Util.path_match /\.html$/, '/index.html').to be true + expect(Middleman::Util.path_match /\.js$/, '/index.html').to be false + end + + it "works with a proc" do + matcher = lambda {|p| p.length > 5 } + + expect(Middleman::Util.path_match matcher, '/index.html').to be true + expect(Middleman::Util.path_match matcher, '/i').to be false + end + + it "works with globs" do + expect(Middleman::Util.path_match '/foo/*.html', '/foo/index.html').to be true + expect(Middleman::Util.path_match '/foo/*.html', '/foo/index.js').to be false + expect(Middleman::Util.path_match '/bar/*.html', '/foo/index.js').to be false + + expect(Middleman::Util.path_match '/foo/*', '/foo/bar/index.html').to be true + expect(Middleman::Util.path_match '/foo/**/*', '/foo/bar/index.html').to be true + expect(Middleman::Util.path_match '/foo/**', '/foo/bar/index.html').to be true + end +end