diff --git a/middleman-cli/lib/middleman-cli/console.rb b/middleman-cli/lib/middleman-cli/console.rb index 829e9901..4e267539 100644 --- a/middleman-cli/lib/middleman-cli/console.rb +++ b/middleman-cli/lib/middleman-cli/console.rb @@ -29,13 +29,13 @@ module Middleman::Cli ::Middleman::Logger.singleton(opts[:debug] ? 0 : 1, opts[:instrumenting] || false) end - interact_with @app + self.class.interact_with @app end # Start an interactive console in the context of the provided object. # @param [Object] context # @return [void] - def interact_with(context) + def self.interact_with(context) IRB.setup nil IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context require 'irb/ext/multi-irb' diff --git a/middleman-core/fixtures/asset-hash-app/source/subdir/index.html.erb b/middleman-core/fixtures/asset-hash-app/source/subdir/index.html.erb index e9c2889c..79f12390 100644 --- a/middleman-core/fixtures/asset-hash-app/source/subdir/index.html.erb +++ b/middleman-core/fixtures/asset-hash-app/source/subdir/index.html.erb @@ -2,8 +2,17 @@
http://example.com/test.jpg
+<%= image_path('100px.jpg') %>
+ + +<%= image_tag('200px.jpg') %>
+ +images/100px.jpg
diff --git a/middleman-core/fixtures/asset-host-app/source/asset_host.html.erb b/middleman-core/fixtures/asset-host-app/source/asset_host.html.erb index 3d7be098..41490760 100755 --- a/middleman-core/fixtures/asset-host-app/source/asset_host.html.erb +++ b/middleman-core/fixtures/asset-host-app/source/asset_host.html.erb @@ -19,4 +19,24 @@ <%= image_tag "blank31.gif" %> <%= image_tag "blank32.gif" %> <%= image_tag "blank33.gif" %> -<%= image_tag "blank34.gif" %> \ No newline at end of file +<%= image_tag "blank34.gif" %> +<%= image_tag "blank100.gif" %> +<%= image_tag "blank101.gif" %> +<%= image_tag "blank102.gif" %> +<%= image_tag "blank103.gif" %> +<%= image_tag "blank104.gif" %> +<%= image_tag "blank1010.gif" %> +<%= image_tag "blank1021.gif" %> +<%= image_tag "blank1032.gif" %> +<%= image_tag "blank1043.gif" %> +<%= image_tag "blank1054.gif" %> +<%= image_tag "blank1020.gif" %> +<%= image_tag "blank1021.gif" %> +<%= image_tag "blank1022.gif" %> +<%= image_tag "blank1023.gif" %> +<%= image_tag "blank1024.gif" %> +<%= image_tag "blank1030.gif" %> +<%= image_tag "blank1031.gif" %> +<%= image_tag "blank1032.gif" %> +<%= image_tag "blank1033.gif" %> +<%= image_tag "blank1034.gif" %> \ No newline at end of file diff --git a/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb b/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb index e66c1739..71666bab 100644 --- a/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb +++ b/middleman-core/lib/middleman-core/core_extensions/default_helpers.rb @@ -221,12 +221,12 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension url = url_for(url, options) super end - + # Modified Padrino image_tag so that it finds the paths for srcset # using asset_path for the images listed in the srcset param def image_tag(path, params={}) params.symbolize_keys! - + if params.key?(:srcset) images_sources = params[:srcset].split(',').map do |src_def| if src_def.include?('//') diff --git a/middleman-core/lib/middleman-core/extensions/asset_hash.rb b/middleman-core/lib/middleman-core/extensions/asset_hash.rb index 39f1a069..07d58234 100644 --- a/middleman-core/lib/middleman-core/extensions/asset_hash.rb +++ b/middleman-core/lib/middleman-core/extensions/asset_hash.rb @@ -1,3 +1,4 @@ +require 'addressable/uri' require 'middleman-core/util' class Middleman::Extensions::AssetHash < ::Middleman::Extension @@ -9,7 +10,6 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension require 'digest/sha1' require 'rack/mock' - require 'uri' require 'middleman-core/middleware/inline_url_rewriter' end @@ -28,7 +28,8 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension Contract String, Or[String, Pathname], Any => Maybe[String] def rewrite_url(asset_path, dirpath, _request_path) - relative_path = Pathname.new(asset_path).relative? + uri = ::Addressable::URI.parse(asset_path) + relative_path = uri.path[0..0] != '/' full_asset_path = if relative_path dirpath.join(asset_path).to_s @@ -36,10 +37,11 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension asset_path end - return unless asset_page = app.sitemap.find_resource_by_path(full_asset_path) + return unless asset_page = app.sitemap.find_resource_by_destination_path(full_asset_path) || app.sitemap.find_resource_by_path(full_asset_path) replacement_path = "/#{asset_page.destination_path}" replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path + replacement_path end @@ -56,7 +58,7 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension # This is so by the time we get around to the text files (which may reference # images and fonts) the static assets' hashes are already calculated. resources.sort_by do |a| - if %w(.svg).include? a.ext + if %w(.svg .svgz).include? a.ext 0 elsif %w(.js .css).include? a.ext 1 @@ -73,9 +75,10 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension return if resource.ignored? # Render through the Rack interface so middleware and mounted apps get a shot - response = @rack_client.get(URI.escape(resource.destination_path), - 'bypass_inline_url_rewriter_asset_hash' => 'true' - ) + response = @rack_client.get( + URI.escape(resource.destination_path), + 'bypass_inline_url_rewriter_asset_hash' => 'true' + ) raise "#{resource.path} should be in the sitemap!" unless response.status == 200 @@ -87,6 +90,8 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension Contract IsA['Middleman::Sitemap::Resource'] => Bool def ignored_resource?(resource) - @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } + @ignore.any? do |ignore| + Middleman::Util.path_match(ignore, resource.destination_path) + end end end diff --git a/middleman-core/lib/middleman-core/extensions/asset_host.rb b/middleman-core/lib/middleman-core/extensions/asset_host.rb index be161501..79e1813d 100644 --- a/middleman-core/lib/middleman-core/extensions/asset_host.rb +++ b/middleman-core/lib/middleman-core/extensions/asset_host.rb @@ -1,3 +1,4 @@ +require 'addressable/uri' require 'middleman-core/middleware/inline_url_rewriter' class Middleman::Extensions::AssetHost < ::Middleman::Extension @@ -18,7 +19,8 @@ class Middleman::Extensions::AssetHost < ::Middleman::Extension Contract String, Or[String, Pathname], Any => String def rewrite_url(asset_path, dirpath, _request_path) - relative_path = Pathname.new(asset_path).relative? + uri = ::Addressable::URI.parse(asset_path) + relative_path = uri.path[0..0] != '/' full_asset_path = if relative_path dirpath.join(asset_path).to_s diff --git a/middleman-core/lib/middleman-core/extensions/relative_assets.rb b/middleman-core/lib/middleman-core/extensions/relative_assets.rb index 88c73af3..58ba20c3 100644 --- a/middleman-core/lib/middleman-core/extensions/relative_assets.rb +++ b/middleman-core/lib/middleman-core/extensions/relative_assets.rb @@ -1,3 +1,5 @@ +require 'addressable/uri' + # Relative Assets extension class Middleman::Extensions::RelativeAssets < ::Middleman::Extension option :exts, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif .ttf .otf .woff .woff2), 'List of extensions that get cache busters strings appended to them.' @@ -22,7 +24,11 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension Contract String, Or[String, Pathname], Any => Maybe[String] def rewrite_url(asset_path, dirpath, request_path) - relative_path = Pathname.new(asset_path).relative? + uri = ::Addressable::URI.parse(asset_path) + + return if uri.path[0..0] != '/' + + relative_path = uri.host.nil? full_asset_path = if relative_path dirpath.join(asset_path).to_s @@ -30,9 +36,9 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension asset_path end - return unless !full_asset_path.include?('//') && !asset_path.start_with?('data:') - current_dir = Pathname(request_path).dirname - Pathname(full_asset_path).relative_path_from(current_dir).to_s + result = Pathname(full_asset_path).relative_path_from(current_dir).to_s + + result end end 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 b1eddd11..34312060 100644 --- a/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb +++ b/middleman-core/lib/middleman-core/middleware/inline_url_rewriter.rb @@ -1,7 +1,8 @@ -require 'middleman-core/util' -require 'middleman-core/contracts' require 'rack' require 'rack/response' +require 'addressable/uri' +require 'middleman-core/util' +require 'middleman-core/contracts' module Middleman module Middleware @@ -51,10 +52,13 @@ module Middleman if path =~ /(^\/$)|(#{@source_exts_regex_text}$)/ if body = ::Middleman::Util.extract_response_text(response) + dirpath = Pathname.new(File.dirname(path)) rewritten = ::Middleman::Util.rewrite_paths(body, path, @exts) do |asset_path| - relative_path = Pathname.new(asset_path).relative? + uri = ::Addressable::URI.parse(asset_path) + + relative_path = uri.host.nil? full_asset_path = if relative_path dirpath.join(asset_path).to_s @@ -76,7 +80,7 @@ module Middleman [status, headers, response] end - Contract IGNORE_DESCRIPTOR => Bool + Contract IGNORE_DESCRIPTOR, String => Bool def should_ignore?(validator, value) if validator.is_a? Regexp # Treat as Regexp diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index debde48e..8a00eb9d 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -356,15 +356,10 @@ module Middleman opening_character = $1 asset_path = $2 - begin - uri = ::Addressable::URI.parse(asset_path) - - if uri.relative? && result = yield(asset_path) - "#{opening_character}#{result}" - else - match - end - rescue + uri = ::Addressable::URI.parse(asset_path) + if uri.relative? && result = yield(asset_path) + "#{opening_character}#{result}" + else match end end