I’m currently writing on a small paper for a course about web accessibility by Giorgio Brajnik of the University of Udine and the requirements for the paper are XHTML. Since every paper in my opinion has to have a table of contents, this also counts for the XHTML paper ;) But updating the TOC everytime you make a change manually?! Nah. It’s one of these times when I happy that I can code ;)
The resulting script can be found here. It’s a small Ruby program parsing the XHTML document for h[2-6] tags and building a small toc out of them. Just to give a small example:
<html> <body> <h1>My little article</h1> <!-- <%= toc %> --> <h2>1</h2> <h2>2</h2> <h3>2.1</h3> </body> </html>
htmltoc.rb -i input.html -o output.html the <%= toc %> call will have been replaced with following listing:
<ul id="_maintoc"> <li>1</li> <li>2 <ul> <li>2.1</li> </ul> </li> </ul>
(In fact the tabbing was just added by me manually to make it better readable ;) )
If you give each header and id, the TOC entry will be a link pointing to that id in the document.
Another neat feature is, that you can tell the parser to skip certain chapters (incl. subchapters) by simply assigning the “notoc” class to the respective header element.
I hope you like it :)