#!/usr/local/bin/perl # Add navigation "bars" containing a link for next and previous LINKs # plus one for any url given in argument # # ToDo: compare URLs to find shortest relative path # # Arnaud Le Hors - lehors@w3.org # Modified by Bert Bos # $Id: addnavbar,v 2.3 2002-07-08 14:59:08 bbos Exp $ use Getopt::Long; use lib 'bin'; use utils; my $PROG = substr($0, rindex($0, "/") + 1); my $USAGE = "Usage: $PROG [options] [file [output]] \t-c=config to give configuration file (default Project.cfg) \t-r=realpath to give real name of file (default is file itself) \t-top to suppress navigation bar at the top of the page \t-bottom to suppress navigation bar at the bottom of the page \t-hr to suppress horizontal rules \t-prev to suppress link to previous page \t-next to suppress link to next page \t-toc to suppress link to table of contents \t-omit=rel to omit other relations in config file (can be repeated)\n"; ### main my %options; GetOptions(\%options, 'top', 'bottom', 'hr', 'prev', 'next', 'toc', 'omit=s@', 'r=s', 'c=s') or die $USAGE; if ($#ARGV >= 0) {$input = $ARGV[0]; shift;} else {$input = '-';} if ($#ARGV >= 0) {$output = $ARGV[0]; shift;} else {$output = '-';} if ($#ARGV >= 0) {die $USAGE;} my $name = defined $options{'r'} ? $options{'r'} : $input; my $configfile = defined $options{'c'} ? $options{'c'} : 'Project.cfg'; # Read configuration and find info about current file read_config($configfile); defined $lookup{$name} or die "$PROG: file $name not found in config file\n"; $chapno = $lookup{$name}; open(IN, $input) or die "$PROG: cannot read file $input\n"; open(OUT, ">$output") or die "$PROG: cannot write to file $output\n"; my $done = 0; my $i; # Build the navigation bar my $navbar = "\n
\n

"; $navbar .= "previous  \n" unless $chapno < 1 or defined $options{'prev'}; $navbar .= "next  \n" unless $chapno >= $#chapter or defined $options{'next'}; $navbar .= "contents  \n" unless !defined $contents or defined $options{'toc'}; for ($i = 0; $i <= $#links; $i++) { $navbar .= "$tonavbar[$i]  \n" unless $tonavbar[$i] eq '' or grep(/$relations[$i]/, $options{'omit'}); } $navbar .= "

\n"; # Insert the navigation bar after the tag. (There better be one!) if (! defined $options{'top'}) { while () { /]*>/io and do { print OUT $`; print OUT $&; print OUT $navbar; print OUT "
\n" unless defined $options{'hr'}; print OUT $'; $done++; last; }; print OUT; } } # Insert the navigation bar before the tag if (! defined $options{'bottom'}) { while () { /<\/body[^>]*>/io and do { print OUT $`; print OUT "
\n" unless defined $options{'hr'}; print OUT $navbar; print OUT $&; print OUT $'; $done++; last; }; print OUT; } } # Copy the rest of the file while () { print OUT; } if (!defined $options{'top'}) {$done--;} if (!defined $options{'bottom'}) {$done--;} if ($done < 0) { warn "$PROG: $input had no / tags; no navbar inserted\n"; } # Finalize close(IN); close(OUT);