-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathaddrefclass
More file actions
executable file
·97 lines (88 loc) · 2.14 KB
/
addrefclass
File metadata and controls
executable file
·97 lines (88 loc) · 2.14 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
#!/usr/local/bin/perl -w
# sub TOC builder
#
# Ian Jacobs/Renaud Marlet
# $Id: addrefclass,v 1.1 1997-11-20 23:35:15 ian Exp $
# To use long names like $INPUT_RECORD_SEPARATOR.
use English;
$PROGNAME = substr($0, rindex($0, "/") + 1);
if ($#ARGV < 1) {
warn "Usage: $PROGNAME refdb file1 file2 ...\n";
exit 1;
} else {
$refdb = $ARGV[0];
shift;
}
# copy file in memory
sub readdb {
# Keys in rdbase{} are reference keys (e.g., HTML40).
$rdbase = {};
$buf = "";
if (!open(DBASE, $_[0])) {
warn "$PROGNAME Error: Cannot open file: $_[0]\n";
return;
}
while (<DBASE>) {
chop;
($key, $data) = split(";", $_, 2);
$rdbase{$key} = $data;
}
close(DBASE);
}
$hp = "[^#]*#ref-";
readdb($refdb);
foreach $file (@ARGV) {
unless (-f $file) {
warn "File $file does not exist.\n";
next;
}
$newfile = "$file.new";
unless (open(FILE, $file)) {
warn "$PROGNAME Error: Cannot open $file\n";
next;
}
unless (open(OUTPUT, "> $newfile")) {
warn "$PROGNAME Error: Cannot output to $newfile\n";
next;
}
print STDOUT "Adding class info to $file\n";
$INPUT_RECORD_SEPARATOR="<";
$buf = "";
while ($buf = <FILE>) {
print OUTPUT $buf;
$INPUT_RECORD_SEPARATOR=">";
unless (eof(FILE)) {
$anchor=<FILE>;
# Remove final ">"
chop($anchor);
$INPUT_RECORD_SEPARATOR="<";
if ($anchor =~ /^A\b.*\bhref\s*=\s*(?:"$hp([^\"]*)"|$hp([^\s]*))/is) {
# Optional quotes => two patterns
# Only one can match, so $1$2.
$key = "$1$2";
# Get out the class information.
if ($rdbase{$key} !~ /^(?:[^;]*;){2}(.*)/) {
warn "Warning: $key not in database";
} else {
$dbclass = $1;
# If class information appears, compare db with match
if ($anchor =~ /\bclass\s*=\s*(?:"([^\"]*)"|([^\s]*))/is) {
$currentclass = "$1$2";
if ($currentclass ne $dbclass) {
warn "Warning: Class not same as $key in db.\n";
}
} else {
$anchor .= " class=\"$dbclass\"";
}
}
}
# Put back chopped ">"
print OUTPUT "$anchor>";
}
}
close(FILE);
close(OUTPUT);
unless (rename($newfile,$file)) {
warn "Cannot create $file.\n";
}
}