Skip to content

Instantly share code, notes, and snippets.

@youpy
Created October 12, 2009 08:18
Show Gist options
  • Select an option

  • Save youpy/208240 to your computer and use it in GitHub Desktop.

Select an option

Save youpy/208240 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'tidy'
module Rack
class CleanHTML
def initialize(app, options = {})
options = options.dup
Tidy.path = options.delete(:tidy_path)
@app = app
@tidy_options = options
end
def call(env)
status, headers, response = @app.call(env)
if headers['Content-Type'] =~ /html/
response = cleanHtml(response)
headers['Content-Length'] = response[0].size.to_s
end
[status, headers, response]
end
private
def cleanHtml(response)
body = ''
response.each do |res|
body += res
end
html = Tidy.open(@tidy_options) do |tidy|
tidy.clean(body)
end
[html]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment