/[suikacvs]/test/html-webhacc/cc.cgi
Suika

Contents of /test/html-webhacc/cc.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations) (download)
Sun Jul 1 10:02:24 2007 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.7: +13 -1 lines
++ ChangeLog	1 Jul 2007 10:02:07 -0000
	* cc.cgi: Return 404 if |PATH_INFO| is different from |/|.
	Link to |error-description|.

	* error-description-source.xml: New.

	* mkdescription.pl: New.

2007-07-01  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.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 wakaba 1.2 use Scalar::Util qw[refaddr];
9 wakaba 1.1
10     use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module
11    
12 wakaba 1.2 sub htescape ($) {
13     my $s = $_[0];
14     $s =~ s/&/&amp;/g;
15     $s =~ s/</&lt;/g;
16     $s =~ s/>/&gt;/g;
17     $s =~ s/"/&quot;/g;
18     $s =~ s!([\x00-\x09\x0B-\x1F\x7F-\x80])!sprintf '<var>U+%04X</var>', ord $1!ge;
19     return $s;
20     } # htescape
21    
22 wakaba 1.1 my $http = SuikaWiki::Input::HTTP->new;
23    
24     ## TODO: _charset_
25    
26 wakaba 1.8 if ($http->meta_variable ('PATH_INFO') ne '/') {
27     print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400";
28     exit;
29     }
30    
31 wakaba 1.2 my $input_format = $http->parameter ('i') || 'text/html';
32     my $inner_html_element = $http->parameter ('e');
33     my $input_uri = 'thismessage:/';
34 wakaba 1.1
35     my $s = $http->parameter ('s');
36     if (length $s > 1000_000) {
37     print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long";
38     exit;
39     }
40 wakaba 1.2
41 wakaba 1.7 load_text_catalog ('en'); ## TODO: conneg
42    
43 wakaba 1.3 my @nav;
44 wakaba 1.2 print STDOUT qq[Content-Type: text/html; charset=utf-8
45    
46     <!DOCTYPE html>
47     <html lang="en">
48     <head>
49     <title>Web Document Conformance Checker (BETA)</title>
50 wakaba 1.3 <link rel="stylesheet" href="../cc-style.css" type="text/css">
51 wakaba 1.2 </head>
52     <body>
53     <h1>Web Document Conformance Checker (<em>beta</em>)</h1>
54    
55 wakaba 1.4 <div id="document-info" class="section">
56 wakaba 1.2 <dl>
57     <dt>Document URI</dt>
58     <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input_uri]}">@{[htescape $input_uri]}</a>&gt;</code></dd>
59     <dt>Internet Media Type</dt>
60     <dd><code class="MIME" lang="en">@{[htescape $input_format]}</code></dd>
61     ]; # no </dl> yet
62 wakaba 1.3 push @nav, ['#document-info' => 'Information'];
63 wakaba 1.1
64     require Message::DOM::DOMImplementation;
65     my $dom = Message::DOM::DOMImplementation->____new;
66     my $doc;
67     my $el;
68    
69 wakaba 1.2 if ($input_format eq 'text/html') {
70     require Encode;
71     require Whatpm::HTML;
72    
73     $s = Encode::decode ('utf-8', $s);
74    
75     print STDOUT qq[
76     <dt>Character Encoding</dt>
77     <dd>(none)</dd>
78     </dl>
79 wakaba 1.3 </div>
80 wakaba 1.2
81     <div id="source-string" class="section">
82 wakaba 1.3 <h2>Document Source</h2>
83 wakaba 1.2 ];
84 wakaba 1.3 push @nav, ['#source-string' => 'Source'];
85 wakaba 1.2 print_source_string (\$s);
86     print STDOUT qq[
87     </div>
88 wakaba 1.1
89 wakaba 1.2 <div id="parse-errors" class="section">
90     <h2>Parse Errors</h2>
91    
92 wakaba 1.4 <dl>
93 wakaba 1.2 ];
94 wakaba 1.3 push @nav, ['#parse-errors' => 'Parse Error'];
95 wakaba 1.1
96     my $onerror = sub {
97     my (%opt) = @_;
98 wakaba 1.7 my ($cls, $msg) = get_text ($opt{type}, $opt{level});
99 wakaba 1.2 if ($opt{column} > 0) {
100 wakaba 1.7 print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n];
101 wakaba 1.2 } else {
102 wakaba 1.7 $opt{line} = $opt{line} - 1 || 1;
103     print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n];
104 wakaba 1.2 }
105 wakaba 1.8 $opt{type} =~ tr/ /-/;
106     $opt{type} =~ s/\|/%7C/g;
107     $msg .= qq[ [<a href="../error-description#$opt{type}">Description</a>]];
108 wakaba 1.7 print STDOUT qq[<dd class="$cls">$msg</dd>\n];
109 wakaba 1.1 };
110    
111     $doc = $dom->create_document;
112 wakaba 1.2 if (defined $inner_html_element and length $inner_html_element) {
113 wakaba 1.1 $el = $doc->create_element_ns
114 wakaba 1.2 ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]);
115 wakaba 1.1 Whatpm::HTML->set_inner_html ($el, $s, $onerror);
116     } else {
117     Whatpm::HTML->parse_string ($s => $doc, $onerror);
118     }
119    
120 wakaba 1.2 print STDOUT qq[
121 wakaba 1.4 </dl>
122 wakaba 1.2 </div>
123     ];
124     } elsif ($input_format eq 'application/xhtml+xml') {
125     require Message::DOM::XMLParserTemp;
126     require Encode;
127    
128     my $t = Encode::decode ('utf-8', $s);
129    
130     print STDOUT qq[
131     <dt>Character Encoding</dt>
132     <dd>(none)</dd>
133     </dl>
134 wakaba 1.3 </div>
135 wakaba 1.2
136     <div id="source-string" class="section">
137 wakaba 1.3 <h2>Document Source</h2>
138 wakaba 1.2 ];
139 wakaba 1.3 push @nav, ['#source-string' => 'Source'];
140 wakaba 1.2 print_source_string (\$t);
141     print STDOUT qq[
142     </div>
143 wakaba 1.1
144 wakaba 1.2 <div id="parse-errors" class="section">
145     <h2>Parse Errors</h2>
146 wakaba 1.1
147 wakaba 1.7 <dl>];
148 wakaba 1.3 push @nav, ['#parse-errors' => 'Parse Error'];
149 wakaba 1.1
150     my $onerror = sub {
151     my $err = shift;
152 wakaba 1.2 my $line = $err->location->line_number;
153 wakaba 1.4 print STDOUT qq[<dt><a href="#line-$line">Line $line</a> column ];
154     print STDOUT $err->location->column_number, "</dt><dd>";
155     print STDOUT htescape $err->text, "</dd>\n";
156 wakaba 1.1 return 1;
157     };
158    
159     open my $fh, '<', \$s;
160     $doc = Message::DOM::XMLParserTemp->parse_byte_stream
161     ($fh => $dom, $onerror, charset => 'utf-8');
162    
163 wakaba 1.7 print STDOUT qq[</dl>
164 wakaba 1.2 </div>
165     ];
166     } else {
167     print STDOUT qq[
168     </dl>
169 wakaba 1.4 </div>
170 wakaba 1.2
171 wakaba 1.3 <div id="result-summary" class="section">
172 wakaba 1.2 <p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p>
173 wakaba 1.3 </div>
174 wakaba 1.2 ];
175 wakaba 1.3 push @nav, ['#result-summary' => 'Result'];
176 wakaba 1.2 }
177    
178    
179     if (defined $doc or defined $el) {
180     print STDOUT qq[
181     <div id="document-tree" class="section">
182     <h2>Document Tree</h2>
183     ];
184 wakaba 1.3 push @nav, ['#document-tree' => 'Tree'];
185 wakaba 1.2
186     print_document_tree ($el || $doc);
187    
188     print STDOUT qq[
189     </div>
190 wakaba 1.1
191 wakaba 1.2 <div id="document-errors" class="section">
192     <h2>Document Errors</h2>
193    
194 wakaba 1.7 <dl>];
195 wakaba 1.3 push @nav, ['#document-errors' => 'Document Error'];
196 wakaba 1.1
197     require Whatpm::ContentChecker;
198     my $onerror = sub {
199     my %opt = @_;
200 wakaba 1.7 my ($cls, $msg) = get_text ($opt{type}, $opt{level});
201 wakaba 1.8 $opt{type} = $opt{level} . ':' . $opt{type} if defined $opt{level};
202     $opt{type} =~ tr/ /-/;
203     $opt{type} =~ s/\|/%7C/g;
204     $msg .= qq[ [<a href="../error-description#$opt{type}">Description</a>]];
205 wakaba 1.7 print STDOUT qq[<dt class="$cls">] . get_node_link ($opt{node}) .
206     qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n";
207 wakaba 1.1 };
208 wakaba 1.2
209 wakaba 1.5 my $elements;
210 wakaba 1.1 if ($el) {
211 wakaba 1.5 $elements = Whatpm::ContentChecker->check_element ($el, $onerror);
212 wakaba 1.1 } else {
213 wakaba 1.5 $elements = Whatpm::ContentChecker->check_document ($doc, $onerror);
214 wakaba 1.1 }
215 wakaba 1.2
216 wakaba 1.7 print STDOUT qq[</dl>
217 wakaba 1.2 </div>
218     ];
219 wakaba 1.5
220     if (@{$elements->{table}}) {
221     require JSON;
222    
223     print STDOUT qq[
224     <div id="tables" class="section">
225     <h2>Tables</h2>
226    
227     <!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->
228     <script src="../table-script.js" type="text/javascript"></script>
229     <noscript>
230     <p><em>Structure of tables are visualized here if scripting is enabled.</em></p>
231     </noscript>
232     ];
233    
234     my $i = 0;
235     for my $table_el (@{$elements->{table}}) {
236     $i++;
237 wakaba 1.6 print STDOUT qq[<div class="section" id="table-$i"><h3>] .
238     get_node_link ($table_el) . q[</h3>];
239 wakaba 1.5
240     my $table = Whatpm::HTMLTable->form_table ($table_el);
241    
242     for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) {
243     next unless $_;
244     delete $_->{element};
245     }
246    
247     for (@{$table->{row_group}}) {
248     next unless $_;
249     next unless $_->{element};
250     $_->{type} = $_->{element}->manakai_local_name;
251     delete $_->{element};
252     }
253    
254     for (@{$table->{cell}}) {
255     next unless $_;
256     for (@{$_}) {
257     next unless $_;
258     for (@$_) {
259     $_->{id} = refaddr $_->{element} if defined $_->{element};
260     delete $_->{element};
261     }
262     }
263     }
264    
265     print STDOUT '</div><script type="text/javascript">tableToCanvas (';
266     print STDOUT JSON::objToJson ($table);
267     print STDOUT qq[, document.getElementById ('table-$i'));</script>];
268     }
269    
270     print STDOUT qq[</div>];
271     }
272 wakaba 1.6
273     if (keys %{$elements->{term}}) {
274     print STDOUT qq[
275     <div id="terms" class="section">
276     <h2>Terms</h2>
277    
278     <dl>
279     ];
280     for my $term (sort {$a cmp $b} keys %{$elements->{term}}) {
281     print STDOUT qq[<dt>@{[htescape $term]}</dt>];
282     for (@{$elements->{term}->{$term}}) {
283     print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>];
284     }
285     }
286     print STDOUT qq[</dl></div>];
287     }
288 wakaba 1.1 }
289    
290 wakaba 1.2 ## TODO: Show result
291 wakaba 1.3
292 wakaba 1.2 print STDOUT qq[
293 wakaba 1.3 <ul class="navigation" id="nav-items">
294     ];
295     for (@nav) {
296     print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>];
297     }
298     print STDOUT qq[
299     </ul>
300 wakaba 1.2 </body>
301     </html>
302     ];
303 wakaba 1.1
304     exit;
305    
306 wakaba 1.2 sub print_source_string ($) {
307     my $s = $_[0];
308     my $i = 1;
309     print STDOUT qq[<ol lang="">\n];
310 wakaba 1.7 if (length $$s) {
311     while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) {
312     print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n";
313     $i++;
314     }
315     if ($$s =~ /\G([^\x0A]+)/gc) {
316     print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n";
317     }
318     } else {
319     print STDOUT q[<li id="line-1"></li>];
320 wakaba 1.2 }
321     print STDOUT "</ol>";
322     } # print_input_string
323    
324     sub print_document_tree ($) {
325 wakaba 1.1 my $node = shift;
326 wakaba 1.2 my $r = '<ol class="xoxo">';
327 wakaba 1.1
328 wakaba 1.2 my @node = ($node);
329 wakaba 1.1 while (@node) {
330     my $child = shift @node;
331 wakaba 1.2 unless (ref $child) {
332     $r .= $child;
333     next;
334     }
335    
336     my $node_id = 'node-'.refaddr $child;
337     my $nt = $child->node_type;
338     if ($nt == $child->ELEMENT_NODE) {
339 wakaba 1.4 my $child_nsuri = $child->namespace_uri;
340     $r .= qq[<li id="$node_id" class="tree-element"><code title="@{[defined $child_nsuri ? $child_nsuri : '']}">] . htescape ($child->tag_name) .
341 wakaba 1.2 '</code>'; ## ISSUE: case
342    
343     if ($child->has_attributes) {
344     $r .= '<ul class="attributes">';
345 wakaba 1.4 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] }
346 wakaba 1.2 @{$child->attributes}) {
347 wakaba 1.4 $r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case?
348 wakaba 1.2 $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children
349     }
350     $r .= '</ul>';
351     }
352    
353 wakaba 1.7 if ($child->has_child_nodes) {
354 wakaba 1.2 $r .= '<ol class="children">';
355 wakaba 1.6 unshift @node, @{$child->child_nodes}, '</ol></li>';
356     } else {
357     $r .= '</li>';
358 wakaba 1.2 }
359     } elsif ($nt == $child->TEXT_NODE) {
360 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>';
361 wakaba 1.2 } elsif ($nt == $child->CDATA_SECTION_NODE) {
362 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-cdata"><code>&lt;[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]&gt;</code></li>';
363 wakaba 1.2 } elsif ($nt == $child->COMMENT_NODE) {
364 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-comment"><code>&lt;!--</code><q lang="">' . htescape ($child->data) . '</q><code>--&gt;</code></li>';
365 wakaba 1.2 } elsif ($nt == $child->DOCUMENT_NODE) {
366 wakaba 1.6 $r .= qq'<li id="$node_id" class="tree-document">Document';
367 wakaba 1.7 $r .= qq[<ul class="attributes">];
368     $r .= qq[<li>@{[scalar get_text ('manakaiIsHTML:'.($child->manakai_is_html?1:0))]}</li>];
369     $r .= qq[<li>@{[scalar get_text ('manakaiCompatMode:'.$child->manakai_compat_mode)]}</li>];
370     $r .= qq[</ul>];
371 wakaba 1.2 if ($child->has_child_nodes) {
372 wakaba 1.7 $r .= '<ol class="children">';
373 wakaba 1.6 unshift @node, @{$child->child_nodes}, '</ol></li>';
374 wakaba 1.1 }
375 wakaba 1.2 } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {
376 wakaba 1.5 $r .= qq'<li id="$node_id" class="tree-doctype"><code>&lt;!DOCTYPE&gt;</code><ul class="attributes">';
377     $r .= qq[<li class="tree-doctype-name">Name = <q>@{[htescape ($child->name)]}</q></li>];
378     $r .= qq[<li class="tree-doctype-publicid">Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>];
379     $r .= qq[<li class="tree-doctype-systemid">System identifier = <q>@{[htescape ($child->system_id)]}</q></li>];
380 wakaba 1.2 $r .= '</ul></li>';
381     } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) {
382 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-id"><code>&lt;?@{[htescape ($child->target)]}</code> <q>@{[htescape ($child->data)]}</q><code>?&gt;</code></li>';
383 wakaba 1.1 } else {
384 wakaba 1.4 $r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error
385 wakaba 1.1 }
386     }
387 wakaba 1.2
388     $r .= '</ol>';
389     print STDOUT $r;
390     } # print_document_tree
391 wakaba 1.1
392     sub get_node_path ($) {
393     my $node = shift;
394     my @r;
395     while (defined $node) {
396     my $rs;
397     if ($node->node_type == 1) {
398     $rs = $node->manakai_local_name;
399     $node = $node->parent_node;
400     } elsif ($node->node_type == 2) {
401     $rs = '@' . $node->manakai_local_name;
402     $node = $node->owner_element;
403     } elsif ($node->node_type == 3) {
404     $rs = '"' . $node->data . '"';
405     $node = $node->parent_node;
406     } elsif ($node->node_type == 9) {
407     $rs = '';
408     $node = $node->parent_node;
409     } else {
410     $rs = '#' . $node->node_type;
411     $node = $node->parent_node;
412     }
413     unshift @r, $rs;
414     }
415     return join '/', @r;
416     } # get_node_path
417    
418 wakaba 1.6 sub get_node_link ($) {
419     return qq[<a href="#node-@{[refaddr $_[0]]}">] .
420     htescape (get_node_path ($_[0])) . qq[</a>];
421     } # get_node_link
422    
423 wakaba 1.7 {
424     my $Msg = {};
425    
426     sub load_text_catalog ($) {
427     my $lang = shift; # MUST be a canonical lang name
428     open my $file, '<', "cc-msg.$lang.txt" or die "$0: cc-msg.$lang.txt: $!";
429     while (<$file>) {
430     if (s/^([^;]+);([^;]*);//) {
431     my ($type, $cls, $msg) = ($1, $2, $_);
432     $msg =~ tr/\x0D\x0A//d;
433     $Msg->{$type} = [$cls, $msg];
434     }
435     }
436     } # load_text_catalog
437    
438     sub get_text ($) {
439     my ($type, $level) = @_;
440     $type = $level . ':' . $type if defined $level;
441     my @arg;
442     {
443     if (defined $Msg->{$type}) {
444     my $msg = $Msg->{$type}->[1];
445     $msg =~ s/\$([0-9]+)/defined $arg[$1] ? htescape ($arg[$1]) : '(undef)'/ge;
446     return ($Msg->{$type}->[0], $msg);
447     } elsif ($type =~ s/:([^:]*)$//) {
448     unshift @arg, $1;
449     redo;
450     }
451     }
452     return ('', htescape ($_[0]));
453     } # get_text
454    
455     }
456    
457 wakaba 1.1 =head1 AUTHOR
458    
459     Wakaba <w@suika.fam.cx>.
460    
461     =head1 LICENSE
462    
463     Copyright 2007 Wakaba <w@suika.fam.cx>
464    
465     This library is free software; you can redistribute it
466     and/or modify it under the same terms as Perl itself.
467    
468     =cut
469    
470 wakaba 1.8 ## $Date: 2007/07/01 06:21:46 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24