Skip to content

Instantly share code, notes, and snippets.

@ingramj
Created February 24, 2009 19:23
Show Gist options
  • Save ingramj/69736 to your computer and use it in GitHub Desktop.
Save ingramj/69736 to your computer and use it in GitHub Desktop.
# 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."
end
return source
end
# 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'
else
tidy_options << '--doctype transitional'
end
# Make it go.
Open3.popen3("tidy #{tidy_options.join(' ')}") {|tidy_in,tidy_out,tidy_err|
tidy_in.write source
tidy_in.close
@result = tidy_out.read
@errors = tidy_err.read
}
if verbose
$stderr.puts @errors
end
return @result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment