Class: Middleman::Extensions::AssetHash::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-more/extensions/asset_hash.rb

Overview

The asset hash middleware is responsible for rewriting references to assets to include their new, hashed name.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



62
63
64
65
66
67
68
# File 'lib/middleman-more/extensions/asset_hash.rb', line 62

def initialize(app, options={})
  @rack_app        = app
  @exts            = options[:exts]
  @ignore          = options[:ignore]
  @exts_regex_text = @exts.map { |e| Regexp.escape(e) }.join('|')
  @middleman_app   = options[:middleman_app]
end

Instance Method Details

#call(env) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/middleman-more/extensions/asset_hash.rb', line 70

def call(env)
  status, headers, response = @rack_app.call(env)

  # 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'] == 'true'

  path = ::Middleman::Util.full_path(env['PATH_INFO'], @middleman_app)

  if path =~ /(^\/$)|(\.(htm|html|php|css|js|json)$)/
    body = ::Middleman::Util.extract_response_text(response)
    if body
      status, headers, response = Rack::Response.new(rewrite_paths(body, path), status, headers).finish
    end
  end

  [status, headers, response]
end