3 # $Cambridge: exim/doc/doc-docbook/TidyHTML-filter,v 1.2 2005/11/10 12:30:13 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.
10 # 3. Turn <div class="literallayout"><p> into <div class="literallayout"> and
11 # a matching </p></div> into </div> to get rid of unwanted vertical white
18 # Read in the filter.html file.
20 open(IN, "filter.html") || die "Failed to open filter.html for reading: $!\n";
24 # Insert a newline after every > because the whole toc is generated as one
25 # humungous line that is hard to check. Then split the lines so that each one
26 # is a separate element in the vector.
28 foreach $line (@text) { $line =~ s/>\s*/>\n/g; }
29 for ($i = 0; $i < scalar(@text); $i++)
30 { splice @text, $i, 1, (split /(?<=\n)/, $text[$i]); }
32 # We want to create reverse links from each chapter and section title back to
33 # the relevant place in the TOC. Scan the TOC for the relevant entries. Add
34 # an id to each entry, and create tables that remember the new link ids. We
35 # detect the start of the TOC by <div class="toc" and the end of the TOC by
36 # <div class="chapter".
38 # Skip to start of TOC
40 for ($i = 0; $i < scalar(@text); $i++)
42 last if $text[$i] =~ /^<div class="toc"/;
47 for (; $i < scalar(@text); $i++)
49 last if $text[$i] =~ /^<div class="chapter"/;
50 if ($text[$i] =~ /^<a href="(#[^"]+)">/)
53 my($id) = sprintf "%04d", $tocref++;
54 $text[$i] =~ s/<a/<a id="toc$id"/;
55 $backref{"$ss"} = "toc$id";
59 # Scan remainder of the document
61 for (; $i < scalar(@text); $i++)
63 if ($text[$i] eq "<div class=\"literallayout\">\n" && $text[$i+1] eq "<p>\n")
68 elsif ($thisdiv && $text[$i] eq "</p>\n" && $text[$i+1] eq "</div>\n")
73 elsif ($text[$i] =~ /^<h[23] /)
76 if ($text[$i] =~ /^<a( xmlns="[^"]+")? id="([^"]+)">$/)
78 my($ref) = $backref{"#$2"};
79 $text[$i++] = "<a$1 href=\"#$ref\" id=\"$2\">\n";
80 my($temp) = $text[$i];
81 $text[$i] = $text[$i+1];
87 # Write out the revised file
89 open(OUT, ">filter.html") || die "Failed to open filter.html for writing: $!\n";