From a65e1619b98e7682a841df13a49b5d46f1b746d2 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Thu, 26 Apr 2012 21:55:07 -0700 Subject: [PATCH] Move extract_response_text into Middleman::Util --- middleman-core/lib/middleman-core/util.rb | 22 ++++++++++++++++++- .../middleman-more/extensions/asset_hash.rb | 19 +--------------- .../middleman-more/extensions/minify_css.rb | 21 ++---------------- .../extensions/minify_javascript.rb | 21 ++---------------- 4 files changed, 26 insertions(+), 57 deletions(-) diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index 520c1be4..5c856de0 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -31,6 +31,26 @@ module Middleman def self.normalize_path(path) path.sub(/^\//, "").gsub("%20", " ") end + + # Extract the text of a Rack response as a string. + # Useful for extensions implemented as Rack middleware. + # @param response The response from #call + # @return [String] The whole response as a string. + def self.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 # Simple shared cache implementation class Cache @@ -87,4 +107,4 @@ module Middleman end end end -end \ No newline at end of file +end diff --git a/middleman-more/lib/middleman-more/extensions/asset_hash.rb b/middleman-more/lib/middleman-more/extensions/asset_hash.rb index e880973c..cb620411 100755 --- a/middleman-more/lib/middleman-more/extensions/asset_hash.rb +++ b/middleman-more/lib/middleman-more/extensions/asset_hash.rb @@ -69,7 +69,7 @@ module Middleman::Extensions dirpath = Pathname.new(File.dirname(path)) if path =~ /(^\/$)|(\.(htm|html|php|css|js)$)/ - body = extract_response_text(response) + body = ::Middleman.Util.extract_response_text(response) if body # TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK? @@ -94,23 +94,6 @@ module Middleman::Extensions 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 - end - end end end diff --git a/middleman-more/lib/middleman-more/extensions/minify_css.rb b/middleman-more/lib/middleman-more/extensions/minify_css.rb index 34f960ee..12ce00e6 100644 --- a/middleman-more/lib/middleman-more/extensions/minify_css.rb +++ b/middleman-more/lib/middleman-more/extensions/minify_css.rb @@ -52,7 +52,7 @@ module Middleman path = env["PATH_INFO"] if (path.end_with?('.html') || path.end_with?('.php')) && @inline - uncompressed_source = extract_response_text(response) + uncompressed_source = ::Middleman.Util.extract_response_text(response) minified = uncompressed_source.gsub(/(]*>\s*(?:\/\*\*\/)?\s*<\/style>)/m) do |match| first = $1 @@ -76,24 +76,7 @@ module Middleman [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 - end - end end end end -end \ No newline at end of file +end diff --git a/middleman-more/lib/middleman-more/extensions/minify_javascript.rb b/middleman-more/lib/middleman-more/extensions/minify_javascript.rb index 5512bd8f..63ffb3fc 100755 --- a/middleman-more/lib/middleman-more/extensions/minify_javascript.rb +++ b/middleman-more/lib/middleman-more/extensions/minify_javascript.rb @@ -54,7 +54,7 @@ module Middleman begin if (path.end_with?('.html') || path.end_with?('.php')) && @inline - uncompressed_source = extract_response_text(response) + uncompressed_source = ::Middleman.Util.extract_response_text(response) minified = uncompressed_source.gsub(/(]*>\s*(?:\/\/(?:(?:)|(?:\]\]>)))?\s*<\/script>)/m) do |match| first = $1 @@ -88,24 +88,7 @@ module Middleman [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 - end - end end end end -end \ No newline at end of file +end