From 2292c32773344d7b55f0b64ba36ba365d3f5acba Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Thu, 26 Apr 2012 15:56:27 -0700 Subject: [PATCH] Minor minify_css cleanup --- .../middleman-more/extensions/minify_css.rb | 170 +++++++++--------- 1 file changed, 85 insertions(+), 85 deletions(-) diff --git a/middleman-more/lib/middleman-more/extensions/minify_css.rb b/middleman-more/lib/middleman-more/extensions/minify_css.rb index 5f3ea461..34f960ee 100644 --- a/middleman-more/lib/middleman-more/extensions/minify_css.rb +++ b/middleman-more/lib/middleman-more/extensions/minify_css.rb @@ -1,99 +1,99 @@ # Extensions namespace -module Middleman::Extensions +module Middleman + module Extensions - # Minify CSS Extension - module MinifyCss + # Minify CSS Extension + module MinifyCss - # Setup extension - class << self + # Setup extension + class << self - # Once registered - def registered(app, options={}) - app.set :css_compressor, false + # Once registered + def registered(app, options={}) + app.set :css_compressor, false - ignore = Array(options[:ignore]) << /\.min\./ - inline = options[:inline] || false + ignore = Array(options[:ignore]) << /\.min\./ + inline = options[:inline] || false - app.after_configuration do - chosen_compressor = css_compressor || options[:compressor] || begin - require "middleman-more/extensions/minify_css/rainpress" - ::Rainpress + app.after_configuration do + chosen_compressor = css_compressor || options[:compressor] || begin + require "middleman-more/extensions/minify_css/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(/(]*>\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 - # Setup Rack middleware to minify CSS - 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(/(]*>\s*(?:\/\*\*\/)?\s*<\/style>)/m) do |match| - first = $1 - css = $2 - last = $3 - - minified_css = @compressor.compress(css) - - first << minified_css << last + [status, headers, response] 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) + private - headers["Content-Length"] = ::Rack::Utils.bytesize(minified_css).to_s - response = [minified_css] - end - - [status, headers, response] - end - - private - - def extract_response_text(response) - case(response) - when String - response - when Array - response.join - when Rack::Response - response.body.join - when Rack::File - File.read(response.path) - else - response.to_s + def extract_response_text(response) + case(response) + when String + response + when Array + response.join + when ::Rack::Response + response.body.join + when ::Rack::File + File.read(response.path) + else + response.to_s + end + end end end end - # Register extension - # register :minify_css, MinifyCss -end +end \ No newline at end of file