-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathxextr
More file actions
executable file
·198 lines (169 loc) · 4.92 KB
/
xextr
File metadata and controls
executable file
·198 lines (169 loc) · 4.92 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/local/bin/perl -w
# Extract examples from an HTML document
#
# extract the content of the blocks:
# <div class="example"> ... </div>
# and put their contents into separate files
#
# Arnaud Le Hors - lehors@w3.org
# $Id: xextr,v 1.7 2006-10-09 18:55:53 ihickson Exp $
$PROGNAME = substr($0, rindex($0, "/") + 1);
if (!$ARGV[0]) {
print "Usage: $PROGNAME file [tgt_dir]\n";
exit 1;
}
# copy file in memory
if (!open(INPUT, $ARGV[0])) {
print "$PROGNAME Error: Cannot open file: $ARGV[0]\n";
exit 1;
}
$buf = "";
while (<INPUT>) {
$buf .= $_;
}
close(INPUT);
$path = "";
if ($ARGV[1]) {
$path = "$ARGV[1]";
}
@paths = split(/\//, $ARGV[0]);
$path .= "/" . $paths[$#paths] . "_xampl";
sub process_html_block {
my ($file, $block, $deprecated) = @_;
# comment out undesired block
$block =~ s/<em>(.*?)<\/em>/<!-- $1 -->/sigo;
# map characters
$block =~ s/</</g;
$block =~ s/>/>/g;
$block =~ s/"/\"/g;
$block =~ s/©/(C)/g;
$block =~ s/&/\&/g;
# make sure it does form a complete document
if (!($_ = $block, /<!doctype/i)) {
if (($_ = $block, /frameset/i)) {
$doctype =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Frameset//EN\">";
} elsif ($deprecated || ($_ = $block, /noframe|iframe/i)) {
$doctype =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
} else {
$doctype = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
if (!($_ = $block, /<head>/i) &&
(substr($block, 0, 1) ne "<" ||
substr($block, 0, 2) eq "<!" ||
substr($block, 0, 2) eq "<?")) {
$block = "<p>\n$block";
}
}
$block = "$doctype\n$block";
}
if (!($_ = $block, /<title>/i)) {
if (!($_ = $block, /<head>/i)) {
$block =~ s/<!doctype.*?>/$&\n<title>example<\/title>/i;
} else {
$block =~ s/<head>/<head>\n<title>example<\/title>/i;
}
}
# print file out
print "\tHTML extracting $file\n";
open(OUTPUT, "> $file") || die "failed to create $file\n";
print OUTPUT $block;
close(OUTPUT);
}
sub process_xml_block {
my ($file, $block, $deprecated) = @_;
# comment out undesired block
$block =~ s/<em>(.*?)<\/em>/<!-- $1 -->/sigo;
# map characters
$block =~ s/</</g;
$block =~ s/>/>/g;
$block =~ s/"/\"/g;
$block =~ s/©/(C)/g;
$block =~ s/&/\&/g;
# print file out
print "\tXML extracting $file\n";
open(OUTPUT, "> $file") || die "failed to create $file\n";
print OUTPUT $block;
close(OUTPUT);
}
sub process_css_block {
my ($file, $block, $deprecated) = @_;
# comment out undesired block
$block =~ s|<em>(.*?)</em>|/* $1 */|sigo;
# map characters
$block =~ s/</</g;
$block =~ s/>/>/g;
$block =~ s/"/\"/g;
$block =~ s/©/(C)/g;
$block =~ s/&/\&/g;
# print file out
print "\tCSS extracting $file\n";
open(OUTPUT, "> $file") || die "failed to create $file\n";
print OUTPUT $block;
close(OUTPUT);
}
# Extract different types of blocks: css, html, xml.
# If the type is "html" or "xml" at the DIV level,
# assume that *all* pre elements within are "html" or
# "xml" respectively. If the type is unknown,
# assume css, but allow internal pre elements to
# override this.
sub get_lang {
$pattern =$_[0];
if ( $pattern =~ /html/io ){
return "html";
} elsif ( $pattern =~ /xml/io ){
return "xml";
} else {
return "css";
}
}
sub process_pre_block {
($preclass, $preblock) = @_;
$lang = get_lang($preclass);
$output = "$path$num.$subnum.$lang";
$deprecated = ($preclass =~ /deprecated/io) ? 1 : 0;
if ($lang eq "html") {
process_html_block("$output", $preblock, $deprecated);
} elsif ($lang eq "xml") {
process_xml_block("$output", $preblock, $deprecated);
} elsif ($lang eq "css") {
process_css_block("$output", $preblock, $deprecated);
} else {
warn "Unknown language $lang";
}
}
$preblockst = "(?:<pre>|(?:<pre\\s+class\\s*=\\s*\"?(example|deprecated-example|html-example|deprecated-html-example|xml-example|deprecated-xml-example)\"?\\s*>))\n?";
$preblocket = "\n?<\/pre>";
# Arguments:
# 0: DIV class value
# 1: DIV content
sub process_pre_blocks {
($divclass, $divcontent) = @_;
while ($divcontent =~ /$preblockst(.*?)$preblocket/iso) {
$subnum++;
process_pre_block(($1 || $divclass), $2);
$divcontent = $'
}
}
$divblockst = "<(div|pre)\\s+class\\s*=\\s*\"?(example|deprecated-example|html-example|deprecated-html-example|xml-example|deprecated-xml-example)\"?\\s*>\n?";
$divblocket = "\n?<\/\\1>";
$_ = $buf;
$num = 0;
$subnum = 0;
while (/$divblockst(.*?)$divblocket/iso) {
$continue = $';
$element = $1;
$class = $2;
$block = $3;
$num++;
$subnum = 0;
# If DIV, process internal PRE blocks,
# otherwise, process PRE block directly.
if ($element =~ /div/io) {
process_pre_blocks($class, $block);
} else {
process_pre_block($class, $block);
}
$_ = $continue;
}