Implement and test asset_hash ignore option

This commit is contained in:
Thomas Reynolds 2012-05-26 15:08:59 -07:00
parent 0788ebce15
commit 98fc81beef
5 changed files with 45 additions and 31 deletions

View file

@ -4,11 +4,12 @@ Feature: Assets get a file hash appended to their and references to them are upd
When I cd to "build" When I cd to "build"
Then the following files should exist: Then the following files should exist:
| index.html | | index.html |
| apple-touch-icon.png |
| images/100px-1242c368.png | | images/100px-1242c368.png |
| images/100px-5fd6fb90.jpg | | images/100px-5fd6fb90.jpg |
| images/100px-5fd6fb90.gif | | images/100px-5fd6fb90.gif |
| javascripts/application-1d8d5276.js | | javascripts/application-1d8d5276.js |
| stylesheets/site-ad4a5abd.css | | stylesheets/site-50eaa978.css |
| index.html | | index.html |
| subdir/index.html | | subdir/index.html |
| other/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 | | stylesheets/site.css |
And the file "javascripts/application-1d8d5276.js" should contain "img.src = '/images/100px-5fd6fb90.jpg'" 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 "stylesheets/site-50eaa978.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 "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="javascripts/application-1d8d5276.js"'
And the file "index.html" should contain 'src="images/100px-5fd6fb90.jpg"' 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="../javascripts/application-1d8d5276.js"'
And the file "subdir/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"' 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="../javascripts/application-1d8d5276.js"'
And the file "other/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"' And the file "other/index.html" should contain 'src="../images/100px-5fd6fb90.jpg"'
Scenario: Hashed assets work in preview server Scenario: Hashed assets work in preview server
Given the Server is running at "asset-hash-app" Given the Server is running at "asset-hash-app"
When I go to "/" 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="javascripts/application-1d8d5276.js"'
And I should see 'src="images/100px-5fd6fb90.jpg"' And I should see 'src="images/100px-5fd6fb90.jpg"'
When I go to "/subdir/" 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="../javascripts/application-1d8d5276.js"'
And I should see 'src="../images/100px-5fd6fb90.jpg"' And I should see 'src="../images/100px-5fd6fb90.jpg"'
When I go to "/other/" 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="../javascripts/application-1d8d5276.js"'
And I should see 'src="../images/100px-5fd6fb90.jpg"' And I should see 'src="../images/100px-5fd6fb90.jpg"'
When I go to "/javascripts/application-1d8d5276.js" When I go to "/javascripts/application-1d8d5276.js"
Then I should see "img.src = '/images/100px-5fd6fb90.jpg'" 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')" Then I should see "background-image: url('../images/100px-5fd6fb90.jpg')"
Scenario: Enabling an asset host still produces hashed files and references Scenario: Enabling an asset host still produces hashed files and references
Given the Server is running at "asset-hash-host-app" Given the Server is running at "asset-hash-host-app"
When I go to "/" 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"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
When I go to "/subdir/" 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"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
When I go to "/other/" 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"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"'
# Asset helpers don't appear to work from Compass right now # Asset helpers don't appear to work from Compass right now
# When I go to "/stylesheets/site-e5a31a3e.css" # 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 font-size: 14px
""" """
When I go to "/partials/" 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 And the file "source/stylesheets/_partial.sass" has the contents
""" """
body body
font-size: 18px !important font-size: 18px !important
""" """
When I go to "/partials/" 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'

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 B

View file

@ -5,6 +5,9 @@
<h2>Image url:</h2> <h2>Image url:</h2>
<img src="<%= image_path('100px.jpg') %>"> <img src="<%= image_path('100px.jpg') %>">
<h2>Ignored path:</h2>
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<h2>Autogenerated tests:</h2> <h2>Autogenerated tests:</h2>
<p>images/100px.jpg</p> <p>images/100px.jpg</p>
<p>/images/100px.jpg</p> <p>/images/100px.jpg</p>

View file

@ -43,6 +43,9 @@ module Middleman
# Default output style # Default output style
config.output_style = :nested 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) if respond_to?(:asset_host) && asset_host.is_a?(Proc)
config.asset_host(&asset_host) config.asset_host(&asset_host)
end end

View file

@ -14,7 +14,7 @@ module Middleman
:asset_hash, :asset_hash,
AssetHashManager.new(self, exts, ignore) AssetHashManager.new(self, exts, ignore)
) )
use Middleware, :exts => exts, :middleman_app => self use Middleware, :exts => exts, :middleman_app => self, :ignore => ignore
end end
end end
alias :included :registered alias :included :registered
@ -31,7 +31,9 @@ module Middleman
# @return [void] # @return [void]
def manipulate_resource_list(resources) def manipulate_resource_list(resources)
resources.each do |resource| resources.each do |resource|
if @exts.include? resource.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 if resource.template? # if it's a template, render it out
digest = Digest::SHA1.hexdigest(resource.render)[0..7] digest = Digest::SHA1.hexdigest(resource.render)[0..7]
else # if it's a static file, just hash it else # if it's a static file, just hash it
@ -42,7 +44,6 @@ module Middleman
end end
end end
end end
end
# The asset hash middleware is responsible for rewriting references to # The asset hash middleware is responsible for rewriting references to
# assets to include their new, hashed name. # assets to include their new, hashed name.
@ -50,6 +51,7 @@ module Middleman
def initialize(app, options={}) def initialize(app, options={})
@rack_app = app @rack_app = app
@exts = options[:exts] @exts = options[:exts]
@ignore = options[:ignore]
@exts_regex_text = @exts.map {|e| Regexp.escape(e) }.join('|') @exts_regex_text = @exts.map {|e| Regexp.escape(e) }.join('|')
@middleman_app = options[:middleman_app] @middleman_app = options[:middleman_app]
end end
@ -66,16 +68,20 @@ module Middleman
if body if body
# TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK? # 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| body.gsub! /([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/ do |match|
opening_character = $1
asset_path = $2 asset_path = $2
relative_path = Pathname.new(asset_path).relative? relative_path = Pathname.new(asset_path).relative?
asset_path = dirpath.join(asset_path).to_s if relative_path 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 = "/#{asset_page.destination_path}"
replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path
"#{$1}#{replacement_path}" "#{opening_character}#{replacement_path}"
else else
match match
end end