/[pub]/test/html-whatpm/parser-manakai.cgi
Suika

Contents of /test/html-whatpm/parser-manakai.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download)
Sun Jul 15 06:14:30 2007 UTC (16 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +8 -5 lines
++ ChangeLog	15 Jul 2007 06:14:19 -0000
2007-07-15  Wakaba  <wakaba@suika.fam.cx>

	* parser-manakai.cgi: Set |Document| as HTML if it is.
	Use |inner_html| attribute for inner HTML value.
	XML documents are now serializable via |inner_html|.

1 #!/usr/bin/perl
2 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 my @mode = split m#/#, scalar $http->meta_variable ('PATH_INFO'), -1;
17 shift @mode if @mode and $mode[0] == '';
18 ## 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 my $dom = Message::DOM::DOMImplementation->new;
32 # $| = 1;
33 my $doc;
34 my $el;
35
36 if (@mode == 3 and $mode[0] eq 'html' and
37 ($mode[2] eq 'html' or $mode[2] eq 'test')) {
38 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 $doc = $dom->create_document;
57 $doc->manakai_is_html (1);
58 $time1 = time;
59 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 $time2 = time;
67 $time{parse} = $time2 - $time1;
68
69 print "#document\n";
70
71 my $out;
72 if ($mode[2] eq 'html') {
73 $time1 = time;
74 $out = \( ($el or $doc)->inner_html );
75 $time2 = time;
76 $time{serialize_html} = $time2 - $time1;
77 } else { # test
78 $time1 = time;
79 $out = test_serialize ($el || $doc);
80 $time2 = time;
81 $time{serialize_test} = $time2 - $time1;
82 }
83 print STDOUT Encode::encode ('utf-8', $$out);
84 print STDOUT "\n";
85 } elsif (@mode == 3 and $mode[0] eq 'xhtml' and
86 ($mode[2] eq 'html' or $mode[2] eq 'test')) {
87 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 print STDOUT $err->location->column_number, ",";
96 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 if ($mode[2] eq 'html') {
111 $time1 = time;
112 $out = \( $doc->inner_html ); ## TODO: $el case
113 $time2 = time;
114 $time{serialize_xml} = $time2 - $time1;
115 } 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 my $onerror = sub {
131 my %opt = @_;
132 print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n";
133 };
134 print STDOUT "#domerrors\n";
135 $time1 = time;
136 if ($el) {
137 Whatpm::ContentChecker->check_element ($el, $onerror);
138 } else {
139 Whatpm::ContentChecker->check_document ($doc, $onerror);
140 }
141 $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 serialize_html => 'dom5->html5(char)',
154 serialize_xml => 'dom5->xml1(char)',
155 serialize_test => 'dom5->test(char)',
156 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 ## $Date: 2007/06/27 11:08:03 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24