Class: Rack::HTMLTidy

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/htmltidy.rb

Constant Summary collapse

FORMAT =
%{\n|| Tidy: %s - [%s] "%s %s%s %s"\n%s}

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ HTMLTidy

Returns a new instance of HTMLTidy.



10
11
12
13
14
15
16
17
18
# File 'lib/rack/htmltidy.rb', line 10

def initialize(app, opts={})
  @app = app
  @errors = opts[:errors] || false
  @diagnostics = opts[:diagnostics] || false
  @logger = opts[:logger]
  
  @path = opts[:path] || "/usr/lib/libtidy-0.99.so.0"      
  ::Tidy.path = @path
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rack/htmltidy.rb', line 20

def call(env)
  status, headers, response = @app.call(env)

  headers = HeaderHash.new(headers)

  if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
     !headers['transfer-encoding'] &&
      headers['content-type'] &&
      headers['content-type'].include?("text/html")

    ::Tidy.open(:show_warnings => true, "char-encoding" => "utf8") do |tidy|
      html = ""
      response.each { |part| html += part }
      tidy.clean(html)
      log(env, tidy, "errors") if @errors && tidy.errors.length > 0
      log(env, tidy, "diagnostics") if @diagnostics && tidy.diagnostics.length > 0
    end
  end
  
  [status, headers, response]
end