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