-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathaddnavbar
More file actions
110 lines (95 loc) · 3.27 KB
/
addnavbar
File metadata and controls
110 lines (95 loc) · 3.27 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/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 <bert@w3.org>
# $Id: addnavbar,v 2.5 2006-10-09 18:55:52 ihickson 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<div class=\"navbar\">\n<p>";
$navbar .= "<a href=\"$chapter[$chapno-1]\">previous</a> \n"
unless $chapno < 1 or defined $options{'prev'};
$navbar .= "<a href=\"$chapter[$chapno+1]\">next</a> \n"
unless $chapno >= $#chapter or defined $options{'next'};
$navbar .= "<a href=\"$contents\">contents</a> \n"
unless !defined $contents or defined $options{'toc'};
for ($i = 0; $i <= $#links; $i++) {
$navbar .= "<a href=\"$links[$i]\">$tonavbar[$i]</a> \n"
unless $tonavbar[$i] eq '' or grep(/$relations[$i]/, $options{'omit'});
}
$navbar .= "</div>\n";
# Insert the navigation bar after the <BODY> tag. (There better be one!)
if (! defined $options{'top'}) {
while (<IN>) {
/<body[^>]*>/io and do {
print OUT $`;
print OUT $&;
print OUT $navbar;
print OUT "<hr class=\"navbar\">\n" unless defined $options{'hr'};
print OUT $';
$done++;
last;
};
print OUT;
}
}
# Insert the navigation bar before the </BODY> tag
if (! defined $options{'bottom'}) {
while (<IN>) {
/<\/body[^>]*>/io and do {
print OUT $`;
print OUT "<hr class=\"navbar\">\n" unless defined $options{'hr'};
print OUT $navbar;
print OUT $&;
print OUT $';
$done++;
last;
};
print OUT;
}
}
# Copy the rest of the file
while (<IN>) {
print OUT;
}
if (!defined $options{'top'}) {$done--;}
if (!defined $options{'bottom'}) {$done--;}
if ($done < 0) {
warn "$PROG: $input had no <BODY>/</BODY> tags; no navbar inserted\n";
}
# Finalize
close(IN);
close(OUT);