From 98fc81beef4e72c5ff89b3498118e65e2f414b9e Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 26 May 2012 15:08:59 -0700 Subject: [PATCH] Implement and test asset_hash ignore option --- middleman-more/features/asset_hash.feature | 32 ++++++++------- .../source/apple-touch-icon.png | Bin 0 -> 216 bytes .../asset-hash-app/source/index.html.erb | 3 ++ .../middleman-more/core_extensions/compass.rb | 3 ++ .../middleman-more/extensions/asset_hash.rb | 38 ++++++++++-------- 5 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 middleman-more/fixtures/asset-hash-app/source/apple-touch-icon.png diff --git a/middleman-more/features/asset_hash.feature b/middleman-more/features/asset_hash.feature index 6764e10f..95459245 100644 --- a/middleman-more/features/asset_hash.feature +++ b/middleman-more/features/asset_hash.feature @@ -4,11 +4,12 @@ Feature: Assets get a file hash appended to their and references to them are upd When I cd to "build" Then the following files should exist: | index.html | + | apple-touch-icon.png | | images/100px-1242c368.png | | images/100px-5fd6fb90.jpg | | images/100px-5fd6fb90.gif | | javascripts/application-1d8d5276.js | - | stylesheets/site-ad4a5abd.css | + | stylesheets/site-50eaa978.css | | index.html | | subdir/index.html | | other/index.html | @@ -20,46 +21,48 @@ Feature: Assets get a file hash appended to their and references to them are upd | stylesheets/site.css | And the file "javascripts/application-1d8d5276.js" should contain "img.src = '/images/100px-5fd6fb90.jpg'" - And the file "stylesheets/site-ad4a5abd.css" should contain "background-image: url('../images/100px-5fd6fb90.jpg')" - And the file "index.html" should contain 'href="stylesheets/site-ad4a5abd.css"' + And the file "stylesheets/site-50eaa978.css" should contain "background-image: url('../images/100px-5fd6fb90.jpg')" + And the file "index.html" should contain 'href="apple-touch-icon.png"' + And the file "index.html" should contain 'href="stylesheets/site-50eaa978.css"' And the file "index.html" should contain 'src="javascripts/application-1d8d5276.js"' And the file "index.html" should contain 'src="images/100px-5fd6fb90.jpg"' - And the file "subdir/index.html" should contain 'href="../stylesheets/site-ad4a5abd.css"' + And the file "subdir/index.html" should contain 'href="../stylesheets/site-50eaa978.css"' And the file "subdir/index.html" should contain 'src="../javascripts/application-1d8d5276.js"' And the file "subdir/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"' - And the file "other/index.html" should contain 'href="../stylesheets/site-ad4a5abd.css"' + And the file "other/index.html" should contain 'href="../stylesheets/site-50eaa978.css"' And the file "other/index.html" should contain 'src="../javascripts/application-1d8d5276.js"' And the file "other/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"' Scenario: Hashed assets work in preview server Given the Server is running at "asset-hash-app" When I go to "/" - Then I should see 'href="stylesheets/site-ad4a5abd.css"' + Then I should see 'href="apple-touch-icon.png"' + And I should see 'href="stylesheets/site-50eaa978.css"' And I should see 'src="javascripts/application-1d8d5276.js"' And I should see 'src="images/100px-5fd6fb90.jpg"' When I go to "/subdir/" - Then I should see 'href="../stylesheets/site-ad4a5abd.css"' + Then I should see 'href="../stylesheets/site-50eaa978.css"' And I should see 'src="../javascripts/application-1d8d5276.js"' And I should see 'src="../images/100px-5fd6fb90.jpg"' When I go to "/other/" - Then I should see 'href="../stylesheets/site-ad4a5abd.css"' + Then I should see 'href="../stylesheets/site-50eaa978.css"' And I should see 'src="../javascripts/application-1d8d5276.js"' And I should see 'src="../images/100px-5fd6fb90.jpg"' When I go to "/javascripts/application-1d8d5276.js" Then I should see "img.src = '/images/100px-5fd6fb90.jpg'" - When I go to "/stylesheets/site-ad4a5abd.css" + When I go to "/stylesheets/site-50eaa978.css" Then I should see "background-image: url('../images/100px-5fd6fb90.jpg')" Scenario: Enabling an asset host still produces hashed files and references Given the Server is running at "asset-hash-host-app" When I go to "/" - Then I should see 'href="http://middlemanapp.com/stylesheets/site-1bac75d8.css"' + Then I should see 'href="http://middlemanapp.com/stylesheets/site-171eb3c0.css"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' When I go to "/subdir/" - Then I should see 'href="http://middlemanapp.com/stylesheets/site-1bac75d8.css"' + Then I should see 'href="http://middlemanapp.com/stylesheets/site-171eb3c0.css"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' When I go to "/other/" - Then I should see 'href="http://middlemanapp.com/stylesheets/site-1bac75d8.css"' + Then I should see 'href="http://middlemanapp.com/stylesheets/site-171eb3c0.css"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' # Asset helpers don't appear to work from Compass right now # When I go to "/stylesheets/site-e5a31a3e.css" @@ -73,12 +76,11 @@ Feature: Assets get a file hash appended to their and references to them are upd font-size: 14px """ When I go to "/partials/" - Then I should see 'href="../stylesheets/uses_partials-b1ef0501.css' + Then I should see 'href="../stylesheets/uses_partials-423a00f7.css' And the file "source/stylesheets/_partial.sass" has the contents """ body font-size: 18px !important """ When I go to "/partials/" - Then I should see 'href="../stylesheets/uses_partials-05453ae6.css' - + Then I should see 'href="../stylesheets/uses_partials-e8c3d4eb.css' \ No newline at end of file diff --git a/middleman-more/fixtures/asset-hash-app/source/apple-touch-icon.png b/middleman-more/fixtures/asset-hash-app/source/apple-touch-icon.png new file mode 100644 index 0000000000000000000000000000000000000000..a78f68b8ee1b84dd22d7a4e2bd67e797bf164daf GIT binary patch literal 216 zcmeAS@N?(olHy`uVBq!ia0vp^DImVS)*E46%Oq(`s)Q*8e9aCbt{26*78VJx=x}*o7Tv&7 zW-wXoyxa|o_z66mjY+F^sGpDdc(jLwfuW&3bIQKGx$^FlrpvuexqWnFT;iJ?t)}je zkC*t*^__q7_>QtkQ+L$}?VIacxwSL6IX+&7P5yDn!)x_x=j*XCFdWd;RsSMg?3L;t Q0CWR`r>mdKI;Vst0C4(O@c;k- literal 0 HcmV?d00001 diff --git a/middleman-more/fixtures/asset-hash-app/source/index.html.erb b/middleman-more/fixtures/asset-hash-app/source/index.html.erb index 14d71d26..fe58ad66 100644 --- a/middleman-more/fixtures/asset-hash-app/source/index.html.erb +++ b/middleman-more/fixtures/asset-hash-app/source/index.html.erb @@ -5,6 +5,9 @@

Image url:

+

Ignored path:

+ +

Autogenerated tests:

images/100px.jpg

/images/100px.jpg

diff --git a/middleman-more/lib/middleman-more/core_extensions/compass.rb b/middleman-more/lib/middleman-more/core_extensions/compass.rb index a834ffb1..c89c6acf 100644 --- a/middleman-more/lib/middleman-more/core_extensions/compass.rb +++ b/middleman-more/lib/middleman-more/core_extensions/compass.rb @@ -42,6 +42,9 @@ module Middleman # Default output style config.output_style = :nested + + # No line-comments in test mode (changing paths mess with sha1) + config.line_comments = false if ENV["TEST"] if respond_to?(:asset_host) && asset_host.is_a?(Proc) config.asset_host(&asset_host) diff --git a/middleman-more/lib/middleman-more/extensions/asset_hash.rb b/middleman-more/lib/middleman-more/extensions/asset_hash.rb index 8cc5678f..d034024e 100755 --- a/middleman-more/lib/middleman-more/extensions/asset_hash.rb +++ b/middleman-more/lib/middleman-more/extensions/asset_hash.rb @@ -14,7 +14,7 @@ module Middleman :asset_hash, AssetHashManager.new(self, exts, ignore) ) - use Middleware, :exts => exts, :middleman_app => self + use Middleware, :exts => exts, :middleman_app => self, :ignore => ignore end end alias :included :registered @@ -31,15 +31,16 @@ module Middleman # @return [void] def manipulate_resource_list(resources) resources.each do |resource| - if @exts.include? resource.ext - if resource.template? # if it's a template, render it out - digest = Digest::SHA1.hexdigest(resource.render)[0..7] - else # if it's a static file, just hash it - digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7] - end - - resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" } + next unless @exts.include? resource.ext + next if @ignore.any? { |r| resource.destination_path.match(r) } + + if resource.template? # if it's a template, render it out + digest = Digest::SHA1.hexdigest(resource.render)[0..7] + else # if it's a static file, just hash it + digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7] end + + resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" } end end end @@ -48,10 +49,11 @@ module Middleman # assets to include their new, hashed name. class Middleware def initialize(app, options={}) - @rack_app = app - @exts = options[:exts] + @rack_app = app + @exts = options[:exts] + @ignore = options[:ignore] @exts_regex_text = @exts.map {|e| Regexp.escape(e) }.join('|') - @middleman_app = options[:middleman_app] + @middleman_app = options[:middleman_app] end def call(env) @@ -66,16 +68,20 @@ module Middleman if body # TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK? body.gsub! /([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/ do |match| + opening_character = $1 asset_path = $2 + relative_path = Pathname.new(asset_path).relative? asset_path = dirpath.join(asset_path).to_s if relative_path - - if asset_page = @middleman_app.sitemap.find_resource_by_path(asset_path) + + if @ignore.any? { |r| asset_path.match(r) } + match + elsif asset_page = @middleman_app.sitemap.find_resource_by_path(asset_path) replacement_path = "/#{asset_page.destination_path}" replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path - - "#{$1}#{replacement_path}" + + "#{opening_character}#{replacement_path}" else match end