2014-07-03 04:04:34 +02:00
|
|
|
require 'middleman-core/contracts'
|
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# 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'
|
2014-04-29 19:50:21 +02:00
|
|
|
option :compressor, proc {
|
2014-04-28 19:21:14 +02:00
|
|
|
require 'sass'
|
|
|
|
SassCompressor
|
|
|
|
}, 'Set the CSS compressor to use.'
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
def after_configuration
|
|
|
|
# Setup Rack middleware to minify CSS
|
2014-04-29 19:50:21 +02:00
|
|
|
app.use Rack, compressor: options[:compressor],
|
|
|
|
ignore: Array(options[:ignore]) + [/\.min\./],
|
|
|
|
inline: options[:inline]
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2013-03-20 00:21:49 +01:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
class SassCompressor
|
2014-04-29 19:50:21 +02:00
|
|
|
def self.compress(style, options={})
|
2013-04-20 23:27:25 +02:00
|
|
|
root_node = ::Sass::SCSS::CssParser.new(style, 'middleman-css-input', 1).parse
|
2014-04-29 19:44:24 +02:00
|
|
|
root_node.options = options.merge(style: :compressed)
|
2013-04-20 23:27:25 +02:00
|
|
|
root_node.render.strip
|
|
|
|
end
|
|
|
|
end
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Rack middleware to look for CSS and compress it
|
|
|
|
class Rack
|
2014-07-03 04:04:34 +02:00
|
|
|
include Contracts
|
2013-05-25 02:11:46 +02:00
|
|
|
INLINE_CSS_REGEX = /(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m
|
2013-04-20 23:27:25 +02:00
|
|
|
|
|
|
|
# Init
|
|
|
|
# @param [Class] app
|
|
|
|
# @param [Hash] options
|
|
|
|
def initialize(app, options={})
|
|
|
|
@app = app
|
2014-04-28 19:21:14 +02:00
|
|
|
@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
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2013-04-20 23:27:25 +02:00
|
|
|
# Rack interface
|
|
|
|
# @param [Rack::Environmemt] env
|
|
|
|
# @return [Array]
|
|
|
|
def call(env)
|
|
|
|
status, headers, response = @app.call(env)
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2013-12-28 01:26:31 +01:00
|
|
|
if inline_html_content?(env['PATH_INFO'])
|
2013-05-25 02:11:46 +02:00
|
|
|
minified = ::Middleman::Util.extract_response_text(response)
|
2014-04-29 01:02:18 +02:00
|
|
|
minified.gsub!(INLINE_CSS_REGEX) do
|
2013-05-25 02:11:46 +02:00
|
|
|
$1 << @compressor.compress($2) << $3
|
2013-04-20 23:27:25 +02:00
|
|
|
end
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2013-12-28 01:26:31 +01:00
|
|
|
headers['Content-Length'] = ::Rack::Utils.bytesize(minified).to_s
|
2013-04-20 23:27:25 +02:00
|
|
|
response = [minified]
|
2013-12-28 01:26:31 +01:00
|
|
|
elsif standalone_css_content?(env['PATH_INFO'])
|
2013-05-25 02:11:46 +02:00
|
|
|
minified_css = @compressor.compress(::Middleman::Util.extract_response_text(response))
|
2012-04-08 05:00:24 +02:00
|
|
|
|
2013-12-28 01:26:31 +01:00
|
|
|
headers['Content-Length'] = ::Rack::Utils.bytesize(minified_css).to_s
|
2013-04-20 23:27:25 +02:00
|
|
|
response = [minified_css]
|
2012-04-08 05:00:24 +02:00
|
|
|
end
|
2013-04-20 23:27:25 +02:00
|
|
|
|
|
|
|
[status, headers, response]
|
2012-04-08 05:00:24 +02:00
|
|
|
end
|
2013-05-25 02:11:46 +02:00
|
|
|
|
2014-04-29 19:50:21 +02:00
|
|
|
private
|
2014-04-29 01:02:18 +02:00
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract String => Bool
|
2013-05-25 02:11:46 +02:00
|
|
|
def inline_html_content?(path)
|
|
|
|
(path.end_with?('.html') || path.end_with?('.php')) && @inline
|
|
|
|
end
|
|
|
|
|
2014-07-03 04:04:34 +02:00
|
|
|
Contract String => Bool
|
2013-05-25 02:11:46 +02:00
|
|
|
def standalone_css_content?(path)
|
2014-04-29 19:50:21 +02:00
|
|
|
path.end_with?('.css') && @ignore.none? { |ignore| Middleman::Util.path_match(ignore, path) }
|
2013-05-25 02:11:46 +02:00
|
|
|
end
|
2012-04-08 05:00:24 +02:00
|
|
|
end
|
2012-04-27 06:55:07 +02:00
|
|
|
end
|