forked from w3c/csswg-drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddianch
More file actions
93 lines (75 loc) · 2.67 KB
/
addianch
File metadata and controls
93 lines (75 loc) · 2.67 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
#!/usr/local/bin/perl
# Add index anchors to source file _and_ generate index database
# Arnaud Le Hors - lehors@w3.org
# $Id: addianch,v 2.6 2006-10-09 18:55:52 ihickson Exp $
use DB_File;
use Getopt::Std;
use lib 'bin';
use utils;
$PROG = substr($0, rindex($0, "/") + 1);
$USAGE = "Usage: $PROG [-r url-prefix] dbase [file [output]]\n";
# Remove A tags, initial and trailing ws, collapse all ws to single space
sub cleanup {
my $h = $_[0];
$h =~ tr/\n\t/ /s; # newline & tab -> spaces, and collapse
$h =~ s/<\/?a\b.*?>//gio; # remove A tags
$h =~ s/^ +//o; # remove initial space
$h =~ s/ +$//o; # remove trailing space
return $h;
}
# After matching: $1=starttag, $2=class value, $3=content
$pre = '(<span\s+[^>]*?class\s*=\s*[\"\']?';
$post = '[\"\']?[^>]*>)(.*?)</span\s*>';
$defp = $pre.'(index-def|index-inst)'.$post;
### main
getopts('r:') || die $USAGE;
if ($#ARGV >= 0) {$db = $ARGV[0]; shift;} else {die $USAGE;}
if ($#ARGV >= 0) {$file = $ARGV[0]; shift;} else {$file = '-';}
if ($#ARGV >= 0) {$output = $ARGV[0]; shift;} else {$output = '-';}
if ($#ARGV >= 0) {die $USAGE;}
$prefix = defined $opt_r ? $opt_r : $file; # URL prefix
dbmopen(%anchors, $db, 0666) || die "$PROG: cannot open database $db\n";
$buf = readfile($file); # Load file
$buf =~ s/<!--.*?-->//gio; # Remove comments
open(OUTPUT, ">$output") || die "$PROG: cannot create file $output\n";
$seqno = 0;
while ($buf =~ /$defp/sio) {
print OUTPUT $`;
$buf = $';
$stag = $1;
$class = $2;
$content = $3;
$elem = $&;
# Check if there is a title attribute, otherwise use content
if ($stag =~ /title\s*=\s*(?:\"([^\"]*)\"|\'([^\']*)\'|([^\s>]*))/sio) {
$entry = $1.$2.$3; # Only one of the three is non-empty
} else {
$entry = $content; # Assume content is index term
}
# Split entry in case there are several
@entries = split(/\|/o, cleanup($entry));
# Create an anchor
if ($content =~ /<a\s+[^>]*?name\s*=\s*(?:[\"]([^\"]+)[\"]|[\']([^\']+)[\']|[\s]([^\s>]+)[\s>])/sio) {
# Already a name in the content, re-use it
$anchor = "$1$2$3";
print OUTPUT $elem;
} elsif ($elem =~ /<a\b/sio) {
# Already an anchor with no name in the content.
$anchor = "x$seqno"; # Create a unique anchor
print OUTPUT $`.$&." name=\"$anchor\"".$';
} else {
# No A in content, surround elem
$anchor = "x$seqno"; # Create a unique anchor
print OUTPUT "<a name=\"$anchor\">$elem</a>";
}
# Put URL in database: url-prefix class anchor entry
foreach $e (@entries) {
$key = "$prefix\t$seqno";
$h = "$e\t$prefix\t$class\t$anchor";
$anchors{$key} = $h;
$seqno++;
}
}
print OUTPUT $buf; # Print final part of file
close(OUTPUT);
dbmclose(%anchors);