forked from whatwg/webidl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xref.pl
executable file
·67 lines (60 loc) · 1.32 KB
/
xref.pl
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
#!/usr/bin/perl -w
use strict;
# regex hacking of xml ftw
my $opt = shift || '';
my $input = shift || '-';
unless ($opt =~ /^-[dt]/ && $input ne '' && @ARGV == 1) {
die <<EOF
usage:
$0 -d INPUT URL output ID database file for the input XML document
$0 -t INPUT DB translate input XML document using ID database file
EOF
}
my %db;
my %internal;
my $url;
sub fixup {
my $tag = shift;
return $tag unless $tag =~ /\bhref=["']#([^"']*)["']/;
my $frag = $1;
return $tag unless !exists($internal{$frag}) && exists($db{$frag});
$tag =~ s/\bhref=["']#[^"']*["']/href="$url#$frag"/;
if (!($tag =~ s/\bclass=["']([^"']*)["']/class="$1 external"/)) {
$tag =~ s/>$/ class="external">/;
}
return $tag;
}
if ($opt eq '-d') {
my $url = shift;
local $/;
open FH, $input;
my $s = <FH>;
close FH;
print "$url\n";
while ($s =~ s/<[a-z][a-z0-9]*[^>]*id=["']([^"']+)["'][^>]*>//) {
print "$1\n";
}
} elsif ($opt eq '-t') {
my $dbfile = shift;
open FH, $dbfile;
$url = <FH>;
chomp $url;
%db = ();
while (<FH>) {
chomp;
$db{$_} = 1;
}
close FH;
local $/;
open FH, $input;
my $s = <FH>;
my $t = $s;
close FH;
%internal = ();
while ($s =~ s/<[a-z][a-z0-9]*[^>]*id=["']([^"']+)["'][^>]*>//) {
$internal{$1} = 1;
}
$s = $t;
$s =~ s/(<a\b[^>]*>)/fixup($1)/ge;
print $s;
}