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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations) (download)
Mon May 28 14:04:57 2007 UTC (17 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.6: +7 -1 lines
++ ChangeLog	28 May 2007 14:04:52 -0000
2007-05-28  Wakaba  <wakaba@suika.fam.cx>

	* parser.cgi: Log elapsed time to external files.

	* time.plt: New file.

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 wakaba 1.7 my $char_length = length $s;
34 wakaba 1.6
35 wakaba 1.1 print STDOUT "Content-Type: text/plain; charset=utf-8\n\n";
36    
37 wakaba 1.2 print STDOUT "#errors\n";
38    
39     my $onerror = sub {
40 wakaba 1.4 my (%opt) = @_;
41     print STDOUT "$opt{line},$opt{column},$opt{type}\n";
42 wakaba 1.2 };
43 wakaba 1.1
44 wakaba 1.6 $time1 = time;
45 wakaba 1.3 my $doc = Whatpm::HTML->parse_string
46     ($s => Whatpm::NanoDOM::Document->new, $onerror);
47 wakaba 1.6 $time2 = time;
48     $time{parse} = $time2 - $time1;
49 wakaba 1.1
50 wakaba 1.2 print "#document\n";
51    
52     my $out;
53 wakaba 1.6 $time1 = time;
54 wakaba 1.2 if ($mode eq '/html') {
55 wakaba 1.3 $out = Whatpm::HTML->get_inner_html ($doc);
56 wakaba 1.2 } else { # test
57     $out = test_serialize ($doc);
58 wakaba 1.1 }
59 wakaba 1.6 $time2 = time;
60     $time{serialize} = $time2 - $time1;
61 wakaba 1.2 print STDOUT Encode::encode ('utf-8', $$out);
62 wakaba 1.4 print STDOUT "\n";
63    
64     if ($http->parameter ('dom5')) {
65     require Whatpm::ContentChecker;
66     print STDOUT "#domerrors\n";
67 wakaba 1.6 $time1 = time;
68     Whatpm::ContentChecker->check_document ($doc, sub {
69 wakaba 1.4 my %opt = @_;
70     print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n";
71     });
72 wakaba 1.6 $time2 = time;
73     $time{check} = $time2 - $time1;
74 wakaba 1.4 }
75 wakaba 1.6
76     print STDOUT "#log\n";
77     print STDOUT "byte->char\t", $time{decode}, "s\n";
78     print STDOUT "html5->dom5\t", $time{parse}, "s\n";
79     print STDOUT "dom5->serialize\t", $time{serialize}, "s\n";
80     print STDOUT "dom5 check\t", $time{check}, "s\n" if defined $time{check};
81 wakaba 1.7 for (qw/decode parse serialize check/) {
82     next unless defined $time{$_};
83     open my $file, '>>', ".$_.txt" or die ".$_.txt: $!";
84     print $file $char_length, "\t", $time{$_}, "\n";
85     }
86 wakaba 1.1 } else {
87     print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n404";
88     }
89 wakaba 1.2
90     exit;
91    
92     sub test_serialize ($) {
93     my $node = shift;
94     my $r = '';
95    
96     my @node = map { [$_, ''] } @{$node->child_nodes};
97     while (@node) {
98     my $child = shift @node;
99     my $nt = $child->[0]->node_type;
100     if ($nt == $child->[0]->ELEMENT_NODE) {
101     $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
102    
103     for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
104     @{$child->[0]->attributes}) {
105     $r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case?
106     $r .= $attr->[1] . '"' . "\x0A";
107     }
108    
109     unshift @node,
110     map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes};
111     } elsif ($nt == $child->[0]->TEXT_NODE) {
112     $r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A";
113     } elsif ($nt == $child->[0]->COMMENT_NODE) {
114     $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
115     } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
116     $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";
117     } else {
118     $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
119     }
120     }
121    
122     return \$r;
123     } # test_serialize
124 wakaba 1.4
125     sub get_node_path ($) {
126     my $node = shift;
127     my @r;
128     while (defined $node) {
129     my $rs;
130     if ($node->node_type == 1) {
131     $rs = $node->manakai_local_name;
132     $node = $node->parent_node;
133     } elsif ($node->node_type == 2) {
134     $rs = '@' . $node->manakai_local_name;
135     $node = $node->owner_element;
136     } elsif ($node->node_type == 3) {
137     $rs = '"' . $node->data . '"';
138     $node = $node->parent_node;
139     } elsif ($node->node_type == 9) {
140     $rs = '';
141     $node = $node->parent_node;
142     } else {
143     $rs = '#' . $node->node_type;
144     $node = $node->parent_node;
145     }
146     unshift @r, $rs;
147     }
148     return join '/', @r;
149     } # get_node_path
150    
151     =head1 AUTHOR
152    
153     Wakaba <w@suika.fam.cx>.
154    
155     =head1 LICENSE
156    
157     Copyright 2007 Wakaba <w@suika.fam.cx>
158    
159     This library is free software; you can redistribute it
160     and/or modify it under the same terms as Perl itself.
161    
162     =cut
163    
164 wakaba 1.7 ## $Date: 2007/05/27 11:16:02 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24