mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
Merge pull request #395 from bhollis/extract_response
Move extract_response_text into Middleman::Util
This commit is contained in:
commit
a355c4a793
4 changed files with 26 additions and 57 deletions
|
@ -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
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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,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
|
||||
end
|
||||
|
|
|
@ -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,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
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue