Skip to content

Instantly share code, notes, and snippets.

Created Feb 24, 2009
What would you like to do?
# HTML Tidy filter. This is pretty Unix-specific. It uses the external 'tidy'
# command, which it looks for using 'which', and communicates with using open3.
require 'open3'
module Tidy
# Runs the source through tidy and returns the result. If tidy isn't found,
# just returns the source as is. The source should be string-like.
# Takes an optional hash parameter, with the keys :strict and :verbose
# If :verbose is true, prints warning messages to stderr.
# The default is :verbose => false.
# If :strict is true, outputs XHTML with a strict doctype.
# If :strict is false, outputs XHTML with a transitional doctype.
# The default is :strict => true.
def Tidy.Tidy(source, params = {})
# We're strict unless we're specifically not.
strict = (params[:strict] == false ? false : true)
# We're not verbose unless we specificaly are.
verbose = params[:verbose] || false
# Find tidy, return unaltered source if we can't.
tidy = `which tidy 2>&1`
if (tidy =~ /(not found)/) || tidy.empty?
if verbose
$stderr.puts "tidy not found."
return source
# Set tidy's options
tidy_options = ['--indent auto', '--output-xhtml yes', '--quiet yes',
'--tidy-mark no', '--char-encoding utf8', '--wrap 78']
if strict
tidy_options << '--doctype strict'
tidy_options << '--doctype transitional'
# Make it go.
Open3.popen3("tidy #{tidy_options.join(' ')}") {|tidy_in,tidy_out,tidy_err|
tidy_in.write source
@result =
@errors =
if verbose
$stderr.puts @errors
return @result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment