#!/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]\n"; } print OUT "$indent[$lvl]
  • "; # print OUT "$hnum $txt"; print OUT "$hnum $txt"; if ($cmt && $cmt ne "") {print OUT " - $cmt";} print OUT "\n"; } $i++; $key = "$file\t$i"; } } while ($prev >= $lo) { $prev--; print OUT "$indent[$prev]\n"; } dbmclose(%anchors); close(OUT);