Rack::Tidy

Added by webficient at October 13, 2009 19:06 Star_smallStar_smallStar_smallStar_smallStar_small
GitHub stats:
Magnifier watcher(s)
Arrow_branch fork(s)
Wrench

About

Tired of excessive white space and crappy ERb indenting? 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.

Usage

This middleware component is available as a gem.

Gemcutter: http://gemcutter.org/gems/rack-tidy
Source: http://github.com/webficient/rack-tidy

Installation

Prerequisites:

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

From Gem:

$ sudo gem install rack-tidy --source=http://gemcutter.org

With a local working copy:

$ git clone git://github.com/webficient/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</code

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

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

blog comments powered by Disqus