Minor minify_css cleanup
This commit is contained in:
parent
072880d79c
commit
2292c32773
1 changed files with 85 additions and 85 deletions
|
@ -1,99 +1,99 @@
|
||||||
# Extensions namespace
|
# Extensions namespace
|
||||||
module Middleman::Extensions
|
module Middleman
|
||||||
|
module Extensions
|
||||||
|
|
||||||
# Minify CSS Extension
|
# Minify CSS Extension
|
||||||
module MinifyCss
|
module MinifyCss
|
||||||
|
|
||||||
# Setup extension
|
# Setup extension
|
||||||
class << self
|
class << self
|
||||||
|
|
||||||
# Once registered
|
# Once registered
|
||||||
def registered(app, options={})
|
def registered(app, options={})
|
||||||
app.set :css_compressor, false
|
app.set :css_compressor, false
|
||||||
|
|
||||||
ignore = Array(options[:ignore]) << /\.min\./
|
ignore = Array(options[:ignore]) << /\.min\./
|
||||||
inline = options[:inline] || false
|
inline = options[:inline] || false
|
||||||
|
|
||||||
app.after_configuration do
|
app.after_configuration do
|
||||||
chosen_compressor = css_compressor || options[:compressor] || begin
|
chosen_compressor = css_compressor || options[:compressor] || begin
|
||||||
require "middleman-more/extensions/minify_css/rainpress"
|
require "middleman-more/extensions/minify_css/rainpress"
|
||||||
::Rainpress
|
::Rainpress
|
||||||
|
end
|
||||||
|
|
||||||
|
# Setup Rack middleware to minify CSS
|
||||||
|
use Rack, :compressor => chosen_compressor,
|
||||||
|
:ignore => ignore,
|
||||||
|
:inline => inline
|
||||||
|
end
|
||||||
|
end
|
||||||
|
alias :included :registered
|
||||||
|
end
|
||||||
|
|
||||||
|
# Rack middleware to look for CSS and compress it
|
||||||
|
class Rack
|
||||||
|
|
||||||
|
# Init
|
||||||
|
# @param [Class] app
|
||||||
|
# @param [Hash] options
|
||||||
|
def initialize(app, options={})
|
||||||
|
@app = app
|
||||||
|
@compressor = options[:compressor]
|
||||||
|
@ignore = options[:ignore]
|
||||||
|
@inline = options[:inline]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Rack interface
|
||||||
|
# @param [Rack::Environmemt] env
|
||||||
|
# @return [Array]
|
||||||
|
def call(env)
|
||||||
|
status, headers, response = @app.call(env)
|
||||||
|
|
||||||
|
path = env["PATH_INFO"]
|
||||||
|
|
||||||
|
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
|
||||||
|
uncompressed_source = extract_response_text(response)
|
||||||
|
|
||||||
|
minified = uncompressed_source.gsub(/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m) do |match|
|
||||||
|
first = $1
|
||||||
|
css = $2
|
||||||
|
last = $3
|
||||||
|
|
||||||
|
minified_css = @compressor.compress(css)
|
||||||
|
|
||||||
|
first << minified_css << last
|
||||||
|
end
|
||||||
|
|
||||||
|
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
|
||||||
|
response = [minified]
|
||||||
|
elsif path.end_with?('.css') && @ignore.none? {|ignore| path =~ ignore }
|
||||||
|
uncompressed_source = extract_response_text(response)
|
||||||
|
minified_css = @compressor.compress(uncompressed_source)
|
||||||
|
|
||||||
|
headers["Content-Length"] = ::Rack::Utils.bytesize(minified_css).to_s
|
||||||
|
response = [minified_css]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Setup Rack middleware to minify CSS
|
[status, headers, response]
|
||||||
use MinifyCSSRack, :compressor => chosen_compressor,
|
|
||||||
:ignore => ignore,
|
|
||||||
:inline => inline
|
|
||||||
end
|
|
||||||
end
|
|
||||||
alias :included :registered
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Rack middleware to look for CSS and compress it
|
|
||||||
class MinifyCSSRack
|
|
||||||
|
|
||||||
# Init
|
|
||||||
# @param [Class] app
|
|
||||||
# @param [Hash] options
|
|
||||||
def initialize(app, options={})
|
|
||||||
@app = app
|
|
||||||
@compressor = options[:compressor]
|
|
||||||
@ignore = options[:ignore]
|
|
||||||
@inline = options[:inline]
|
|
||||||
end
|
|
||||||
|
|
||||||
# Rack interface
|
|
||||||
# @param [Rack::Environmemt] env
|
|
||||||
# @return [Array]
|
|
||||||
def call(env)
|
|
||||||
status, headers, response = @app.call(env)
|
|
||||||
|
|
||||||
path = env["PATH_INFO"]
|
|
||||||
|
|
||||||
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
|
|
||||||
uncompressed_source = extract_response_text(response)
|
|
||||||
|
|
||||||
minified = uncompressed_source.gsub(/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m) do |match|
|
|
||||||
first = $1
|
|
||||||
css = $2
|
|
||||||
last = $3
|
|
||||||
|
|
||||||
minified_css = @compressor.compress(css)
|
|
||||||
|
|
||||||
first << minified_css << last
|
|
||||||
end
|
end
|
||||||
|
|
||||||
headers["Content-Length"] = ::Rack::Utils.bytesize(minified).to_s
|
private
|
||||||
response = [minified]
|
|
||||||
elsif path.end_with?('.css') && @ignore.none? {|ignore| path =~ ignore }
|
|
||||||
uncompressed_source = extract_response_text(response)
|
|
||||||
minified_css = @compressor.compress(uncompressed_source)
|
|
||||||
|
|
||||||
headers["Content-Length"] = ::Rack::Utils.bytesize(minified_css).to_s
|
def extract_response_text(response)
|
||||||
response = [minified_css]
|
case(response)
|
||||||
end
|
when String
|
||||||
|
response
|
||||||
[status, headers, response]
|
when Array
|
||||||
end
|
response.join
|
||||||
|
when ::Rack::Response
|
||||||
private
|
response.body.join
|
||||||
|
when ::Rack::File
|
||||||
def extract_response_text(response)
|
File.read(response.path)
|
||||||
case(response)
|
else
|
||||||
when String
|
response.to_s
|
||||||
response
|
end
|
||||||
when Array
|
end
|
||||||
response.join
|
|
||||||
when Rack::Response
|
|
||||||
response.body.join
|
|
||||||
when Rack::File
|
|
||||||
File.read(response.path)
|
|
||||||
else
|
|
||||||
response.to_s
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# Register extension
|
end
|
||||||
# register :minify_css, MinifyCss
|
|
||||||
end
|
|
Loading…
Add table
Reference in a new issue