2013-12-31 23:41:17 +01:00
|
|
|
require 'middleman-core/util'
|
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
class Middleman::Extensions::AssetHash < ::Middleman::Extension
|
2015-02-25 20:43:46 +01:00
|
|
|
option :exts, %w(.jpg .jpeg .png .gif .webp .js .css .otf .woff .woff2 .eot .ttf .svg .svgz), 'List of extensions that get asset hashes appended to them.'
|
2014-06-24 22:34:24 +02:00
|
|
|
option :ignore, [], 'Patterns to avoid adding asset hashes to'
|
2012-05-25 01:14:17 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def initialize(app, options_hash={}, &block)
|
|
|
|
super
|
2012-04-04 09:00:33 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
require 'digest/sha1'
|
|
|
|
require 'rack/test'
|
|
|
|
require 'uri'
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def after_configuration
|
|
|
|
# Allow specifying regexes to ignore, plus always ignore apple touch icons
|
|
|
|
@ignore = Array(options.ignore) + [/^apple-touch-icon/]
|
2012-09-11 06:56:12 +02:00
|
|
|
|
2014-04-29 19:44:24 +02:00
|
|
|
app.use Middleware, exts: options.exts, middleman_app: app, ignore: @ignore
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-05-27 00:08:59 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Update the main sitemap resource list
|
|
|
|
# @return [void]
|
|
|
|
def manipulate_resource_list(resources)
|
2013-06-01 02:46:12 +02:00
|
|
|
@rack_client ||= ::Rack::Test::Session.new(app.class.to_rack_app)
|
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Process resources in order: binary images and fonts, then SVG, then JS/CSS.
|
|
|
|
# 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
|
|
|
|
0
|
|
|
|
elsif %w(.js .css).include? a.ext
|
|
|
|
1
|
|
|
|
else
|
|
|
|
-1
|
2012-03-05 00:24:38 +01:00
|
|
|
end
|
2013-06-01 02:46:12 +02:00
|
|
|
end.each(&method(:manipulate_single_resource))
|
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2013-06-01 02:46:12 +02:00
|
|
|
def manipulate_single_resource(resource)
|
|
|
|
return unless options.exts.include?(resource.ext)
|
|
|
|
return if ignored_resource?(resource)
|
2014-03-24 07:35:12 +01:00
|
|
|
return if resource.ignored?
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2013-06-01 02:46:12 +02:00
|
|
|
# Render through the Rack interface so middleware and mounted apps get a shot
|
2014-04-29 01:02:18 +02:00
|
|
|
response = @rack_client.get(URI.escape(resource.destination_path), {}, 'bypass_asset_hash' => 'true')
|
2013-06-01 02:46:12 +02:00
|
|
|
raise "#{resource.path} should be in the sitemap!" unless response.status == 200
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2013-06-01 02:46:12 +02:00
|
|
|
digest = Digest::SHA1.hexdigest(response.body)[0..7]
|
|
|
|
|
|
|
|
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignored_resource?(resource)
|
|
|
|
@ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# The asset hash middleware is responsible for rewriting references to
|
|
|
|
# assets to include their new, hashed name.
|
|
|
|
class Middleware
|
|
|
|
def initialize(app, options={})
|
|
|
|
@rack_app = app
|
|
|
|
@exts = options[:exts]
|
|
|
|
@ignore = options[:ignore]
|
2015-05-28 16:21:50 +02:00
|
|
|
@exts_regex_text = @exts.map { |e| Regexp.escape(e) }.sort.reverse.join('|')
|
2013-04-20 23:27:25 +02:00
|
|
|
@middleman_app = options[:middleman_app]
|
|
|
|
end
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def call(env)
|
|
|
|
status, headers, response = @rack_app.call(env)
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# We don't want to use this middleware when rendering files to figure out their hash!
|
2013-12-28 01:26:31 +01:00
|
|
|
return [status, headers, response] if env['bypass_asset_hash'] == 'true'
|
2012-09-11 06:56:12 +02:00
|
|
|
|
2013-12-31 23:41:17 +01:00
|
|
|
path = ::Middleman::Util.full_path(env['PATH_INFO'], @middleman_app)
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2014-09-28 22:39:46 +02:00
|
|
|
if path =~ /(^\/$)|(\.(htm|html|php|css|js|json)$)/
|
2013-04-20 23:27:25 +02:00
|
|
|
body = ::Middleman::Util.extract_response_text(response)
|
|
|
|
if body
|
2013-06-01 02:46:12 +02:00
|
|
|
status, headers, response = Rack::Response.new(rewrite_paths(body, path), status, headers).finish
|
|
|
|
end
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-06-01 02:46:12 +02:00
|
|
|
[status, headers, response]
|
|
|
|
end
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2014-04-29 01:02:18 +02:00
|
|
|
private
|
2013-06-01 02:46:12 +02:00
|
|
|
|
|
|
|
def rewrite_paths(body, path)
|
|
|
|
dirpath = Pathname.new(File.dirname(path))
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-06-01 02:46:12 +02:00
|
|
|
# TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK?
|
2014-11-16 21:25:28 +01:00
|
|
|
body.gsub(/([=\'\"\(,]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/) do |match|
|
2013-06-01 02:46:12 +02:00
|
|
|
opening_character = $1
|
|
|
|
asset_path = $2
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2013-06-01 02:46:12 +02:00
|
|
|
relative_path = Pathname.new(asset_path).relative?
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2013-06-01 02:46:12 +02:00
|
|
|
asset_path = dirpath.join(asset_path).to_s if relative_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 = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path
|
|
|
|
|
|
|
|
"#{opening_character}#{replacement_path}"
|
|
|
|
else
|
|
|
|
match
|
2012-03-05 00:24:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-03-05 00:24:38 +01:00
|
|
|
|
|
|
|
# =================Temp Generate Test data==============================
|
|
|
|
# ["jpg", "png", "gif"].each do |ext|
|
|
|
|
# [["<p>", "</p>"], ["<p><img src=", " /></p>"], ["<p>background-image:url(", ");</p>"]].each do |outer|
|
|
|
|
# [["",""], ["'", "'"], ['"','"']].each do |inner|
|
|
|
|
# [["", ""], ["/", ""], ["../", ""], ["../../", ""], ["../../../", ""], ["http://example.com/", ""], ["a","a"], ["1","1"], [".", "."], ["-","-"], ["_","_"]].each do |path_parts|
|
|
|
|
# name = 'images/100px.'
|
|
|
|
# puts outer[0] + inner[0] + path_parts[0] + name + ext + path_parts[1] + inner[1] + outer[1]
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# puts "<br /><br /><br />"
|
|
|
|
# end
|