Class: Middleman::Extensions::AssetHash
- Inherits:
-
Middleman::Extension
- Object
- Middleman::Extension
- Middleman::Extensions::AssetHash
- Defined in:
- lib/middleman-more/extensions/asset_hash.rb
Defined Under Namespace
Classes: Middleware
Instance Attribute Summary
Attributes inherited from Middleman::Extension
Instance Method Summary collapse
- #after_configuration ⇒ Object
- #ignored_resource?(resource) ⇒ Boolean
-
#initialize(app, options_hash = {}, &block) ⇒ AssetHash
constructor
A new instance of AssetHash.
-
#manipulate_resource_list(resources) ⇒ void
Update the main sitemap resource list.
- #manipulate_single_resource(resource) ⇒ Object
Methods inherited from Middleman::Extension
activate, activated_extension, after_extension_activated, clear_after_extension_callbacks, config, extension_name, helpers, option, register
Constructor Details
#initialize(app, options_hash = {}, &block) ⇒ AssetHash
Returns a new instance of AssetHash.
5 6 7 8 9 10 11 |
# File 'lib/middleman-more/extensions/asset_hash.rb', line 5 def initialize(app, ={}, &block) super require 'digest/sha1' require 'rack/test' require 'uri' end |
Instance Method Details
#after_configuration ⇒ Object
13 14 15 16 17 18 |
# File 'lib/middleman-more/extensions/asset_hash.rb', line 13 def after_configuration # Allow specifying regexes to ignore, plus always ignore apple touch icons @ignore = Array(.ignore) + [/^apple-touch-icon/] app.use Middleware, :exts => .exts, :middleman_app => app, :ignore => @ignore end |
#ignored_resource?(resource) ⇒ Boolean
52 53 54 |
# File 'lib/middleman-more/extensions/asset_hash.rb', line 52 def ignored_resource?(resource) @ignore.any? { |ignore| Middleman::Util.path_match(ignore, resource.destination_path) } end |
#manipulate_resource_list(resources) ⇒ void
This method returns an undefined value.
Update the main sitemap resource list
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/middleman-more/extensions/asset_hash.rb', line 22 def manipulate_resource_list(resources) @rack_client ||= ::Rack::Test::Session.new(app.class.to_rack_app) # 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 end end.each(&method(:manipulate_single_resource)) end |
#manipulate_single_resource(resource) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/middleman-more/extensions/asset_hash.rb', line 39 def manipulate_single_resource(resource) return unless .exts.include?(resource.ext) return if ignored_resource?(resource) # Render through the Rack interface so middleware and mounted apps get a shot 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] resource.destination_path = resource.destination_path.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" } end |