|
| 1 | +#!/usr/local/bin/perl |
| 2 | +# sub TOC builder |
| 3 | +# |
| 4 | +# Arnaud Le Hors - lehors@w3.org |
| 5 | +# $Id: mksubtoc,v 1.1 1997-08-05 17:31:59 ijacobs Exp $ |
| 6 | + |
| 7 | +if (($_ = $ARGV[0], /^-r/) && $ARGV[0]) { |
| 8 | + shift; |
| 9 | + $realpath = $ARGV[0]; |
| 10 | + shift; |
| 11 | +} else { |
| 12 | + $realpath = (); |
| 13 | +} |
| 14 | + |
| 15 | +if (($_ = $ARGV[0], /^-l/) && $ARGV[0]) { |
| 16 | + shift; |
| 17 | + $startlvl = $ARGV[0] - 1; |
| 18 | + shift; |
| 19 | +} else { |
| 20 | + $startlvl = 0; |
| 21 | +} |
| 22 | + |
| 23 | +$PROGNAME = substr($0, rindex($0, "/") + 1); |
| 24 | + |
| 25 | +if ($#ARGV < 2) { |
| 26 | + print STDERR "Usage: $PROGNAME [ -r realpath ] [ -l startlvl ] subtoc headingdb src1 src2 ...\n"; |
| 27 | + exit 1; |
| 28 | +} else { |
| 29 | + $tocf = $ARGV[0]; |
| 30 | + shift; |
| 31 | + if (! $realpath) { |
| 32 | + $realpath = $tocf; |
| 33 | + } |
| 34 | + $hdbasef = $ARGV[0]; |
| 35 | + shift; |
| 36 | +} |
| 37 | + |
| 38 | +# compute relative path from 1 to 2 |
| 39 | +sub rpath { |
| 40 | + @path1 = split("/", $_[0]); |
| 41 | + @path2 = split("/", $_[1]); |
| 42 | + |
| 43 | + pop(@path1); |
| 44 | + while ($path1[0] eq $path2[0]) { |
| 45 | + shift(@path1); |
| 46 | + shift(@path2); |
| 47 | + } |
| 48 | + |
| 49 | + $root = ""; |
| 50 | + foreach $el (@path1) { |
| 51 | + $root .= "../"; |
| 52 | + } |
| 53 | + $path = join("/", @path2); |
| 54 | + return "$root$path"; |
| 55 | +} |
| 56 | + |
| 57 | +# copy file in memory |
| 58 | +sub readfile { |
| 59 | + $buf = ""; |
| 60 | + if (!open(INPUT, $_[0])) { |
| 61 | + print STDERR "$PROGNAME Error: Cannot open file: $_[0]\n"; |
| 62 | + return; |
| 63 | + } |
| 64 | + while (<INPUT>) { |
| 65 | + $buf .= $_; |
| 66 | + } |
| 67 | + close(INPUT); |
| 68 | +} |
| 69 | + |
| 70 | +sub olinit { |
| 71 | + @ol = (); |
| 72 | + $ol[0] = 0; |
| 73 | +} |
| 74 | + |
| 75 | +sub ol { |
| 76 | +# args: $h |
| 77 | + $hn = $_[0] - 1; |
| 78 | + for ($i = $#n; $i > $hn; $i--) { |
| 79 | + if ($ol[$i] != 0) { |
| 80 | + print OUTPUT "</ol>\n"; |
| 81 | + $ol[$i] = 0; |
| 82 | + } |
| 83 | + $n[$i] = 0; |
| 84 | + } |
| 85 | + if ($hn >= 0) { |
| 86 | + if ($ol[$hn] == 0) { |
| 87 | + print OUTPUT "<ol>\n"; |
| 88 | + $ol[$hn] = 1; |
| 89 | + } |
| 90 | + $n[$hn]++; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +# remove every possible anchor |
| 95 | +sub cleanup { |
| 96 | + $text = $_[0]; |
| 97 | + $text =~ s/<a(.*?)>//sgi; |
| 98 | + $text =~ s/<\/a>//sgi; |
| 99 | + return $text; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +### main |
| 104 | + |
| 105 | +# read heading database |
| 106 | +readfile($hdbasef); |
| 107 | + |
| 108 | +# search for all the headings related to the given files and build the toc |
| 109 | +open(OUTPUT, "> $tocf"); |
| 110 | +olinit(); |
| 111 | +foreach $file (@ARGV) { |
| 112 | + # change extension |
| 113 | + $hfil = $file; |
| 114 | + $hfil =~ s/\.src/\.html/; |
| 115 | + $rpath = rpath($realpath, $hfil); |
| 116 | + $_ = $buf; |
| 117 | + while (/$file;(.*?);([0-9]*);([1-9].*?)\n/si) { |
| 118 | + $t = $1; |
| 119 | + $h = int($2); |
| 120 | + if ($h > $startlvl && $h < 4) { |
| 121 | + $num = $3; |
| 122 | + ol($h); |
| 123 | + if ($h == 1) { # if H1 go to the top of the page and set rel |
| 124 | + $url = "$rpath"; |
| 125 | + $rel= " rel=\"section\""; |
| 126 | + } else { |
| 127 | + $url = "$rpath#h-$num"; |
| 128 | + $rel = ""; |
| 129 | + } |
| 130 | + $txt = cleanup($t); |
| 131 | + print OUTPUT "<li><a href=\"$url\"$rel>$txt</a>\n"; |
| 132 | + } |
| 133 | + $_ = $'; |
| 134 | + } |
| 135 | +} |
| 136 | +ol(0); # close all open lists |
| 137 | +close(OUTPUT); |
0 commit comments