-
Notifications
You must be signed in to change notification settings - Fork 709
/
Copy pathaddlongdesc
executable file
·82 lines (72 loc) · 1.86 KB
/
addlongdesc
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
#!/usr/local/bin/perl
# Add d-links from images to long descriptions.
# Two cases:
# Image is part of a figure with a caption (<P class="caption">):
# Insert d-link at beginning of caption.
# Not part of a figure or no caption.
# Add d-link just after image.
#
# Ian Jacobs ij@w3.org
# $Id: addlongdesc,v 1.5 2006-10-09 18:55:52 ihickson Exp $
use English;
use DB_File;
use lib 'bin';
use utils;
$PROG = substr($0, rindex($0, "/") + 1);
$USAGE = "Usage: $PROG input [output]\n";
($file = $ARGV[0]) || die $USAGE;
shift;
($output = $ARGV[0]) || ($output = "-");
shift;
sub image_name {
my $f = `basename $_[0] '.gif'`;
$f = `basename $_[0] '.png'`;
chop $f;
return($f);
}
sub desc_title {
my $f = "$_[0]";
my $title = "";
if (open(DLINK, $f)) {
local $/ = undef;
my $b = <DLINK>;
close(DLINK);
$b =~ /<TITLE>(.*?)<\/TITLE>/iso;
# Probably need to strip title of markup here.
$title = $1;
}
return($title);
}
open(FILE, $file) ||
die "$PROGNAME Error: Cannot open $file\n";
open(OUTPUT, "> $output") ||
die "$PROGNAME Error: Cannot output to $output\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="<";
$dlink = "";
if ($anchor =~ /^IMG.*?src\s*=\s*(?:([^\s>]+)|\"([^\"]+)\").*/is){
my $imagename = image_name("$1$2");
my $descfile = "images/longdesc/$imagename-desc.html";
if (-f "$descfile") {
$title = desc_title($descfile);
if ($title) {
$dlink = "<SPAN class=\"dlink\"> <A name=\"img-$imagename\" href=\"$descfile\" title=\"$title\">[D]</A></SPAN>";
}
}
} else {
# No change to the anchor.
}
# Put back chopped ">"
print OUTPUT "$anchor>$dlink";
}
}
close(FILE);
close(OUTPUT);