forked from w3c/csswg-drafts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlcat
85 lines (69 loc) · 1.82 KB
/
htmlcat
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
#!/usr/local/bin/perl
#
# Arnaud Le Hors - lehors@w3.org
# $Id: htmlcat,v 1.4 2006-10-09 18:55:52 ihickson Exp $
use strict;
use warnings;
my $PROGNAME = substr($0, rindex($0, "/") + 1);
if (!$ARGV[0]) {
print "Usage: $PROGNAME output file1 file2 ...\n";
exit 1;
}
open(OUTPUT, "> $ARGV[0]");
shift;
# Fix up links & anchors
sub fixup($$)
{
my ($name, $s) = @_;
# Make local links unique
$s =~ s/(\bhref\s*=\s*[\"\']?)#([^\"\'> ]+)([\"\']?)/$1$name.$2$3/ig;
$s =~ s/(\b(?:id|name)\s*=\s*[\"\']?)([^\"\'> ]+)([\"\']?)/$1$name.$2$3/ig;
# Give the first H1 an ID equal to its source file name
$s =~ s/<H1>/<H1 id=\"$name\">/i;
# Try to make links to other chapters into links to the file itself
$s =~ s/(\bhref\s*=\s*[\"\'])([^#\/:\"\']+)([\"\'])/$1\#$2$3/ig;
$s =~ s/(\bhref\s*=\s*[\"\']?)([^\/:\"\'> ]+)#([^\"\'> ]+)([\"\']?)/$1\#$2.$3$4/ig;
return $s;
}
# copy file in memory
sub readfile {
my $buf = "";
if (!open(INPUT, $_[0])) {
print STDERR "$PROGNAME Error: Cannot open file: $_[0]\n";
return;
}
while (<INPUT>) {
$buf .= $_;
}
close(INPUT);
return $buf;
}
# 1st file: take everything up to the body end tag or the end otherwise
$_ = readfile($ARGV[0]);
if (/<\/body>/sio) {
print OUTPUT fixup($ARGV[0], $`);
} else {
print OUTPUT fixup($ARGV[0], $_);
}
shift;
# then for each other given html file only take the body content
foreach my $file (@ARGV) {
$_ = readfile($file);
# look for the beginning of the body content
if (/<body>(.*)/sio) {
$_ = $1;
} elsif (/<\/head>/sio) {
$_ = $';
}
# look for the end of the body content
if (/<\/body>/sio) {
print OUTPUT fixup($file, $`);
} elsif (/<\/html>/sio) {
print OUTPUT fixup($file, $`);
} else {
print OUTPUT fixup($file, $_);
}
}
# close body
print OUTPUT "</body>\n";
close(OUTPUT);