class Middleman::Extension::BemHtml::Rack

Public Class Methods

new(app, options={}) click to toggle source
# File lib/middleman-bem-html.rb, line 53
def initialize(app, options={})
        @app = app
        @ignore = options.fetch(:ignore)
        @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-bem-html.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 minifiable_inline?(content_type)
                minify_inline(::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-bem-html.rb, line 85
def ignore?(path)
        @ignore.any? { |ignore| ::Middleman::Util.path_match(ignore, path) }
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-bem-html.rb, line 93
def minifiable_inline?(content_type)
        @inline_content_types.include?(content_type)
end
minify_inline(content) click to toggle source

Detect and minify inline content @param [String] content @return [String]

# File lib/middleman-bem-html.rb, line 101
def minify_inline(content)
        BemHtml.parse(content)
end