2009-11-30 14:32:02 -05:00
|
|
|
begin
|
|
|
|
require "yui/compressor"
|
|
|
|
rescue LoadError
|
|
|
|
puts "YUI-Compressor not available. Install it with: gem install yui-compressor"
|
|
|
|
end
|
|
|
|
|
2010-01-20 18:22:40 -05:00
|
|
|
class Middleman::Rack::MinifyCSS
|
|
|
|
def initialize(app, options={})
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
if Middleman::Base.enabled?(:minify_css) &&
|
|
|
|
env["DOWNSTREAM"] && env["PATH_INFO"].match(/\.css$/)
|
|
|
|
|
|
|
|
compressor = ::YUI::CssCompressor.new
|
|
|
|
|
|
|
|
source = env["DOWNSTREAM"][2].is_a?(::Rack::File) ?
|
|
|
|
File.read(env["DOWNSTREAM"][2].path) :
|
|
|
|
env["DOWNSTREAM"][2]
|
|
|
|
|
|
|
|
env["DOWNSTREAM"][2] = compressor.compress(source)
|
|
|
|
env["DOWNSTREAM"][1]["Content-Length"] = ::Rack::Utils.bytesize(env["DOWNSTREAM"][2]).to_s
|
2009-11-30 14:32:02 -05:00
|
|
|
end
|
2010-01-20 18:22:40 -05:00
|
|
|
|
|
|
|
@app.call(env)
|
2009-11-30 14:32:02 -05:00
|
|
|
end
|
|
|
|
end
|