Merge pull request #395 from bhollis/extract_response
Move extract_response_text into Middleman::Util
This commit is contained in:
commit
a355c4a793
|
@ -32,6 +32,26 @@ module Middleman
|
||||||
path.sub(/^\//, "").gsub("%20", " ")
|
path.sub(/^\//, "").gsub("%20", " ")
|
||||||
end
|
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
|
# Simple shared cache implementation
|
||||||
class Cache
|
class Cache
|
||||||
# Initialize
|
# Initialize
|
||||||
|
|
|
@ -69,7 +69,7 @@ module Middleman::Extensions
|
||||||
dirpath = Pathname.new(File.dirname(path))
|
dirpath = Pathname.new(File.dirname(path))
|
||||||
|
|
||||||
if path =~ /(^\/$)|(\.(htm|html|php|css|js)$)/
|
if path =~ /(^\/$)|(\.(htm|html|php|css|js)$)/
|
||||||
body = extract_response_text(response)
|
body = ::Middleman.Util.extract_response_text(response)
|
||||||
|
|
||||||
if body
|
if body
|
||||||
# TODO: This regex will change some paths in plan HTML (not in a tag) - is that OK?
|
# 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
|
end
|
||||||
[status, headers, response]
|
[status, headers, response]
|
||||||
end
|
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
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ module Middleman
|
||||||
path = env["PATH_INFO"]
|
path = env["PATH_INFO"]
|
||||||
|
|
||||||
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
|
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|
|
minified = uncompressed_source.gsub(/(<style[^>]*>\s*(?:\/\*<!\[CDATA\[\*\/\n)?)(.*?)((?:(?:\n\s*)?\/\*\]\]>\*\/)?\s*<\/style>)/m) do |match|
|
||||||
first = $1
|
first = $1
|
||||||
|
@ -76,23 +76,6 @@ module Middleman
|
||||||
|
|
||||||
[status, headers, response]
|
[status, headers, response]
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -54,7 +54,7 @@ module Middleman
|
||||||
|
|
||||||
begin
|
begin
|
||||||
if (path.end_with?('.html') || path.end_with?('.php')) && @inline
|
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|
|
minified = uncompressed_source.gsub(/(<script[^>]*>\s*(?:\/\/(?:(?:<!--)|(?:<!\[CDATA\[))\n)?)(.*?)((?:(?:\n\s*)?\/\/(?:(?:-->)|(?:\]\]>)))?\s*<\/script>)/m) do |match|
|
||||||
first = $1
|
first = $1
|
||||||
|
@ -88,23 +88,6 @@ module Middleman
|
||||||
|
|
||||||
[status, headers, response]
|
[status, headers, response]
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue