#! /usr/bin/perl # Copyright (c) 1997-1999 by Alex Schroeder $usage = 'Usage: overview.pl [-hd] file.html [...] OPTIONS -h this message -d debuggin -v verbose This script reads all the headings level 2 to 6 and constructs a table of contents for the page. This replaces the first paragraph starting with any of the following (and ended by an empty line):

Overview: ...

Übersicht: ...

Sommaire: ... '; require "getopts.pl"; &Getopts('hdv'); if($opt_h) { print $usage; exit; } if($opt_d) { $debug = 1; $verbose = 1; print "Debugging...\n"; } if($opt_v) { $verbose = 1; print "Verbose...\n"; } # Convert all files in the current directory if no file spec given on # the command line. if ($#ARGV < 0) { opendir(DIR,'.') || die "Can't open current directory: $!\n"; @srcfiles = grep(/\.html$/,readdir(DIR)); print "Files:\n", join("\n", @srcfiles), "\n" if $debug; close(DIR); } else { print "Files:\n" if $debug; foreach (@ARGV) { push(@srcfiles, $_); print "$_\n" if $debug; } } foreach $srcfile (@srcfiles) { if($srcfile =~ /~$/) { print "$srcfile is a backup. Skipping...\n" if $verbose; next; } # Read file into $html. undef $/; open(DATA, $srcfile); $original = $html = ; close(DATA); # Generation of an overview: an unordered list of headers level # 2-6 (this assumes that

was only used once, at the beginning # of the document). Overview stored in $overview. &overview; # If no overview requested, skip the file. Note that $overview # ends with a \n. if ( $html !~ s/

(Übersicht|Overview|Sommaire):.*? /

$1:\n$overview /si ) { print STDERR "No overview requested for $srcfile.\n" if $verbose; next; } # Don't touch the file if nothing has changed. if ( $original eq $html ) { print "No changes in $srcfile.\n" if $verbose; next; } # Backup old HTML file. rename $srcfile, $srcfile . '~'; # Write new HTML file. open(OUT,"> $srcfile") || die "Can't write to $srcfile: $!\n"; print OUT $html; close(OUT); print "Added overview to $srcfile.\n" if $verbose; } sub overview { my $language = $1; my $anchor = 0; my $level = 1; my @replacements = (); $overview = "

\n"; while ( $html =~ m|()?(.*?)|isg ) { $anchor++; my $this_level = $1; my $title = $3; print STDERR "$anchor, $this_level, $title\n" if $debug; while ($this_level > $level) { $level++; $overview .= substr(" ", 0, ($level -2) * 2) . "

\n"; $level--; } (my $ptitle = $title) =~ s/<.*?>//g; $ptitle =~ tr/ \n\r\t/ /s; $overview .= substr(" ", 0, ($level -2) * 2) . "
  • $ptitle\n"; my $qtitle = quotemeta $title; push @replacements, "\$html =~ s|()?$qtitle|$qtitle|is"; } $overview .= "\n" while $level-- > 1; print STDERR $overview if $debug; print STDERR join ("\n", @replacements) if $debug; eval $command while $command = pop @replacements; print STDERR "\n" if $debug; } exit;