1 |
wakaba |
1.3 |
#!/usr/bin/perl |
2 |
wakaba |
1.1 |
use strict; |
3 |
|
|
|
4 |
|
|
use lib qw[/home/httpd/html/www/markup/html/whatpm |
5 |
|
|
/home/wakaba/work/manakai/lib |
6 |
|
|
/home/wakaba/public_html/-temp/wiki/lib]; |
7 |
|
|
use CGI::Carp qw[fatalsToBrowser]; |
8 |
|
|
use Time::HiRes qw/time/; |
9 |
|
|
|
10 |
|
|
use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module |
11 |
|
|
|
12 |
|
|
my $http = SuikaWiki::Input::HTTP->new; |
13 |
|
|
|
14 |
|
|
## TODO: _charset_ |
15 |
|
|
|
16 |
wakaba |
1.2 |
my @mode = split m#/#, scalar $http->meta_variable ('PATH_INFO'), -1; |
17 |
|
|
shift @mode if @mode and $mode[0] == ''; |
18 |
wakaba |
1.1 |
## TODO: decode unreserved characters |
19 |
|
|
|
20 |
|
|
my $s = $http->parameter ('s'); |
21 |
|
|
if (length $s > 1000_000) { |
22 |
|
|
print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long"; |
23 |
|
|
exit; |
24 |
|
|
} |
25 |
|
|
my $char_length = length $s; |
26 |
|
|
my %time; |
27 |
|
|
my $time1; |
28 |
|
|
my $time2; |
29 |
|
|
|
30 |
|
|
require Message::DOM::DOMImplementation; |
31 |
wakaba |
1.4 |
my $dom = Message::DOM::DOMImplementation->new; |
32 |
wakaba |
1.2 |
# $| = 1; |
33 |
wakaba |
1.1 |
my $doc; |
34 |
wakaba |
1.2 |
my $el; |
35 |
wakaba |
1.1 |
|
36 |
wakaba |
1.2 |
if (@mode == 3 and $mode[0] eq 'html' and |
37 |
|
|
($mode[2] eq 'html' or $mode[2] eq 'test')) { |
38 |
wakaba |
1.1 |
print STDOUT "Content-Type: text/plain; charset=utf-8\n\n"; |
39 |
|
|
|
40 |
|
|
require Encode; |
41 |
|
|
require Whatpm::HTML; |
42 |
|
|
|
43 |
|
|
$time1 = time; |
44 |
|
|
$s = Encode::decode ('utf-8', $s); |
45 |
|
|
$time2 = time; |
46 |
|
|
$time{decode} = $time2 - $time1; |
47 |
|
|
|
48 |
|
|
|
49 |
|
|
print STDOUT "#errors\n"; |
50 |
|
|
|
51 |
|
|
my $onerror = sub { |
52 |
|
|
my (%opt) = @_; |
53 |
|
|
print STDOUT "$opt{line},$opt{column},$opt{type}\n"; |
54 |
|
|
}; |
55 |
|
|
|
56 |
wakaba |
1.2 |
$doc = $dom->create_document; |
57 |
wakaba |
1.4 |
$doc->manakai_is_html (1); |
58 |
wakaba |
1.1 |
$time1 = time; |
59 |
wakaba |
1.2 |
if (length $mode[1]) { |
60 |
|
|
$el = $doc->create_element_ns |
61 |
|
|
('http://www.w3.org/1999/xhtml', [undef, $mode[1]]); |
62 |
|
|
Whatpm::HTML->set_inner_html ($el, $s, $onerror); |
63 |
|
|
} else { |
64 |
|
|
Whatpm::HTML->parse_string ($s => $doc, $onerror); |
65 |
|
|
} |
66 |
wakaba |
1.1 |
$time2 = time; |
67 |
|
|
$time{parse} = $time2 - $time1; |
68 |
|
|
|
69 |
|
|
print "#document\n"; |
70 |
|
|
|
71 |
|
|
my $out; |
72 |
wakaba |
1.2 |
if ($mode[2] eq 'html') { |
73 |
|
|
$time1 = time; |
74 |
wakaba |
1.4 |
$out = \( ($el or $doc)->inner_html ); |
75 |
wakaba |
1.2 |
$time2 = time; |
76 |
|
|
$time{serialize_html} = $time2 - $time1; |
77 |
wakaba |
1.1 |
} else { # test |
78 |
wakaba |
1.2 |
$time1 = time; |
79 |
|
|
$out = test_serialize ($el || $doc); |
80 |
|
|
$time2 = time; |
81 |
|
|
$time{serialize_test} = $time2 - $time1; |
82 |
wakaba |
1.1 |
} |
83 |
|
|
print STDOUT Encode::encode ('utf-8', $$out); |
84 |
|
|
print STDOUT "\n"; |
85 |
wakaba |
1.2 |
} elsif (@mode == 3 and $mode[0] eq 'xhtml' and |
86 |
|
|
($mode[2] eq 'html' or $mode[2] eq 'test')) { |
87 |
wakaba |
1.1 |
print STDOUT "Content-Type: text/plain; charset=utf-8\n\n"; |
88 |
|
|
|
89 |
|
|
require Message::DOM::XMLParserTemp; |
90 |
|
|
print STDOUT "#errors\n"; |
91 |
|
|
|
92 |
|
|
my $onerror = sub { |
93 |
|
|
my $err = shift; |
94 |
|
|
print STDOUT $err->location->line_number, ","; |
95 |
wakaba |
1.2 |
print STDOUT $err->location->column_number, ","; |
96 |
wakaba |
1.1 |
print STDOUT $err->text, "\n"; |
97 |
|
|
return 1; |
98 |
|
|
}; |
99 |
|
|
|
100 |
|
|
open my $fh, '<', \$s; |
101 |
|
|
my $time1 = time; |
102 |
|
|
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
103 |
|
|
($fh => $dom, $onerror, charset => 'utf-8'); |
104 |
|
|
my $time2 = time; |
105 |
|
|
$time{parse_xml} = $time2 - $time1; |
106 |
|
|
|
107 |
|
|
print "#document\n"; |
108 |
|
|
|
109 |
|
|
my $out; |
110 |
wakaba |
1.2 |
if ($mode[2] eq 'html') { |
111 |
wakaba |
1.4 |
$time1 = time; |
112 |
|
|
$out = \( $doc->inner_html ); ## TODO: $el case |
113 |
|
|
$time2 = time; |
114 |
|
|
$time{serialize_xml} = $time2 - $time1; |
115 |
wakaba |
1.1 |
} else { # test |
116 |
|
|
$time1 = time; |
117 |
|
|
$out = test_serialize ($doc); |
118 |
|
|
$time2 = time; |
119 |
|
|
$time{serialize_test} = $time2 - $time1; |
120 |
|
|
} |
121 |
|
|
print STDOUT Encode::encode ('utf-8', $$out); |
122 |
|
|
print STDOUT "\n"; |
123 |
|
|
} else { |
124 |
|
|
print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n404"; |
125 |
|
|
exit; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
if ($http->parameter ('dom5')) { |
129 |
|
|
require Whatpm::ContentChecker; |
130 |
wakaba |
1.2 |
my $onerror = sub { |
131 |
|
|
my %opt = @_; |
132 |
|
|
print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n"; |
133 |
|
|
}; |
134 |
wakaba |
1.1 |
print STDOUT "#domerrors\n"; |
135 |
|
|
$time1 = time; |
136 |
wakaba |
1.2 |
if ($el) { |
137 |
|
|
Whatpm::ContentChecker->check_element ($el, $onerror); |
138 |
|
|
} else { |
139 |
|
|
Whatpm::ContentChecker->check_document ($doc, $onerror); |
140 |
|
|
} |
141 |
wakaba |
1.1 |
$time2 = time; |
142 |
|
|
$time{check} = $time2 - $time1; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
print STDOUT "#log\n"; |
146 |
|
|
for (qw/decode parse parse_xml serialize_html serialize_xml serialize_test |
147 |
|
|
check/) { |
148 |
|
|
next unless defined $time{$_}; |
149 |
|
|
print STDOUT { |
150 |
|
|
decode => 'bytes->chars', |
151 |
|
|
parse => 'html5(chars)->dom5', |
152 |
|
|
parse_xml => 'xml1(chars)->dom5', |
153 |
wakaba |
1.2 |
serialize_html => 'dom5->html5(char)', |
154 |
|
|
serialize_xml => 'dom5->xml1(char)', |
155 |
|
|
serialize_test => 'dom5->test(char)', |
156 |
wakaba |
1.1 |
check => 'dom5 check', |
157 |
|
|
}->{$_}; |
158 |
|
|
print STDOUT "\t", $time{$_}, "s\n"; |
159 |
|
|
open my $file, '>>', ".manakai-$_.txt" or die ".manakai-$_.txt: $!"; |
160 |
|
|
print $file $char_length, "\t", $time{$_}, "\n"; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
exit; |
164 |
|
|
|
165 |
|
|
sub test_serialize ($) { |
166 |
|
|
my $node = shift; |
167 |
|
|
my $r = ''; |
168 |
|
|
|
169 |
|
|
my @node = map { [$_, ''] } @{$node->child_nodes}; |
170 |
|
|
while (@node) { |
171 |
|
|
my $child = shift @node; |
172 |
|
|
my $nt = $child->[0]->node_type; |
173 |
|
|
if ($nt == $child->[0]->ELEMENT_NODE) { |
174 |
|
|
$r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
175 |
|
|
|
176 |
|
|
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } |
177 |
|
|
@{$child->[0]->attributes}) { |
178 |
|
|
$r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
179 |
|
|
$r .= $attr->[1] . '"' . "\x0A"; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
unshift @node, |
183 |
|
|
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
184 |
|
|
} elsif ($nt == $child->[0]->TEXT_NODE) { |
185 |
|
|
$r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A"; |
186 |
|
|
} elsif ($nt == $child->[0]->CDATA_SECTION_NODE) { |
187 |
|
|
$r .= '| ' . $child->[1] . '<![CDATA[' . $child->[0]->data . "]]>\x0A"; |
188 |
|
|
} elsif ($nt == $child->[0]->COMMENT_NODE) { |
189 |
|
|
$r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A"; |
190 |
|
|
} elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) { |
191 |
|
|
$r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A"; |
192 |
|
|
} elsif ($nt == $child->[0]->PROCESSING_INSTRUCTION_NODE) { |
193 |
|
|
$r .= '| ' . $child->[1] . '<?' . $child->[0]->target . ' ' . |
194 |
|
|
$child->[0]->data . "?>\x0A"; |
195 |
|
|
} else { |
196 |
|
|
$r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
return \$r; |
201 |
|
|
} # test_serialize |
202 |
|
|
|
203 |
|
|
sub get_node_path ($) { |
204 |
|
|
my $node = shift; |
205 |
|
|
my @r; |
206 |
|
|
while (defined $node) { |
207 |
|
|
my $rs; |
208 |
|
|
if ($node->node_type == 1) { |
209 |
|
|
$rs = $node->manakai_local_name; |
210 |
|
|
$node = $node->parent_node; |
211 |
|
|
} elsif ($node->node_type == 2) { |
212 |
|
|
$rs = '@' . $node->manakai_local_name; |
213 |
|
|
$node = $node->owner_element; |
214 |
|
|
} elsif ($node->node_type == 3) { |
215 |
|
|
$rs = '"' . $node->data . '"'; |
216 |
|
|
$node = $node->parent_node; |
217 |
|
|
} elsif ($node->node_type == 9) { |
218 |
|
|
$rs = ''; |
219 |
|
|
$node = $node->parent_node; |
220 |
|
|
} else { |
221 |
|
|
$rs = '#' . $node->node_type; |
222 |
|
|
$node = $node->parent_node; |
223 |
|
|
} |
224 |
|
|
unshift @r, $rs; |
225 |
|
|
} |
226 |
|
|
return join '/', @r; |
227 |
|
|
} # get_node_path |
228 |
|
|
|
229 |
|
|
=head1 AUTHOR |
230 |
|
|
|
231 |
|
|
Wakaba <w@suika.fam.cx>. |
232 |
|
|
|
233 |
|
|
=head1 LICENSE |
234 |
|
|
|
235 |
|
|
Copyright 2007 Wakaba <w@suika.fam.cx> |
236 |
|
|
|
237 |
|
|
This library is free software; you can redistribute it |
238 |
|
|
and/or modify it under the same terms as Perl itself. |
239 |
|
|
|
240 |
|
|
=cut |
241 |
|
|
|
242 |
wakaba |
1.4 |
## $Date: 2007/06/27 11:08:03 $ |