middleman/middleman-core/lib/middleman-core/extensions/minify_css.rb
2014-04-28 10:21:14 -07:00

76 lines
2.4 KiB
Ruby

# Minify CSS Extension
class Middleman::Extensions::MinifyCss < ::Middleman::Extension
option :inline, false, 'Whether to minify CSS inline within HTML files'
option :ignore, [], 'Patterns to avoid minifying'
option :compressor, Proc.new {
require 'sass'
SassCompressor
}, 'Set the CSS compressor to use.'
def after_configuration
# Setup Rack middleware to minify CSS
app.use Rack, :compressor => options[:compressor],
:ignore => Array(options[:ignore]) + [/\.min\./],
:inline => options[:inline]
end
class SassCompressor
def self.compress(style, options = {})
root_node = ::Sass::SCSS::CssParser.new(style, 'middleman-css-input', 1).parse
root_node.options = { :style => :compressed }
root_node.render.strip
end
end
# Rack middleware to look for CSS and compress it
class Rack
INLINE_CSS_REGEX = /(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m
# Init
# @param [Class] app
# @param [Hash] options
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
end
# Rack interface
# @param [Rack::Environmemt] env
# @return [Array]
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
private
def inline_html_content?(path)
(path.end_with?('.html') || path.end_with?('.php')) && @inline
end
def standalone_css_content?(path)
path.end_with?('.css') && @ignore.none? {|ignore| Middleman::Util.path_match(ignore, path) }
end
end
end