class Middleman::Extensions::MinifyCss::Rack
Rack
middleware to look for CSS and compress it
Constants
- INLINE_CSS_REGEX
Public Class Methods
new(app, options={})
click to toggle source
# File lib/middleman-core/extensions/minify_css.rb, line 47 def initialize(app, options={}) @app = app @ignore = options.fetch(:ignore) @inline = options.fetch(:inline) @compressor = options.fetch(:compressor) @compressor = @compressor.to_proc if @compressor.respond_to? :to_proc @compressor = @compressor.call if @compressor.is_a? Proc @content_types = options[:content_types] @inline_content_types = options[:inline_content_types] end
Public Instance Methods
call(env)
click to toggle source
Rack
interface @param [Rack::Environmemt] env @return [Array]
# File lib/middleman-core/extensions/minify_css.rb, line 62 def call(env) status, headers, response = @app.call(env) content_type = headers['Content-Type'].try(:slice, /^[^;]*/) path = env['PATH_INFO'] minified = if @inline && minifiable_inline?(content_type) minify_inline(::Middleman::Util.extract_response_text(response)) elsif minifiable?(content_type) && !ignore?(path) minify(::Middleman::Util.extract_response_text(response)) end if minified headers['Content-Length'] = ::Rack::Utils.bytesize(minified).to_s response = [minified] end [status, headers, response] end
Private Instance Methods
ignore?(path)
click to toggle source
Whether the path should be ignored @param [String] path @return [Boolean]
# File lib/middleman-core/extensions/minify_css.rb, line 87 def ignore?(path) @ignore.any? { |ignore| ::Middleman::Util.path_match(ignore, path) } end
minifiable?(content_type)
click to toggle source
Whether this type of content can be minified @param [String, nil] content_type @return [Boolean]
# File lib/middleman-core/extensions/minify_css.rb, line 95 def minifiable?(content_type) @content_types.include?(content_type) end
minifiable_inline?(content_type)
click to toggle source
Whether this type of content contains inline content that can be minified @param [String, nil] content_type @return [Boolean]
# File lib/middleman-core/extensions/minify_css.rb, line 103 def minifiable_inline?(content_type) @inline_content_types.include?(content_type) end
minify(content)
click to toggle source
Minify the content @param [String] content @return [String]
# File lib/middleman-core/extensions/minify_css.rb, line 111 def minify(content) @compressor.compress(content) end
minify_inline(content)
click to toggle source
Detect and minify inline content @param [String] content @return [String]
# File lib/middleman-core/extensions/minify_css.rb, line 119 def minify_inline(content) content.gsub(INLINE_CSS_REGEX) do $1 + minify($2) + $3 end end