Skip to content

Instantly share code, notes, and snippets.

@jzajpt
Created January 31, 2010 14:57
Show Gist options
  • Save jzajpt/291100 to your computer and use it in GitHub Desktop.
Save jzajpt/291100 to your computer and use it in GitHub Desktop.
# 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