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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Fri May 25 14:24:31 2007 UTC (16 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.4: +2 -1 lines
++ ChangeLog	25 May 2007 14:24:27 -0000
2007-05-25  Wakaba  <wakaba@suika.fam.cx>

	* parser.cgi: Use CGI::Carp.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24