/[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.7 - (hide annotations) (download)
Sat Apr 12 15:57:56 2008 UTC (16 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.6: +26 -4 lines
++ ChangeLog	12 Apr 2008 15:57:44 -0000
2008-04-12  Wakaba  <wakaba@suika.fam.cx>

	* parser-manakai.cgi, parser-manakai-interface.en.html: The |innerHTML|
	output mode is split into "|innerHTML| (HTML)" and "|innerHTML| (XML)"
	output modes.

2008-03-29  Wakaba  <wakaba@suika.fam.cx>

	* cc.cgi: New "URI" section is implemented.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24