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
|
2013-12-28 01:26:31 +01:00
|
|
|
option :exts, %w(.jpg .jpeg .png .gif .js .css .otf .woff .eot .ttf .svg), 'List of extensions that get asset hashes appended to them.'
|
|
|
|
option :ignore, [], 'Regexes of filenames to skip 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'
|
2014-01-03 02:25:31 +01:00
|
|
|
require 'rack/mock'
|
2013-04-20 23:27:25 +02:00
|
|
|
require 'uri'
|
2014-04-16 00:16:52 +02:00
|
|
|
require 'middleman-core/middleware/inline_url_rewriter'
|
2013-04-20 23:27:25 +02:00
|
|
|
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-16 00:16:52 +02:00
|
|
|
app.use ::Middleman::Middleware::InlineURLRewriter,
|
|
|
|
:id => :asset_hash,
|
|
|
|
:url_extensions => options.exts,
|
|
|
|
:source_extensions => %w(.htm .html .php .css .js),
|
|
|
|
:ignore => @ignore,
|
|
|
|
:middleman_app => app,
|
|
|
|
:proc => method(:rewrite_url)
|
|
|
|
end
|
|
|
|
|
2014-04-07 21:43:16 +02:00
|
|
|
def rewrite_url(asset_path, dirpath, request_path)
|
2014-04-16 00:16:52 +02:00
|
|
|
relative_path = Pathname.new(asset_path).relative?
|
|
|
|
|
|
|
|
full_asset_path = if relative_path
|
|
|
|
dirpath.join(asset_path).to_s
|
|
|
|
else
|
|
|
|
asset_path
|
|
|
|
end
|
|
|
|
|
|
|
|
if asset_page = 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
|
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)
|
2014-01-03 02:25:31 +01:00
|
|
|
@rack_client = ::Rack::MockRequest.new(app.class.to_rack_app)
|
2013-06-01 02:46:12 +02:00
|
|
|
|
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.
|
2014-05-12 09:05:22 +02:00
|
|
|
resources.sort_by do |a|
|
2013-04-20 23:27:25 +02:00
|
|
|
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
|
2014-05-12 09:05:22 +02:00
|
|
|
end.each(&method(:manipulate_single_resource))
|
2014-04-14 19:05:00 +02:00
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
|
2014-05-12 09:05:22 +02:00
|
|
|
def manipulate_single_resource(resource)
|
|
|
|
return unless options.exts.include?(resource.ext)
|
|
|
|
return if ignored_resource?(resource)
|
|
|
|
return if resource.ignored?
|
|
|
|
|
2013-06-01 02:46:12 +02:00
|
|
|
# Render through the Rack interface so middleware and mounted apps get a shot
|
2014-04-16 00:16:52 +02:00
|
|
|
response = @rack_client.get(URI.escape(resource.destination_path), {
|
|
|
|
'bypass_inline_url_rewriter_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]
|
|
|
|
|
2014-05-12 09:05:22 +02:00
|
|
|
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
|
2013-06-01 02:46:12 +02:00
|
|
|
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
|
|
|
|
end
|