Sha256: 9c40a0555e3cb78f06516908ecd1a62394fa073aa253652d1879e1e916f65312

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

require 'rack/utils'
require 'tidy'

module Rack
  class HTMLTidy
    include Rack::Utils
    
    FORMAT = %{\n|| Tidy: %s - [%s] "%s %s%s %s"\n%s}

    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
 
    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
    
    private

    def log(env, tidy, what)
      now = Time.now
      logger = @logger || env['rack.errors']
      
      logger.write FORMAT % [
        what,
        now.strftime("%d/%b/%Y %H:%M:%S"),
        env["REQUEST_METHOD"],
        env["PATH_INFO"],
        env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
        env["HTTP_VERSION"],
        tidy.send(what)
      ]
    end
    
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
wbzyl-rack-htmltidy-0.0.8 lib/rack/htmltidy.rb
rack-htmltidy-0.1.0 lib/rack/htmltidy.rb