2012-05-25 01:14:17 +02:00
|
|
|
module Middleman
|
|
|
|
module Extensions
|
|
|
|
module AssetHash
|
|
|
|
class << self
|
2012-05-24 23:29:29 +02:00
|
|
|
def registered(app, options={})
|
2012-05-25 01:14:17 +02:00
|
|
|
require 'digest/sha1'
|
2012-06-02 08:54:57 +02:00
|
|
|
exts = options[:exts] || %w(.jpg .jpeg .png .gif .js .css)
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
# Allow specifying regexes to ignore, plus always ignore apple touch icons
|
|
|
|
ignore = Array(options[:ignore]) << /^apple-touch-icon/
|
|
|
|
|
|
|
|
app.ready do
|
|
|
|
sitemap.register_resource_list_manipulator(
|
2012-08-14 00:39:06 +02:00
|
|
|
:asset_hash,
|
2012-05-25 01:14:17 +02:00
|
|
|
AssetHashManager.new(self, exts, ignore)
|
|
|
|
)
|
2012-09-16 07:24:39 +02:00
|
|
|
|
2012-05-27 00:08:59 +02:00
|
|
|
use Middleware, :exts => exts, :middleman_app => self, :ignore => ignore
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
2012-04-17 09:22:08 +02:00
|
|
|
end
|
2012-05-25 01:14:17 +02:00
|
|
|
alias :included :registered
|
2012-04-17 09:22:08 +02:00
|
|
|
end
|
2012-04-04 09:00:33 +02:00
|
|
|
|
2012-08-14 00:39:06 +02:00
|
|
|
# Central class for managing asset_hash extension
|
2012-05-25 01:14:17 +02:00
|
|
|
class AssetHashManager
|
|
|
|
def initialize(app, exts, ignore)
|
|
|
|
@app = app
|
|
|
|
@exts = exts
|
|
|
|
@ignore = ignore
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
# Update the main sitemap resource list
|
|
|
|
# @return [void]
|
|
|
|
def manipulate_resource_list(resources)
|
|
|
|
resources.each do |resource|
|
2012-05-27 00:08:59 +02:00
|
|
|
next unless @exts.include? resource.ext
|
2012-06-17 05:23:33 +02:00
|
|
|
next if @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) }
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-27 00:08:59 +02:00
|
|
|
if resource.template? # if it's a template, render it out
|
2012-09-11 06:56:12 +02:00
|
|
|
# Render through the Rack interface so middleware and mounted apps get a shot
|
|
|
|
rack_client = ::Rack::Test::Session.new(@app.class)
|
|
|
|
response = rack_client.get(URI.escape(resource.destination_path), {}, { "bypass_asset_hash" => true })
|
|
|
|
raise "#{resource.path} should be in the sitemap!" unless response.status == 200
|
|
|
|
|
|
|
|
digest = Digest::SHA1.hexdigest(response.body)[0..7]
|
2012-05-27 00:08:59 +02:00
|
|
|
else # if it's a static file, just hash it
|
|
|
|
digest = Digest::SHA1.file(resource.source_file).hexdigest[0..7]
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
2012-05-27 00:08:59 +02:00
|
|
|
|
|
|
|
resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
|
2012-04-17 09:22:08 +02:00
|
|
|
end
|
2012-03-05 00:24:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-25 01:14:17 +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={})
|
2012-05-27 00:08:59 +02:00
|
|
|
@rack_app = app
|
|
|
|
@exts = options[:exts]
|
|
|
|
@ignore = options[:ignore]
|
2012-05-25 01:14:17 +02:00
|
|
|
@exts_regex_text = @exts.map {|e| Regexp.escape(e) }.join('|')
|
2012-05-27 00:08:59 +02:00
|
|
|
@middleman_app = options[:middleman_app]
|
2012-05-25 01:14:17 +02:00
|
|
|
end
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
def call(env)
|
|
|
|
status, headers, response = @rack_app.call(env)
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2012-09-11 06:56:12 +02:00
|
|
|
# We don't want to use this middleware when rendering files to figure out their hash!
|
|
|
|
return [status, headers, response] if env["bypass_asset_hash"]
|
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
path = @middleman_app.full_path(env["PATH_INFO"])
|
|
|
|
dirpath = Pathname.new(File.dirname(path))
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
if path =~ /(^\/$)|(\.(htm|html|php|css|js)$)/
|
|
|
|
body = ::Middleman::Util.extract_response_text(response)
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
if body
|
|
|
|
# TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK?
|
|
|
|
body.gsub! /([=\'\"\(]\s*)([^\s\'\"\)]+(#{@exts_regex_text}))/ do |match|
|
2012-05-27 00:08:59 +02:00
|
|
|
opening_character = $1
|
2012-05-25 01:14:17 +02:00
|
|
|
asset_path = $2
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
relative_path = Pathname.new(asset_path).relative?
|
2012-03-05 00:24:38 +01:00
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
asset_path = dirpath.join(asset_path).to_s if relative_path
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-27 00:08:59 +02:00
|
|
|
if @ignore.any? { |r| asset_path.match(r) }
|
|
|
|
match
|
|
|
|
elsif asset_page = @middleman_app.sitemap.find_resource_by_path(asset_path)
|
2012-05-25 01:14:17 +02:00
|
|
|
replacement_path = "/#{asset_page.destination_path}"
|
|
|
|
replacement_path = Pathname.new(replacement_path).relative_path_from(dirpath).to_s if relative_path
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-27 00:08:59 +02:00
|
|
|
"#{opening_character}#{replacement_path}"
|
2012-05-25 01:14:17 +02:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
2012-03-05 00:24:38 +01:00
|
|
|
end
|
|
|
|
|
2012-05-25 01:14:17 +02:00
|
|
|
status, headers, response = Rack::Response.new(body, status, headers).finish
|
|
|
|
end
|
2012-03-05 00:24:38 +01:00
|
|
|
end
|
2012-05-25 01:14:17 +02:00
|
|
|
[status, headers, response]
|
2012-03-05 00:24:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# =================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
|