Sha256: 1c31bb22b4d5c8b849dc646e7aae76a28bbbd1a2b62891a7d41754cfff0b230b

Contents?: true

Size: 566 Bytes

Versions: 2

Compression:

Stored size: 566 Bytes

Contents

require 'Open3'

class TidyRack
  def initialize(app)
    @app = app
  end
  
  def call(env)
    status, headers, response = @app.call(env)
    if headers['Content-Type'].include?('text/html')
      response = tidy(response)
      headers['Content-Length'] = response.size.to_s
    end
    [status, headers, response]
  end
  
  def tidy(response)
    html = ''
    Open3.popen3("tidy -i") do |stdin, stdout, stderr|
      response.each { |s| stdin.write s }
      stdin.close
      html << stdout.read
      html << "<!--\n#{stderr.read}\n-->\n"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
phorsfall-tidy_rack-0.0.0 lib/tidy_rack.rb
phorsfall-tidy_rack-0.1.0 lib/tidy_rack.rb