3 # $Cambridge: exim/doc/doc-docbook/TidyHTML-filter,v 1.3 2006/02/01 11:01:01 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 > in the toc, because the whole toc is generated
25 # as one humungous line that is hard to check. Indeed, the start of the first
26 # chapter is also on the line, so we have to split if off first. Having
27 # inserted newlines, we split the toc into separate items in the vector.
29 for ($i = 0; $i < scalar(@text); $i++)
31 if ($text[$i] =~ ?<title>Exim's interfaces to mail filtering</title>?)
33 splice @text, $i, 1, (split /(?=<div class="chapter")/, $text[$i]);
34 $text[$i] =~ s/>\s*/>\n/g;
35 splice @text, $i, 1, (split /(?<=\n)/, $text[$i]);
40 # We want to create reverse links from each chapter and section title back to
41 # the relevant place in the TOC. Scan the TOC for the relevant entries. Add
42 # an id to each entry, and create tables that remember the new link ids. We
43 # detect the start of the TOC by <div class="toc" and the end of the TOC by
44 # <div class="chapter".
46 # Skip to start of TOC
48 for ($i = 0; $i < scalar(@text); $i++)
50 last if $text[$i] =~ /^<div class="toc"/;
55 for (; $i < scalar(@text); $i++)
57 last if $text[$i] =~ /^<div class="chapter"/;
58 if ($text[$i] =~ /^<a href="(#[^"]+)">/)
61 my($id) = sprintf "%04d", $tocref++;
62 $text[$i] =~ s/<a/<a id="toc$id"/;
63 $backref{"$ss"} = "toc$id";
67 # Scan remainder of the document
69 for (; $i < scalar(@text); $i++)
72 /^(.*)<a( xmlns="[^"]+")? id="([^"]+)"><\/a>(.*?)<\/h(.*)/)
74 my($ref) = $backref{"#$2"};
75 $text[$i] = "$1<a$2 href=\"#$ref\" id=\"$3\">$4</a></h$5";
78 if ($text[$i] =~ /^(.*)<div class="literallayout"><p>(?:<br \/>)?(.*)/)
81 $text[$i] = "$1<div class=\"literallayout\">$2";
83 for ($j = $i + 1; $j < scalar(@text); $j++)
85 if ($text[$j] =~ /^<\/p><\/div>/)
87 $text[$j] =~ s/<\/p>//;
94 # Write out the revised file
96 open(OUT, ">filter.html") || die "Failed to open filter.html for writing: $!\n";