-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathaddnavbar
More file actions
181 lines (160 loc) · 3.49 KB
/
addnavbar
File metadata and controls
181 lines (160 loc) · 3.49 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/local/bin/perl
# Add navigation "bars" containing a link for next and previous LINKs
# plus one for any url given in argument
#
# Arnaud Le Hors - lehors@w3.org
# $Id: addnavbar,v 1.5 1997-11-20 23:46:44 ian Exp $
if ($ARGV[0] eq "-top") {
$addtop = ();
shift;
} else {
$addtop = true;
}
if ($ARGV[0] eq "-bottom") {
$addbottom = ();
shift;
} else {
$addbottom = true;
}
if ($ARGV[0] eq "-hr") {
$hr = "";
shift;
} else {
$hr = "<hr>";
}
if ($ARGV[0] eq "-prev") {
$addprev = ();
shift;
} else {
$addprev = true;
}
if ($ARGV[0] eq "-next") {
$addnext = ();
shift;
} else {
$addnext = true;
}
if (($ARGV[0] eq "-r") && $ARGV[1]) {
shift;
$realpath = $ARGV[0];
shift;
} else {
$realpath = ();
}
$PROGNAME = substr($0, rindex($0, "/") + 1);
if ($#ARGV < 1) {
print "Usage: $PROGNAME [-top] [-bottom] [-hr] [-prev] [-next] [-r realpath] src trgt [name1 url1] [name2 url2] ...
\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 horinzontal rules
\t-prev to suppress link to previous page
\t-next to suppress link to next page\n";
exit 1;
} else {
$input = $ARGV[0];
shift;
if (! $realpath) {
$realpath = $input;
}
$output = $ARGV[0];
shift;
while ($ARGV[0] && $ARGV[1]) {
$names[++$#names] = $ARGV[0];
$urls[++$#urls] = $ARGV[1];
shift; shift;
}
}
### main
# copy file in memory
$buf = "";
if (!open(INPUT, $input)) {
print STDERR "$PROGNAME Error: Cannot open file: $input\n";
exit 1;
}
while (<INPUT>) {
$buf .= $_;
}
close(INPUT);
# compute relative path from 1 to 2
sub rpath {
@path1 = split("/", $_[0]);
@path2 = split("/", $_[1]);
pop(@path1);
while ($path1[0] eq $path2[0]) {
shift(@path1);
shift(@path2);
}
$root = "";
foreach $el (@path1) {
$root .= "../";
}
$path = join("/", @path2);
return "$root$path";
}
# args: tag
sub addnavbar {
($elem, $tag, $prev, $next) = @_;
if ($prev && $addprev) {
$navbar = "<a href=\"$prev\">previous</a>";
} else {
$navbar = "";
}
if ($next && $addnext) {
if ($prev && $addprev) {
$navbar .= " ";
}
$navbar .= "<a href=\"$next\">next</a>";
}
for ($i = 0; $i <= $#names; $i++) {
$path = rpath($realpath, $urls[$i]);
$navbar .= " <a href=\"$path\">$names[$i]</a>";
}
if (lc($tag) eq "body") {
if ($addtop) {
return
"$elem\n<div class=\"navbar\">\n<center> $navbar\n</center>"
. $hr . "\n</div>\n";
} else {
return "$elem";
}
} else {
if ($addbottom) {
return "<div class=\"navbar\">\n" . $hr .
"<center>$navbar\n</center></div>\n$elem";
} else {
return "$elem";
}
}
}
$nexttag="<link[ \t\n]+rel=(next|\"next\")[ \t\n]+href=(.*?)>";
$prevtag="<link[ \t\n]+rel=(prev|\"prev\"|previous|\"previous\")[ \t\n]+href=(.*?)>";
$_ = $buf;
if (/$nexttag/sio) {
# we don't want the quotes
$next = $2;
$next =~ s/\"//go;
} else {
$next = ();
}
$_ = $buf;
if (/$prevtag/sio) {
# we don't want the quotes
$prev = $2;
$prev =~ s/\"//go;
} else {
$prev = ();
}
if ($next || $prev) {
# insert navigation bar after <body>
$buf =~ s/(<(body).*?>)/
addnavbar($1, $2, $prev, $next)/sgieo;
}
if ($next || $prev) {
# insert navigation bar before </body>
$buf =~ s/(<(\/body)>)/
addnavbar($1, $2, $prev, $next)/sgieo;
}
# print out result
open(OUTPUT, "> $output");
print OUTPUT $buf;
close(OUTPUT);