-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathhtmlcat
More file actions
90 lines (80 loc) · 2.19 KB
/
htmlcat
File metadata and controls
90 lines (80 loc) · 2.19 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
#!/usr/local/bin/perl
#
# Arnaud Le Hors - lehors@w3.org
# $Id: htmlcat,v 1.4 2006-10-09 18:55:52 ihickson Exp $
use lib 'bin';
use utils;
$PROGNAME = substr($0, rindex($0, "/") + 1);
if (!$ARGV[0]) {
print "Usage: $PROGNAME output file1 file2 ...\n";
exit 1;
}
open(OUTPUT, "> $ARGV[0]");
shift;
# copy file in memory
sub readfile {
$buf = "";
if (!open(INPUT, $_[0])) {
print STDERR "$PROGNAME Error: Cannot open file: $_[0]\n";
return;
}
while (<INPUT>) {
$buf .= $_;
}
close(INPUT);
}
# Convert a file name to the first anchor in that file (#q7.0)
sub guess_anchor {
my ($file, $fragment) = @_;
my $chapno = $lookup{$file};
if (!defined $chapno) {return $file . $fragment;} # Not a chapter
elsif (defined $fragment) {return $fragment;} # Fragment of a chapter
else {return "#q" . $chapno . ".0";} # Guess anchor of first H1 in that file
}
# Read config file
read_config('Project.cfg');
# 1st file:
readfile($ARGV[0]);
$_ = $buf;
# Remove navbar
s/<div\b[^>]*\bclass=["']?navbar\b[^>]*>.*?<\/div>//sigo;
s/<hr\b[^>]*\bclass=["']?navbar\b[^>]*>//sigo;
# Fix local links: "foo.html" -> "#q7.0" and "foo.html#bar" -> "#bar"
s/(href=")([a-z0-9]+\.html)(#[a-z0-9.-]+)?"/$1.guess_anchor($2, $3).'"'/gie;
# take everything up to the body end tag or the end otherwise
if (/<\/body>/sio) {
print OUTPUT $`;
} else {
print OUTPUT $buf;
}
shift;
# then for each other given html file only take the body content
foreach $file (@ARGV) {
readfile($file);
$_ = $buf;
# Remove navbar
s/<div\b[^>]*\bclass=["']?navbar\b[^>]*>.*?<\/div>//sigo;
s/<hr\b[^>]*\bclass=["']?navbar\b[^>]*>//sigo;
# Fix local links: "foo.html#bar" -> "#bar"
s/(href=")([a-z0-9]+\.html)(#[a-z0-9.-]+)?"/$1.guess_anchor($2, $3).'"'/gie;
# Remove warning
s/<!-- warning -->.*?<!-- \/warning -->//gs;
# look for the beginning of the body content
if (/<body>(.*)/sio) {
$buf = $1;
} elsif (/<\/head>/sio) {
$buf = $';
}
# look for the end of the body content
$_ = $buf;
if (/<\/body>/sio) {
print OUTPUT $`;
} elsif (/<\/html>/sio) {
print OUTPUT $`;
} else {
print OUTPUT $buf;
}
}
# close body
print OUTPUT "</body>\n";
close(OUTPUT);