Only rewrite urls in sitemap. Fixes #1873

This commit is contained in:
Thomas Reynolds 2016-04-12 12:53:50 -07:00
parent 4791e01f10
commit b794961916
40 changed files with 28 additions and 18 deletions

View file

@ -5,9 +5,7 @@ gem 'rake', '~> 10.3', require: false
gem 'yard', '~> 0.8', require: false gem 'yard', '~> 0.8', require: false
# Test tools # Test tools
gem 'pry', '~> 0.10', group: :development, require: false gem 'byebug'
gem 'pry-byebug'
gem 'pry-stack_explorer'
gem 'aruba', '~> 0.7.4', require: false gem 'aruba', '~> 0.7.4', require: false
gem 'rspec', '~> 3.0', require: false gem 'rspec', '~> 3.0', require: false
gem 'cucumber', '~> 2.0', require: false gem 'cucumber', '~> 2.0', require: false

View file

@ -111,28 +111,29 @@ Feature: Assets get file hashes appended to them and references to them are upda
Given a fixture app "asset-hash-host-app" Given a fixture app "asset-hash-host-app"
And a file named "config.rb" with: And a file named "config.rb" with:
""" """
set :sass_source_maps, false
activate :asset_hash activate :asset_hash
activate :directory_indexes activate :directory_indexes
activate :asset_host, host: 'http://middlemanapp.com' activate :asset_host, host: 'http://middlemanapp.com'
""" """
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-210612a0.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-7474cadd.css"'
Then I should see 'href="http://middlemanapp.com/stylesheets/fragment-7000b132.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/fragment-2902933e.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"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"'
When I go to "/subdir/" When I go to "/subdir/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-210612a0.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-7474cadd.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-210612a0.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-7474cadd.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"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"'
When I go to "/stylesheets/fragment-7000b132.css" When I go to "/stylesheets/fragment-2902933e.css"
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg");' And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg");'
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?test");' And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?test");'
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test");' And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test");'
@ -142,28 +143,29 @@ Feature: Assets get file hashes appended to them and references to them are upda
Given a fixture app "asset-hash-host-app" Given a fixture app "asset-hash-host-app"
And a file named "config.rb" with: And a file named "config.rb" with:
""" """
set :sass_source_maps, false
activate :asset_host, host: 'http://middlemanapp.com' activate :asset_host, host: 'http://middlemanapp.com'
activate :directory_indexes activate :directory_indexes
activate :asset_hash activate :asset_hash
""" """
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-210612a0.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-7474cadd.css"'
Then I should see 'href="http://middlemanapp.com/stylesheets/fragment-7000b132.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/fragment-2902933e.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"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"'
When I go to "/subdir/" When I go to "/subdir/"
Then I should see 'href="http://middlemanapp.com/stylesheets/site-210612a0.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-7474cadd.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-210612a0.css"' Then I should see 'href="http://middlemanapp.com/stylesheets/site-7474cadd.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"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"'
And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"'
When I go to "/stylesheets/fragment-7000b132.css" When I go to "/stylesheets/fragment-2902933e.css"
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg")' And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg")'
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?test")' And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?test")'
And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test")' And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test")'

View file

@ -82,7 +82,7 @@ module Middleman
dirpath = ::Pathname.new(File.dirname(path)) dirpath = ::Pathname.new(File.dirname(path))
rewritten = ::Middleman::Util.instrument 'inline_url_rewriter', path: path do rewritten = ::Middleman::Util.instrument 'inline_url_rewriter', path: path do
::Middleman::Util.rewrite_paths(body, path, all_asset_exts) do |asset_path| ::Middleman::Util.rewrite_paths(body, path, all_asset_exts, @middleman_app) do |asset_path|
uri = ::Addressable::URI.parse(asset_path) uri = ::Addressable::URI.parse(asset_path)
relative_path = uri.host.nil? relative_path = uri.host.nil?

View file

@ -20,8 +20,8 @@ module Middleman
result result
end end
Contract String, String, ArrayOf[String], Proc => String Contract String, String, ArrayOf[String], IsA['::Middleman::Application'], Proc => String
def rewrite_paths(body, _path, exts, &_block) def rewrite_paths(body, path, exts, app, &_block)
matcher = /([\'\"\(,]\s*|# sourceMappingURL=)([^\s\'\"\)\(>]+(#{::Regexp.union(exts)}))/ matcher = /([\'\"\(,]\s*|# sourceMappingURL=)([^\s\'\"\)\(>]+(#{::Regexp.union(exts)}))/
url_fn_prefix = 'url(' url_fn_prefix = 'url('
@ -35,14 +35,24 @@ module Middleman
asset_path = asset_path[url_fn_prefix.length..-1] asset_path = asset_path[url_fn_prefix.length..-1]
end end
current_resource = app.sitemap.find_resource_by_destination_path(path)
begin begin
uri = ::Addressable::URI.parse(asset_path) uri = ::Addressable::URI.parse(asset_path)
if uri.relative? && uri.host.nil? && !(asset_path =~ /^[^\/].*[a-z]+\.[a-z]+\/.*/) && (result = yield(asset_path)) if uri.relative? && uri.host.nil? && !(asset_path =~ /^[^\/].*[a-z]+\.[a-z]+\/.*/)
dest_path = ::Middleman::Util.url_for(app, asset_path, relative: false, current_resource: current_resource)
resource = app.sitemap.find_resource_by_destination_path(dest_path)
if resource && (result = yield(asset_path))
"#{opening_character}#{result}" "#{opening_character}#{result}"
else else
match match
end end
else
match
end
rescue ::Addressable::URI::InvalidURIError rescue ::Addressable::URI::InvalidURIError
match match
end end