Fix up inline url confusion

This commit is contained in:
Thomas Reynolds 2015-04-26 13:01:19 -07:00
parent 2403fa2d81
commit b127283040
9 changed files with 73 additions and 32 deletions

View file

@ -29,13 +29,13 @@ module Middleman::Cli
::Middleman::Logger.singleton(opts[:debug] ? 0 : 1, opts[:instrumenting] || false) ::Middleman::Logger.singleton(opts[:debug] ? 0 : 1, opts[:instrumenting] || false)
end end
interact_with @app self.class.interact_with @app
end end
# Start an interactive console in the context of the provided object. # Start an interactive console in the context of the provided object.
# @param [Object] context # @param [Object] context
# @return [void] # @return [void]
def interact_with(context) def self.interact_with(context)
IRB.setup nil IRB.setup nil
IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context
require 'irb/ext/multi-irb' require 'irb/ext/multi-irb'

View file

@ -2,8 +2,17 @@
<title>The Middleman!</title> <title>The Middleman!</title>
<% end %> <% end %>
<h2>Abs</h2>
<p>http://example.com/test.jpg</p>
<h2>Image url:</h2> <h2>Image url:</h2>
<p><%= image_path('100px.jpg') %></p> <p><img src="<%= image_path('100px.jpg') %>"></p>
<h2>Image url2:</h2>
<p><%= image_tag('200px.jpg') %></p>
<h2>Image url3:</h2>
<p><img src="/images/100px.jpg"></p>
<h2>Autogenerated tests:</h2> <h2>Autogenerated tests:</h2>
<p>images/100px.jpg</p> <p>images/100px.jpg</p>

View file

@ -19,4 +19,24 @@
<%= image_tag "blank31.gif" %> <%= image_tag "blank31.gif" %>
<%= image_tag "blank32.gif" %> <%= image_tag "blank32.gif" %>
<%= image_tag "blank33.gif" %> <%= image_tag "blank33.gif" %>
<%= image_tag "blank34.gif" %> <%= 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" %>

View file

@ -221,12 +221,12 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
url = url_for(url, options) url = url_for(url, options)
super super
end end
# Modified Padrino image_tag so that it finds the paths for srcset # Modified Padrino image_tag so that it finds the paths for srcset
# using asset_path for the images listed in the srcset param # using asset_path for the images listed in the srcset param
def image_tag(path, params={}) def image_tag(path, params={})
params.symbolize_keys! params.symbolize_keys!
if params.key?(:srcset) if params.key?(:srcset)
images_sources = params[:srcset].split(',').map do |src_def| images_sources = params[:srcset].split(',').map do |src_def|
if src_def.include?('//') if src_def.include?('//')

View file

@ -1,3 +1,4 @@
require 'addressable/uri'
require 'middleman-core/util' require 'middleman-core/util'
class Middleman::Extensions::AssetHash < ::Middleman::Extension class Middleman::Extensions::AssetHash < ::Middleman::Extension
@ -9,7 +10,6 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
require 'digest/sha1' require 'digest/sha1'
require 'rack/mock' require 'rack/mock'
require 'uri'
require 'middleman-core/middleware/inline_url_rewriter' require 'middleman-core/middleware/inline_url_rewriter'
end end
@ -28,7 +28,8 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
Contract String, Or[String, Pathname], Any => Maybe[String] Contract String, Or[String, Pathname], Any => Maybe[String]
def rewrite_url(asset_path, dirpath, _request_path) 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 full_asset_path = if relative_path
dirpath.join(asset_path).to_s dirpath.join(asset_path).to_s
@ -36,10 +37,11 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
asset_path asset_path
end 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 = "/#{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
replacement_path replacement_path
end 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 # 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. # images and fonts) the static assets' hashes are already calculated.
resources.sort_by do |a| resources.sort_by do |a|
if %w(.svg).include? a.ext if %w(.svg .svgz).include? a.ext
0 0
elsif %w(.js .css).include? a.ext elsif %w(.js .css).include? a.ext
1 1
@ -73,9 +75,10 @@ class Middleman::Extensions::AssetHash < ::Middleman::Extension
return if resource.ignored? return if resource.ignored?
# Render through the Rack interface so middleware and mounted apps get a shot # Render through the Rack interface so middleware and mounted apps get a shot
response = @rack_client.get(URI.escape(resource.destination_path), response = @rack_client.get(
'bypass_inline_url_rewriter_asset_hash' => 'true' URI.escape(resource.destination_path),
) 'bypass_inline_url_rewriter_asset_hash' => 'true'
)
raise "#{resource.path} should be in the sitemap!" unless response.status == 200 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 Contract IsA['Middleman::Sitemap::Resource'] => Bool
def ignored_resource?(resource) 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
end end

View file

@ -1,3 +1,4 @@
require 'addressable/uri'
require 'middleman-core/middleware/inline_url_rewriter' require 'middleman-core/middleware/inline_url_rewriter'
class Middleman::Extensions::AssetHost < ::Middleman::Extension class Middleman::Extensions::AssetHost < ::Middleman::Extension
@ -18,7 +19,8 @@ class Middleman::Extensions::AssetHost < ::Middleman::Extension
Contract String, Or[String, Pathname], Any => String Contract String, Or[String, Pathname], Any => String
def rewrite_url(asset_path, dirpath, _request_path) 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 full_asset_path = if relative_path
dirpath.join(asset_path).to_s dirpath.join(asset_path).to_s

View file

@ -1,3 +1,5 @@
require 'addressable/uri'
# Relative Assets extension # Relative Assets extension
class Middleman::Extensions::RelativeAssets < ::Middleman::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.' 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] Contract String, Or[String, Pathname], Any => Maybe[String]
def rewrite_url(asset_path, dirpath, request_path) 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 full_asset_path = if relative_path
dirpath.join(asset_path).to_s dirpath.join(asset_path).to_s
@ -30,9 +36,9 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension
asset_path asset_path
end end
return unless !full_asset_path.include?('//') && !asset_path.start_with?('data:')
current_dir = Pathname(request_path).dirname 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
end end

View file

@ -1,7 +1,8 @@
require 'middleman-core/util'
require 'middleman-core/contracts'
require 'rack' require 'rack'
require 'rack/response' require 'rack/response'
require 'addressable/uri'
require 'middleman-core/util'
require 'middleman-core/contracts'
module Middleman module Middleman
module Middleware module Middleware
@ -51,10 +52,13 @@ module Middleman
if path =~ /(^\/$)|(#{@source_exts_regex_text}$)/ if path =~ /(^\/$)|(#{@source_exts_regex_text}$)/
if body = ::Middleman::Util.extract_response_text(response) if body = ::Middleman::Util.extract_response_text(response)
dirpath = Pathname.new(File.dirname(path)) dirpath = Pathname.new(File.dirname(path))
rewritten = ::Middleman::Util.rewrite_paths(body, path, @exts) do |asset_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 full_asset_path = if relative_path
dirpath.join(asset_path).to_s dirpath.join(asset_path).to_s
@ -76,7 +80,7 @@ module Middleman
[status, headers, response] [status, headers, response]
end end
Contract IGNORE_DESCRIPTOR => Bool Contract IGNORE_DESCRIPTOR, String => Bool
def should_ignore?(validator, value) def should_ignore?(validator, value)
if validator.is_a? Regexp if validator.is_a? Regexp
# Treat as Regexp # Treat as Regexp

View file

@ -356,15 +356,10 @@ module Middleman
opening_character = $1 opening_character = $1
asset_path = $2 asset_path = $2
begin uri = ::Addressable::URI.parse(asset_path)
uri = ::Addressable::URI.parse(asset_path) if uri.relative? && result = yield(asset_path)
"#{opening_character}#{result}"
if uri.relative? && result = yield(asset_path) else
"#{opening_character}#{result}"
else
match
end
rescue
match match
end end
end end