3 # $Cambridge: exim/doc/doc-docbook/TidyHTML-filter,v 1.1 2005/06/16 10:32:31 ph10 Exp $
5 # Script to tidy up the filter HTML file that is generated by xmlto. The
6 # following changes are made:
8 # 1. Split very long lines.
9 # 2. Create reverse links from chapter and section titles back to the TOC.
14 # Read in the filter.html file.
16 open(IN, "filter.html") || die "Failed to open filter.html for reading: $!\n";
20 # Insert a newline after every > because the whole toc is generated as one
21 # humungous line that is hard to check. Then split the lines so that each one
22 # is a separate element in the vector.
24 foreach $line (@text) { $line =~ s/>\s*/>\n/g; }
25 for ($i = 0; $i < scalar(@text); $i++)
26 { splice @text, $i, 1, (split /(?<=\n)/, $text[$i]); }
28 # We want to create reverse links from each chapter and section title back to
29 # the relevant place in the TOC. Scan the TOC for the relevant entries. Add
30 # an id to each entry, and create tables that remember the new link ids. We
31 # detect the start of the TOC by <div class="toc" and the end of the TOC by
32 # <div class="chapter".
34 # Skip to start of TOC
36 for ($i = 0; $i < scalar(@text); $i++)
38 last if $text[$i] =~ /^<div class="toc"/;
43 for (; $i < scalar(@text); $i++)
45 last if $text[$i] =~ /^<div class="chapter"/;
46 if ($text[$i] =~ /^<a href="(#[^"]+)">/)
49 my($id) = sprintf "%04d", $tocref++;
50 $text[$i] =~ s/<a/<a id="toc$id"/;
51 $backref{"$ss"} = "toc$id";
55 # Scan remainder of the document
57 for (; $i < scalar(@text); $i++)
59 if ($text[$i] =~ /^<h[23] /)
62 if ($text[$i] =~ /^<a( xmlns="[^"]+")? id="([^"]+)">$/)
64 my($ref) = $backref{"#$2"};
65 $text[$i++] = "<a$1 href=\"#$ref\" id=\"$2\">\n";
66 my($temp) = $text[$i];
67 $text[$i] = $text[$i+1];
73 # Write out the revised file
75 open(OUT, ">filter.html") || die "Failed to open filter.html for writing: $!\n";