Class: Middleman::Extensions::MinifyCss::Rack

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

Overview

Rack middleware to look for CSS and compress it

Constant Summary collapse

INLINE_CSS_REGEX =
/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m

Instance Method Summary collapse

Constructor Details

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

Init

Parameters:

  • app (Class)
  • options (Hash) (defaults to: {})


37
38
39
40
41
42
# File 'lib/middleman-more/extensions/minify_css.rb', line 37

def initialize(app, options={})
  @app = app
  @compressor = options[:compressor]
  @ignore = options[:ignore]
  @inline = options[:inline]
end

Instance Method Details

#call(env) ⇒ Array

Rack interface

Parameters:

  • env (Rack::Environmemt)

Returns:

  • (Array)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/middleman-more/extensions/minify_css.rb', line 47

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

  if inline_html_content?(env["PATH_INFO"])
    minified = ::Middleman::Util.extract_response_text(response)
    minified.gsub!(INLINE_CSS_REGEX) do |match|
      $1 << @compressor.compress($2) << $3
    end

    headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
    response = [minified]
  elsif standalone_css_content?(env["PATH_INFO"])
    minified_css = @compressor.compress(::Middleman::Util.extract_response_text(response))

    headers["Content-Length"] = ::Rack::Utils.bytesize(minified_css).to_s
    response = [minified_css]
  end

  [status, headers, response]
end