3 # $Cambridge: exim/doc/doc-docbook/TidyHTML-spec,v 1.2 2005/11/10 12:30:13 ph10 Exp $
5 # Script to tidy up the spec HTML files that are generated by xmlto. The
6 # following changes are made:
8 # 1. Tidy the index.html file by splitting the very long lines.
9 # 2. Create reverse links from chapter and section titles back to the TOC.
10 # 3. Tidy the ix01.html file - the actual index - by splitting long lines.
11 # 4. Insert links from the letter divisions to the top of the Index.
12 # 5. Turn <div class="literallayout"><p> into <div class="literallayout"> and
13 # a matching </p></div> into </div> to get rid of unwanted vertical white
15 # 6. Before each occurrence of </td> insert so that the table's cell
16 # is a little bit wider than the text itself.
22 # Read in the index.html file. It's really the TOC.
24 open(IN, "index.html") || die "Failed to open index.html for reading: $!\n";
28 # Insert a newline after every > because the whole toc is generated as one
29 # humungous line that is hard to check. Then split the lines so that each one
30 # is a separate element in the vector.
32 foreach $line (@toc) { $line =~ s/>\s*/>\n/g; }
33 for ($i = 0; $i < scalar(@toc); $i++)
34 { splice @toc, $i, 1, (split /(?<=\n)/, $toc[$i]); }
36 # We want to create reverse links from each chapter and section title back to
37 # the relevant place in the TOC. Scan the TOC for the relevant entries. Add
38 # an id to each entry, and create tables that remember the file names and the
43 if ($line =~ /^<a href="((?:ch|ix)\d+\.html)(#[^"]+)?">/)
47 my($id) = sprintf "%04d", $tocref++;
48 $line =~ s/<a/<a id="toc$id"/;
49 $backref{"$chix$ss"} = "toc$id";
54 # Write out the modified index.html file.
56 open (OUT, ">index.html") || die "Failed to open index.html for writing: $!\n";
60 # Now scan each of the other page files and insert the reverse links. While
61 # we are at it, we tidy up <div class="literallayout"> by removing unwanted
62 # paragraph marks, which generate unwanted vertical space. We also insert
63 # before </td> to push table cells apart from each other.
65 foreach $file (@chlist)
67 open(IN, "$file") || die "Failed to open $file for reading: $!\n";
71 # Insert a newline after certain elements, and split the lines so that each
72 # one is a separate element in the vector. This makes it easier to recognize
77 $line =~ s/<p>\s*(?!\n)/<p>\n/g;
78 $line =~ s/<\/p>\s*(?!\n)/<\/p>\n/g;
79 $line =~ s/<\/div>\s*(?!\n)/<\/div>\n/g;
80 $line =~ s/<div([^>]*)>(?!\n)/<div$1>\n/g;
83 for ($i = 0; $i < scalar(@text); $i++)
84 { splice @text, $i, 1, (split /(?<=\n)/, $text[$i]); }
88 for ($i = 0; $i < scalar(@text); $i++)
90 if ($text[$i] =~ /^(.*?)<a( xmlns="[^"]+")? id="([^"]+)"><\/a>(.+?)<\/h(.*)$/)
92 my($pre, $opt, $id, $title, $post) = ($1, $2, $3, $4, $5);
95 my($ref) = $backref{"$file#$id"};
97 # If not found, try for a chapter reference
98 $ref = $backref{"$file"} if !defined $ref;
101 $text[$i]= "$pre<a$opt href=\"index.html#$ref\" id=\"$id\">$title</a></h$post";
104 elsif ($text[$i] eq "<div class=\"literallayout\">\n" && $text[$i+1] eq "<p>\n")
109 elsif ($thisdiv && $text[$i] eq "</p>\n" && $text[$i+1] eq "</div>\n")
114 elsif ($text[$i] =~ /^\s*<\/td>/)
116 $text[$i] = " $text[$i]";
120 open(OUT, ">$file") || die "Failed to open $file for writing: $!\n";
125 # Now process the ix01.html file
127 open(IN, "ix01.html") || die "Failed to open ix01.html for reading: $!\n";
131 # Insert a newline after every > because the whole index is generated as one
132 # humungous line that is hard to check. Then split the lines so that each one
133 # is a separate element in the vector.
135 foreach $line (@index) { $line =~ s/>\s*/>\n/g; }
136 for ($i = 0; $i < scalar(@index); $i++)
137 { splice @index, $i, 1, (split /(?<=\n)/, $index[$i]); }
139 # We want to add a list of letters at the top of the index, and link back
140 # to them from each letter heading. First find the index title and remember
141 # where to insert the list of letters.
143 for ($i = 0; $i < scalar(@index); $i++)
145 if ($index[$i] =~ /^<\/h2>$/)
152 # Now scan through for the letter headings and build the cross references,
153 # while also building up the list to insert.
156 for (; $i < scalar(@index); $i++)
158 if ($index[$i] =~ /^(.)<\/h3>$/)
161 $index[$i-1] =~ s/^/<a id="${letter}B" href="#${letter}T">/;
162 $index[$i] =~ s/$/<\/a>/;
163 $list .= "<a id=\"${letter}T\" href=\"#${letter}B\"> $letter</a>\n";
167 # Now we know which letters we have, we can insert the list.
170 splice @index, $listindex, 0, $list;
172 # Write out the modified index.html file.
174 open (OUT, ">ix01.html") || die "Failed to open ix01.html for writing: $!\n";