From 3879be0f23a7f999a184ce12001b4694e589cd38 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Mon, 2 Jun 2014 15:56:07 -0700 Subject: [PATCH] Add proc as a means of defining a rewriter ignore. Closes #1289 --- middleman-core/features/asset_hash.feature | 11 ++++++++--- .../middleware/inline_url_rewriter.rb | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/middleman-core/features/asset_hash.feature b/middleman-core/features/asset_hash.feature index 18abf3f9..27362377 100644 --- a/middleman-core/features/asset_hash.feature +++ b/middleman-core/features/asset_hash.feature @@ -109,7 +109,12 @@ Feature: Assets get a file hash appended to their and references to them are upd Given a fixture app "asset-hash-app" And a file named "config.rb" with: """ - activate :asset_hash, ignore: [%r(javascripts/*), 'images/*'] + is_stylesheet = proc { |path| path.start_with? 'stylesheets' } + activate :asset_hash, ignore: [ + %r(javascripts/*), + 'images/*', + is_stylesheet + ] activate :relative_assets activate :directory_indexes """ @@ -123,7 +128,7 @@ Feature: Assets get a file hash appended to their and references to them are upd | images/100px.jpg | | images/100px.gif | | javascripts/application.js | - | stylesheets/site-50eaa978.css | + | stylesheets/site.css | | index.html | | subdir/index.html | | other/index.html | @@ -132,7 +137,7 @@ Feature: Assets get a file hash appended to their and references to them are upd | images/100px-5fd6fb90.jpg | | images/100px-5fd6fb90.gif | | javascripts/application-1d8d5276.js | - | stylesheets/site.css | + | stylesheets/site-50eaa978.css | # @wip Currently broken, we should move all asset-host functionality out of Compass and into something more similar to asset_hash with Rack-based rewrites # Scenario: Enabling an asset host and referencing assets in CSS with URL fragments are rewritten correctly diff --git a/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb b/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb index 6b12f23e..4617b07d 100644 --- a/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb +++ b/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb @@ -49,7 +49,7 @@ module Middleman asset_path end - @ignore.none? { |r| full_asset_path.match(r) } && @proc.call(asset_path, dirpath) + @ignore.none? { |r| should_ignore?(r, full_asset_path) } && @proc.call(asset_path, dirpath) end status, headers, response = ::Rack::Response.new( @@ -62,6 +62,21 @@ module Middleman [status, headers, response] end + + def should_ignore?(validator, value) + if validator.is_a? Regexp + # Treat as Regexp + value.match(validator) + elsif validator.respond_to? :call + # Treat as proc + validator.call(value) + elsif validator.is_a? String + # Treat as glob + File.fnmatch(value, validator) + else + # If some unknown thing, don't ignore + end + end end end end