Created
January 31, 2010 14:57
-
-
Save jzajpt/291100 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rack middleware. Strips unecessary whitespaces from HTML output. | |
class HtmlOutputStripper | |
def initialize(app, options = {}) | |
@app = app | |
end | |
def call(env) | |
status, headers, response = @app.call(env) | |
if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/ | |
body = "" | |
response.each { |part| body << part } | |
body = body.gsub(/>\s+([^\s])/, '> \1'). | |
gsub(/([^\s])\s+</, '\1 <'). | |
gsub(/>[ \t\n]+</, '><') | |
headers["Content-Length"] = body.length.to_s | |
response = [body] | |
end | |
[status, headers, response] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment