|Previous:||A Web Interface to the Mail Tools||(See printing version)|
|Next:||Scientific Applications and Software Engineering|
Michel Goossens, IT/ASD
Tidy is a C program written by Dave Raggett (W3C) to help you write clean (and correct) HTML pages. It has been installed on all supported UNIX platforms at CERN (via asis).
We all know how easy it is to make mistakes when editing HTML files. Therefore an easy-to-install tool for fixing our mistakes (more or less) automatically and tidy up sloppy editing into nicely laid out markup is an HTML author's dream. This is what Tidy does. It is able to fix all by itself quite a wide range of problems. Moreover, it will bring to your attention things that Tidy cannot be sure of how to handle so that you take the necessary action yourself.
Tidy corrects the markup in a way that matches where possible the observed rendering in popular browsers from Netscape and Microsoft IE. Here are just a few examples of how Tidy perfects your HTML for you:
is mapped to
<p>here is a para <b>bold <i>bold italic</b> bold?</i> normal?
is mapped to
<p>here is a para <b>bold <i>bold italic</i> bold?</b> normal?
<h1><i>italic heading</h1> <p>new paragraph
In Netscape and Internet Explorer this causes everything following the heading to be in the heading font size, not the desired effect at all!
Tidy maps the example to
<h1><i>italic heading</i></h1> <p>new paragraph
<i><h1>heading</h1></i> <p>new paragraph <b>bold text <p>some more bold text
Tidy maps this to
<h1><i>heading</i></h1> <p>new paragraph <b>bold text</b> <p><b>some more bold text</b>
Tidy maps this to
<hr> <h1>heading</h1> <h2>sub</h2> <hr> <h2>heading</h2>
Tidy maps this to
<body> <li>1st list item <li>2nd list item
is mapped to
<body> <ul> <li>1st list item</li> <li>2nd list item</li> </ul>
Tidy inserts quote marks around all attribute values for you. It can also detect when you have forgotten the closing quote mark, although this is something you will have to fix yourself.
Tidy has a comprehensive knowledge of the attributes defined in the HTML 4.0 recommendation from W3C. This often allows you to spot where you have mistyped an attribute or value.
Tidy will even work out which version of HTML you are using and insert the appropriate DOCTYPE element, as per the W3C recommendations.
This is something you then have to fix yourself as Tidy is unsure of where the `>' should be inserted.
You can choose which style you want Tidy to use when it generates the cleaned-up markup: for instance, whether you like elements to indent their contents or not.
Tidy offers you a choice of character encodings: US ASCII, ISO Latin-1, UTF-8 and the ISO 2022 family of 7 bit encodings. The full set of HTML 4.0 entities are defined. Cleaned-up output uses HTML entity names for characters where appropriate. Otherwise, characters outside the normal range are output as numeric character entities.
Tidy offers advice on accessibility problems for people using non-graphical browsers. The most common thing you will see is the suggestion that you add a summary attribute to table elements. The idea is to provide a summary of the table's role and structure suitable for use with aural browsers.
Many tools generate HTML with an excess of FONT, NOBR and CENTER tags. Tidy's -clean option will replace them by style properties and rules using CSS. This makes the markup easier to read and maintain as well as reducing the file size. Tidy is expected to get smarter at this in the future.
XML processors compliant with W3C's XML 1.0 recommendation are very picky about which files they will accept. Tidy can help you to fix errors that cause your XML files to be rejected. Tidy does not yet recognize all XML features though, e.g. it does not yet understand CDATA sections or DTD subsets.
The -slides option allows you to split a single HTML file into a number of linked slides. Each H1 element in the input file is treated as delimiting the start of a slide. The slides are named slide1.html, slide2.html, slide3.html etc. This is a new feature and ideas are welcomed as to how to improve it.
<html> <head> </head> <body> <p> para which has enough text to cause a line break, and so test the wrapping mechanism for long lines. </p> <pre>This is <em>genuine preformatted</em> text </pre> <ul> <li> 1st list item </li> <li> 2nd list item </li> </ul> <!-- end comment --> </body> </html>
and this is the default style:
<html> <head> </head> <body> <p>para which has enough text to cause a line break, and so test the wrapping mechanism for long lines.</p> <pre>This is <em>genuine preformatted</em> text </pre> <ul> <li>1st list item </li> <li>2nd list item</li> </ul> <!-- end comment --> </body> </html>
tidy [[options] filename]*
If you run Tidy without any arguments, it will just sit there waiting to read markup on the stdin stream. Tidy's input and output default to stdin and stdout respectively. Errors are written to stderr but can be redirected to a file with the -f filename option.
I generally use the -m option to get Tidy to update the original file, and if the file is particularly bad I also use the -f option to write the errors to a file to make it easier to review them. Tidy supports a small set of character encoding options. The default is ASCII, which makes it easy to edit markup in regular text editors.
tidy -f errs.txt -m index.html
which runs Tidy on the file index.html updating it in place and writing the error messages to the file errs.txt. It is a good idea to save your work before running Tidy, as, like all complex software, Tidy may have bugs.
To get a list of available options use:
You should see something like this:
$ tidy -help tidy: file1 file2 ... Utility to clean up & pretty print html files see http://www.w3.org/People/Raggett/tidy/ options for tidy vers: 7th January 1999 -indent or -i indent element content -omit or -o omit optional endtags -wrap 72 wrap text at column 72 (default is 68) -upper or -u force tags to upper case -clean or -c replace font, nobr & center tags by CSS -raw leave chars > 128 unchanged upon output -ascii use ASCII for output, Latin-1 for input -latin1 use Latin-1 for both input and output -iso2022 use ISO2022 for both input and output -utf8 use UTF-8 for both input and output -numeric or -n output numeric rather than named entities -modify or -m to modify original files -errors or -e only show errors -f <file> write errors to <file> -xml use this when input is in xml -asxml to convert html to xml -slides to burst into slides on h1 elements -help or -h list command line options
Input and Output default to stdin and stdout, respectively. Single letter options apart from -f may be combined.
tidy -f errs.txt -imu foo.html
You can run Tidy on the present article and ask it to generate XML:
$ tidy -asxml tidy.html > tidy.xml Parsing "tidy.html" "tidy.html" appears to be HTML 2.0 no warnings or errors were found
If you compare the files tidy.html and tidy.xml, you will only find a difference at the beginning, where in the case of XML the first line contains an XML declaration.
Moreover, empty tags, such as <meta> get a slash at the end to adhere to the XML Specification, that is, in HTML you write:
<meta name="keywords" content="HTML, validation">
Correct XML syntax requires:
<meta name="keywords" content="HTML, validation" />
The blank preceeding the / is to ensure that the XML file can also be viewed with HTML browsers that do not yet understand the empty element syntax.
For matters related to this article please contact the author.