Sha256: 47db8e981199b1e75135e37eb19ed250f4793426db7f0f1920014c29b019573e

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

gem 'rack'
require 'rack/utils'

gem 'tidy'
require 'tidy'

module Rack
  class HTMLTidy
    include Rack::Utils
    
    FORMAT = %{|| 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

1 entries across 1 versions & 1 rubygems

Version Path
wbzyl-rack-htmltidy-0.0.6 lib/rack/htmltidy.rb