-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathmktoc
86 lines (72 loc) · 2.61 KB
/
mktoc
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
80
81
82
83
84
85
86
#!/usr/local/bin/perl
# Generate a table of contents plus a database of headings
# When an HTML comment is found right before the end tag of a header its
# content is added as a description note to the TOC like this:
# 1. Some header - and its related comment
# If a TITLE attribute is found it has the same effect, and overrides
# the comment.
#
# Arnaud Le Hors - lehors@w3.org
# $Id: mktoc,v 2.6 2006-10-09 18:55:52 ihickson Exp $
use DB_File;
use Getopt::Std;
use lib 'bin';
use utils;
$PROG = substr($0, rindex($0, "/") + 1);
$USAGE = "Usage: $PROG [-l startlvl] [-h maxlvl] [-c config] [-r chapter] headingdb [output]
\t-r to generate a ToC only for that chapter (default all chapters)
\t-c to choose a different configuration file (default Project.cfg)
\t-l to generate only entries for header level startlvl and above
\t-h to generate only entries for header level maxlvl and below\n";
@indent = (""," "," "," "," "," "," ");
### main
getopts('l:h:r:c:') || die $USAGE;
if ($#ARGV >= 0) {$tocdb = $ARGV[0]; shift;} else {die $USAGE;}
if ($#ARGV >= 0) {$output = $ARGV[0]; shift;} else {$output = '-';}
if ($#ARGV >= 0) {die $USAGE;}
dbmopen(%anchors, $tocdb, 0666) || die "$PROG: cannot open database $tocdb\n";
my $lo = defined $opt_l ? int($opt_l) : 1;
my $hi = defined $opt_h ? int($opt_h) : 6;
my $config = defined $opt_c ? $opt_c : 'Project.cfg';
read_config($config);
open(OUT, ">$output") || die "$PROG: cannot write to $output\n";
my ($start, $end, $i, $chap, $prev, $file);
if (defined $opt_r) { # For one file only
if (! defined $lookup{$opt_r}) {die "$PROG: $opt_r not found in config\n";}
$start = $end = $lookup{$opt_r};
} else { # For all files
$start = 0;
$end = $#chapter;
}
$prev = $lo - 1;
for ($chap = $start; $chap <= $end; $chap++) {
$file = $chapter[$chap];
$i = 0;
my $key = "$file\t$i";
while (($entry = $anchors{$key})) {
my ($fn, $j, $txt, $lvl, $hnum, $anchor, $cmt) = split(/\t/, $entry);
if ($lo <= $lvl && $lvl <= $hi) {
while ($prev < $lvl) {
print OUT "$indent[$prev]<ul class=\"toc\">\n";
$prev++;
}
while ($prev > $lvl) {
$prev--;
print OUT "$indent[$prev]</ul>\n";
}
print OUT "$indent[$lvl]<li class=\"tocline$lvl\">";
# print OUT "<a href=\"$file$anchor\" class=\"toc-link\">$hnum $txt</a>";
print OUT "<a href=\"$file$anchor\" class=\"tocxref\">$hnum $txt</a>";
if ($cmt && $cmt ne "") {print OUT " - <em>$cmt</em>";}
print OUT "\n";
}
$i++;
$key = "$file\t$i";
}
}
while ($prev >= $lo) {
$prev--;
print OUT "$indent[$prev]</ul>\n";
}
dbmclose(%anchors);
close(OUT);