From b794961916565a259c89a0b52d044f7c3c9d8405 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Tue, 12 Apr 2016 12:53:50 -0700 Subject: [PATCH] Only rewrite urls in sitemap. Fixes #1873 --- Gemfile | 4 +--- middleman-core/features/asset_hash.feature | 22 ++++++++++--------- .../asset-host-app/source/images/blank0.gif | 0 .../asset-host-app/source/images/blank1.gif | 0 .../asset-host-app/source/images/blank10.gif | 0 .../asset-host-app/source/images/blank100.gif | 0 .../asset-host-app/source/images/blank101.gif | 0 .../source/images/blank1010.gif | 0 .../asset-host-app/source/images/blank102.gif | 0 .../source/images/blank1020.gif | 0 .../source/images/blank1021.gif | 0 .../source/images/blank1022.gif | 0 .../source/images/blank1023.gif | 0 .../source/images/blank1024.gif | 0 .../asset-host-app/source/images/blank103.gif | 0 .../source/images/blank1030.gif | 0 .../source/images/blank1031.gif | 0 .../source/images/blank1032.gif | 0 .../source/images/blank1033.gif | 0 .../source/images/blank1034.gif | 0 .../asset-host-app/source/images/blank104.gif | 0 .../source/images/blank1043.gif | 0 .../source/images/blank1054.gif | 0 .../asset-host-app/source/images/blank2.gif | 0 .../asset-host-app/source/images/blank20.gif | 0 .../asset-host-app/source/images/blank21.gif | 0 .../asset-host-app/source/images/blank22.gif | 0 .../asset-host-app/source/images/blank23.gif | 0 .../asset-host-app/source/images/blank24.gif | 0 .../asset-host-app/source/images/blank3.gif | 0 .../asset-host-app/source/images/blank30.gif | 0 .../asset-host-app/source/images/blank31.gif | 0 .../asset-host-app/source/images/blank32.gif | 0 .../asset-host-app/source/images/blank33.gif | 0 .../asset-host-app/source/images/blank34.gif | 0 .../asset-host-app/source/images/blank4.gif | 0 .../asset-host-app/source/images/blank43.gif | 0 .../asset-host-app/source/images/blank54.gif | 0 .../core_extensions/inline_url_rewriter.rb | 2 +- .../lib/middleman-core/util/rack.rb | 18 +++++++++++---- 40 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank0.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank10.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank100.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank101.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1010.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank102.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1020.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1021.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1022.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1023.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1024.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank103.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1030.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1031.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1032.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1033.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1034.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank104.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1043.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank1054.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank2.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank20.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank21.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank22.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank23.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank24.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank3.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank30.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank31.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank32.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank33.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank34.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank4.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank43.gif create mode 100644 middleman-core/fixtures/asset-host-app/source/images/blank54.gif diff --git a/Gemfile b/Gemfile index b446cdcd..f14eb3b1 100644 --- a/Gemfile +++ b/Gemfile @@ -5,9 +5,7 @@ gem 'rake', '~> 10.3', require: false gem 'yard', '~> 0.8', require: false # Test tools -gem 'pry', '~> 0.10', group: :development, require: false -gem 'pry-byebug' -gem 'pry-stack_explorer' +gem 'byebug' gem 'aruba', '~> 0.7.4', require: false gem 'rspec', '~> 3.0', require: false gem 'cucumber', '~> 2.0', require: false diff --git a/middleman-core/features/asset_hash.feature b/middleman-core/features/asset_hash.feature index 6a353370..19517e3b 100644 --- a/middleman-core/features/asset_hash.feature +++ b/middleman-core/features/asset_hash.feature @@ -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" And a file named "config.rb" with: """ + set :sass_source_maps, false activate :asset_hash activate :directory_indexes activate :asset_host, host: 'http://middlemanapp.com' """ Given the Server is running at "asset-hash-host-app" 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/fragment-7000b132.css"' + Then I should see 'href="http://middlemanapp.com/stylesheets/site-7474cadd.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?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/" - 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"' 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?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?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" And a file named "config.rb" with: """ + set :sass_source_maps, false activate :asset_host, host: 'http://middlemanapp.com' activate :directory_indexes activate :asset_hash """ Given the Server is running at "asset-hash-host-app" 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/fragment-7000b132.css"' + Then I should see 'href="http://middlemanapp.com/stylesheets/site-7474cadd.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?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/" - 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"' 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?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?test")' And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test")' diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank0.gif b/middleman-core/fixtures/asset-host-app/source/images/blank0.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank10.gif b/middleman-core/fixtures/asset-host-app/source/images/blank10.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank100.gif b/middleman-core/fixtures/asset-host-app/source/images/blank100.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank101.gif b/middleman-core/fixtures/asset-host-app/source/images/blank101.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1010.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1010.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank102.gif b/middleman-core/fixtures/asset-host-app/source/images/blank102.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1020.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1020.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1021.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1021.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1022.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1022.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1023.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1023.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1024.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1024.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank103.gif b/middleman-core/fixtures/asset-host-app/source/images/blank103.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1030.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1030.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1031.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1031.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1032.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1032.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1033.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1033.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1034.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1034.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank104.gif b/middleman-core/fixtures/asset-host-app/source/images/blank104.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1043.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1043.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank1054.gif b/middleman-core/fixtures/asset-host-app/source/images/blank1054.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank2.gif b/middleman-core/fixtures/asset-host-app/source/images/blank2.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank20.gif b/middleman-core/fixtures/asset-host-app/source/images/blank20.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank21.gif b/middleman-core/fixtures/asset-host-app/source/images/blank21.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank22.gif b/middleman-core/fixtures/asset-host-app/source/images/blank22.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank23.gif b/middleman-core/fixtures/asset-host-app/source/images/blank23.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank24.gif b/middleman-core/fixtures/asset-host-app/source/images/blank24.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank3.gif b/middleman-core/fixtures/asset-host-app/source/images/blank3.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank30.gif b/middleman-core/fixtures/asset-host-app/source/images/blank30.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank31.gif b/middleman-core/fixtures/asset-host-app/source/images/blank31.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank32.gif b/middleman-core/fixtures/asset-host-app/source/images/blank32.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank33.gif b/middleman-core/fixtures/asset-host-app/source/images/blank33.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank34.gif b/middleman-core/fixtures/asset-host-app/source/images/blank34.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank4.gif b/middleman-core/fixtures/asset-host-app/source/images/blank4.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank43.gif b/middleman-core/fixtures/asset-host-app/source/images/blank43.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asset-host-app/source/images/blank54.gif b/middleman-core/fixtures/asset-host-app/source/images/blank54.gif new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/lib/middleman-core/core_extensions/inline_url_rewriter.rb b/middleman-core/lib/middleman-core/core_extensions/inline_url_rewriter.rb index 280c8db0..14cf4715 100644 --- a/middleman-core/lib/middleman-core/core_extensions/inline_url_rewriter.rb +++ b/middleman-core/lib/middleman-core/core_extensions/inline_url_rewriter.rb @@ -82,7 +82,7 @@ module Middleman dirpath = ::Pathname.new(File.dirname(path)) 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) relative_path = uri.host.nil? diff --git a/middleman-core/lib/middleman-core/util/rack.rb b/middleman-core/lib/middleman-core/util/rack.rb index f8003efe..0aeda851 100644 --- a/middleman-core/lib/middleman-core/util/rack.rb +++ b/middleman-core/lib/middleman-core/util/rack.rb @@ -20,8 +20,8 @@ module Middleman result end - Contract String, String, ArrayOf[String], Proc => String - def rewrite_paths(body, _path, exts, &_block) + Contract String, String, ArrayOf[String], IsA['::Middleman::Application'], Proc => String + def rewrite_paths(body, path, exts, app, &_block) matcher = /([\'\"\(,]\s*|# sourceMappingURL=)([^\s\'\"\)\(>]+(#{::Regexp.union(exts)}))/ url_fn_prefix = 'url(' @@ -35,11 +35,21 @@ module Middleman asset_path = asset_path[url_fn_prefix.length..-1] end + current_resource = app.sitemap.find_resource_by_destination_path(path) + begin uri = ::Addressable::URI.parse(asset_path) - if uri.relative? && uri.host.nil? && !(asset_path =~ /^[^\/].*[a-z]+\.[a-z]+\/.*/) && (result = yield(asset_path)) - "#{opening_character}#{result}" + 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}" + else + match + end else match end