Module: Rack::Tidy

Defined in:
lib/rack/tidy.rb,
lib/rack/tidy/cleaner.rb

Overview

Tidy Markup Cleaner for Rack

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

Within a rackup file (or with Rack::Builder):

require 'rack/tidy'
use Rack::Tidy,
  :ignore_paths => ['/admin', '/cms'],
  'indent-spaces' => 4
run app

Rails example:

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

# inside Rails::Initializer block 
config.middleware.use Rack::Tidy,
  :ignore_paths => ['/admin', '/cms'],
  'indent-spaces' => 4

Defined Under Namespace

Classes: Cleaner

Constant Summary collapse

TIDY_LIB =

Specify path of Tidy library, e.g.

"/usr/lib/libtidy.A.dylib" (Mac; also the default if not set)
"/usr/lib/tidylib.so" (Ubuntu)
"/usr/lib/libtidy-0.99.so.0" (Fedora/CentOS)
defined?(::TIDY_LIB) ? ::TIDY_LIB : find_libtidy_path

Class Method Summary collapse

Class Method Details

.find_libtidy_pathObject

look for libtidy lib in different OSes



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack/tidy.rb', line 35

def self.find_libtidy_path
  [ "/usr/lib64/libtidy.so",    # CentOs 64bit
    "/usr/lib/libtidy.so",      # CentOs/Fedora
    "/usr/lib/tidylib.so",     # (Ubuntu)
    "/usr/lib/libtidy.A.dylib", # MacOS / default
  ].each{|p|
    if File.exist?(p) 
      puts "Found libtidy in: #{p}"
      return p         
    end
  }
  puts "html5-rack-tidy couldn't find the libtidy library in your system"
  puts "Run:  yum install libtidy-devel on Fedora/CentOS"
  raise "libtidy NOT FOUND!"
end

.new(backend, options = {}) ⇒ Object

Create a new Rack::Tidy middleware component that cleans text/html markup using the Tidy gem. The options Hash can be used to specify which paths should be ignored during processing as well as Tidy gem configuration values. See Cleaner for defaults and tidy.sourceforge.net/docs/quickref.html for more options



63
64
65
# File 'lib/rack/tidy.rb', line 63

def self.new(backend, options = {})
  Cleaner.new(backend, options)
end