/[suikacvs]/test/html-whatpm/parser.cgi
Suika

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations) (download)
Sun May 27 11:16:02 2007 UTC (17 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.5: +22 -13 lines
++ ChangeLog	27 May 2007 11:15:58 -0000
	* parser.cgi: Output how long each process takes.
	Document element testing is moved to the |Whatpm::ContentChecker|
	module.

2007-05-27  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3    
4 wakaba 1.2 use lib qw[/home/httpd/html/www/markup/html/whatpm
5     /home/wakaba/public_html/-temp/wiki/lib];
6 wakaba 1.5 use CGI::Carp qw[fatalsToBrowser];
7 wakaba 1.6 use Time::HiRes qw/time/;
8 wakaba 1.1
9     use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module
10    
11     my $http = SuikaWiki::Input::HTTP->new;
12    
13     ## TODO: _charset_
14    
15     my $mode = $http->meta_variable ('PATH_INFO');
16     ## TODO: decode unreserved characters
17    
18 wakaba 1.2 if ($mode eq '/html' or $mode eq '/test') {
19 wakaba 1.1 require Encode;
20 wakaba 1.3 require Whatpm::HTML;
21     require Whatpm::NanoDOM;
22 wakaba 1.1
23     my $s = $http->parameter ('s');
24     if (length $s > 1000_000) {
25     print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long";
26     exit;
27     }
28    
29 wakaba 1.6 my $time1 = time;
30 wakaba 1.1 $s = Encode::decode ('utf-8', $s);
31 wakaba 1.6 my $time2 = time;
32     my %time = (decode => $time2 - $time1);
33    
34 wakaba 1.1 print STDOUT "Content-Type: text/plain; charset=utf-8\n\n";
35    
36 wakaba 1.2 print STDOUT "#errors\n";
37    
38     my $onerror = sub {
39 wakaba 1.4 my (%opt) = @_;
40     print STDOUT "$opt{line},$opt{column},$opt{type}\n";
41 wakaba 1.2 };
42 wakaba 1.1
43 wakaba 1.6 $time1 = time;
44 wakaba 1.3 my $doc = Whatpm::HTML->parse_string
45     ($s => Whatpm::NanoDOM::Document->new, $onerror);
46 wakaba 1.6 $time2 = time;
47     $time{parse} = $time2 - $time1;
48 wakaba 1.1
49 wakaba 1.2 print "#document\n";
50    
51     my $out;
52 wakaba 1.6 $time1 = time;
53 wakaba 1.2 if ($mode eq '/html') {
54 wakaba 1.3 $out = Whatpm::HTML->get_inner_html ($doc);
55 wakaba 1.2 } else { # test
56     $out = test_serialize ($doc);
57 wakaba 1.1 }
58 wakaba 1.6 $time2 = time;
59     $time{serialize} = $time2 - $time1;
60 wakaba 1.2 print STDOUT Encode::encode ('utf-8', $$out);
61 wakaba 1.4 print STDOUT "\n";
62    
63     if ($http->parameter ('dom5')) {
64     require Whatpm::ContentChecker;
65     print STDOUT "#domerrors\n";
66 wakaba 1.6 $time1 = time;
67     Whatpm::ContentChecker->check_document ($doc, sub {
68 wakaba 1.4 my %opt = @_;
69     print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n";
70     });
71 wakaba 1.6 $time2 = time;
72     $time{check} = $time2 - $time1;
73 wakaba 1.4 }
74 wakaba 1.6
75     print STDOUT "#log\n";
76     print STDOUT "byte->char\t", $time{decode}, "s\n";
77     print STDOUT "html5->dom5\t", $time{parse}, "s\n";
78     print STDOUT "dom5->serialize\t", $time{serialize}, "s\n";
79     print STDOUT "dom5 check\t", $time{check}, "s\n" if defined $time{check};
80 wakaba 1.1 } else {
81     print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n404";
82     }
83 wakaba 1.2
84     exit;
85    
86     sub test_serialize ($) {
87     my $node = shift;
88     my $r = '';
89    
90     my @node = map { [$_, ''] } @{$node->child_nodes};
91     while (@node) {
92     my $child = shift @node;
93     my $nt = $child->[0]->node_type;
94     if ($nt == $child->[0]->ELEMENT_NODE) {
95     $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
96    
97     for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
98     @{$child->[0]->attributes}) {
99     $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
100     $r .= $attr->[1] . '"' . "\x0A";
101     }
102    
103     unshift @node,
104     map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
105     } elsif ($nt == $child->[0]->TEXT_NODE) {
106     $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
107     } elsif ($nt == $child->[0]->COMMENT_NODE) {
108     $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
109     } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
110     $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";
111     } else {
112     $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
113     }
114     }
115    
116     return \$r;
117     } # test_serialize
118 wakaba 1.4
119     sub get_node_path ($) {
120     my $node = shift;
121     my @r;
122     while (defined $node) {
123     my $rs;
124     if ($node->node_type == 1) {
125     $rs = $node->manakai_local_name;
126     $node = $node->parent_node;
127     } elsif ($node->node_type == 2) {
128     $rs = '@' . $node->manakai_local_name;
129     $node = $node->owner_element;
130     } elsif ($node->node_type == 3) {
131     $rs = '"' . $node->data . '"';
132     $node = $node->parent_node;
133     } elsif ($node->node_type == 9) {
134     $rs = '';
135     $node = $node->parent_node;
136     } else {
137     $rs = '#' . $node->node_type;
138     $node = $node->parent_node;
139     }
140     unshift @r, $rs;
141     }
142     return join '/', @r;
143     } # get_node_path
144    
145     =head1 AUTHOR
146    
147     Wakaba <w@suika.fam.cx>.
148    
149     =head1 LICENSE
150    
151     Copyright 2007 Wakaba <w@suika.fam.cx>
152    
153     This library is free software; you can redistribute it
154     and/or modify it under the same terms as Perl itself.
155    
156     =cut
157    
158 wakaba 1.6 ## $Date: 2007/05/25 14:24:31 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24