From 57f5701e72ec9dd020578b7be7762ba506fdd61f Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 16 Jun 2012 20:23:33 -0700 Subject: [PATCH] Add a Middleman::Util.path_match function that handles matching on regexes, globs, and procs, and use it from asset_hash, minify_javascript, and minify_css. Fixes #480. --- middleman-core/lib/middleman-core/util.rb | 21 ++++++++++++++++++- .../middleman-more/extensions/asset_hash.rb | 2 +- .../middleman-more/extensions/minify_css.rb | 2 +- .../extensions/minify_javascript.rb | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 7d69dedc..4a0ef315 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -56,6 +56,25 @@ module Middleman end end + # Takes a matcher, which can be a literal string + # or a string containing glob expressions, or a + # regexp, or a proc, or anything else that responds + # 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 + # @return [Boolean] Whether the path matches the matcher + def self.path_match(matcher, path) + if matcher.respond_to? :match + matcher.match path + elsif matcher.respond_to? :call + matcher.call path + else + File.fnmatch(matcher.to_s, path) + end + end + # Simple shared cache implementation class Cache # Initialize @@ -113,4 +132,4 @@ module Middleman end end end -end \ No newline at end of file +end diff --git a/middleman-more/lib/middleman-more/extensions/asset_hash.rb b/middleman-more/lib/middleman-more/extensions/asset_hash.rb index fc3c0fbb..298f8151 100755 --- a/middleman-more/lib/middleman-more/extensions/asset_hash.rb +++ b/middleman-more/lib/middleman-more/extensions/asset_hash.rb @@ -32,7 +32,7 @@ module Middleman def manipulate_resource_list(resources) resources.each do |resource| next unless @exts.include? resource.ext - next if @ignore.any? { |r| resource.destination_path.match(r) } + next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } if resource.template? # if it's a template, render it out digest = Digest::SHA1.hexdigest(resource.render)[0..7] diff --git a/middleman-more/lib/middleman-more/extensions/minify_css.rb b/middleman-more/lib/middleman-more/extensions/minify_css.rb index e4eedcc0..fba21fb2 100644 --- a/middleman-more/lib/middleman-more/extensions/minify_css.rb +++ b/middleman-more/lib/middleman-more/extensions/minify_css.rb @@ -66,7 +66,7 @@ module Middleman headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s response = [minified] - elsif path.end_with?('.css') && @ignore.none? {|ignore| path =~ ignore } + elsif path.end_with?('.css') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) } uncompressed_source = ::Middleman::Util.extract_response_text(response) minified_css = @compressor.compress(uncompressed_source) diff --git a/middleman-more/lib/middleman-more/extensions/minify_javascript.rb b/middleman-more/lib/middleman-more/extensions/minify_javascript.rb index 1030a634..23cc5591 100755 --- a/middleman-more/lib/middleman-more/extensions/minify_javascript.rb +++ b/middleman-more/lib/middleman-more/extensions/minify_javascript.rb @@ -75,7 +75,7 @@ module Middleman headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s response = [minified] - elsif path.end_with?('.js') && @ignore.none? {|ignore| path =~ ignore } + elsif path.end_with?('.js') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) } uncompressed_source = ::Middleman::Util.extract_response_text(response) minified_js = @compressor.compress(uncompressed_source)