Move extract_response_text into Middleman::Util

This commit is contained in:
Ben Hollis 2012-04-26 21:55:07 -07:00
parent a4b68335d4
commit a65e1619b9
4 changed files with 26 additions and 57 deletions

View file

@ -32,6 +32,26 @@ module Middleman
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
# Initialize

View file

@ -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

View file

@ -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(/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m) do |match|
first = $1
@ -76,23 +76,6 @@ 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

View file

@ -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(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match|
first = $1
@ -88,23 +88,6 @@ 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