Tidy Markup Cleaner for Rack

NOTE: This is a fork of the rack-tidy gem. This fork only provides basic HTML5 Doctype support.

Rack::Tidy cleans text/html markup by automatically indenting and reformatting content. Best results are achieved with valid markup, when you simply want to use this component to produce clean (X)HTML rendered by templating systems such as ERb.

Rack::Tidy relies on the power of the Tidy gem and defaults to settings based on convention. However, you can override these through configuration.

For more information about the Rack specification, check out http://rack.rubyforge.org

Installation

Bundler:

Add this to your Gemfile:

gem "html5-rack-tidy", "~> 0.2.3"

With a local copy:

$ git clone git://github.com/customink/html5-rack-tidy.git

And in your Gemfile:

gem "html5-rack-tidy", :path => "path/to/your/local/copy"

Command Line

Prerequisites:

  • Rack gem (sudo gem install rack)
  • Tidy gem (sudo gem install tidy)

Enter this at the command-line:

$ sudo gem install html5-rack-tidy

With a local working copy:

$ git clone git://github.com/customink/html5-rack-tidy.git
$ rake build && sudo rake install

Usage

Basic Usage

Rack::Tidy is implemented as a piece of Rack middleware and can be used with any Rack-based application. If your application includes a rackup (.ru) file or uses Rack::Builder to construct the application pipeline, simply require and use as follows:

require 'rack/tidy'

use Rack::Tidy
run app

Using with Rails

Add this to your config/environment.rb:

# above Rails::Initializer block
require 'rack/tidy'

# inside Rails::Initializer block
config.middleware.use Rack::Tidy

You should now see Rack::Tidy listed in the middleware pipeline:

rake middleware

Configuration Options

The Tidy gem requires setting an internal path variable that points to the Tidy library, which will differ per platform. Rack::Tidy defaults to ‘/usr/lib/libtidy.A.dylib’ which is the default location for the Tidy gem on Mac Leopard. To override the constant, simply define it in your application’s configuration file:

TIDY_LIB = '/usr/lib/tidylib.so'

Need Rack::Tidy to ignore certain paths? In your config, pass in an optional array of paths:

# Rails example
config.middleware.use Rack::Tidy, 
  :ignore_paths => ['/admin', '/cms']

Rack::Tidy relies on convention with regard to Tidy’s configuration (see Rack::Tidy::Cleaner for details). If you want to override/set attributes supported by the Tidy gem, declare them in your configuration as an optional hash:

# Rails example
config.middleware.use Rack::Tidy,
  'indent-spaces' => 4

To specify the HTML5 Doctype:

config.middleware.use Rack::Tidy,
  'doctype' => 'html5'

See http://tidy.sourceforge.net/docs/quickref.html for Tidy gem config options

Copyright (c) 2009 Phil Misiowiec, Webficient LLC. See MIT-LICENSE for details.

Modifications made by CustomInk, LLC (2011) without consent from Phil Misiowiec of Webficient, LLC as allowed by the licensing terms.