/[suikacvs]/test/html-webhacc/WebHACC/Output.pm
Suika

Contents of /test/html-webhacc/WebHACC/Output.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations) (download)
Sat Aug 2 06:07:11 2008 UTC (16 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.9: +41 -8 lines
++ html/WebHACC/Language/ChangeLog	2 Aug 2008 06:07:07 -0000
2008-08-02  Wakaba  <wakaba@suika.fam.cx>

	* WebIDL.pm (generate_structure_error_section): New method.

++ html/WebHACC/ChangeLog	2 Aug 2008 06:06:40 -0000
2008-08-02  Wakaba  <wakaba@suika.fam.cx>

	* Output.pm ($htescape_value): New code.  Some methods should
	invoke this code instead of original $htescape, since that
	code will convert invisible characters into HTML tags
	such that that code is not suitable for attribute values
	and CDATA/PCDATA element contents.
	($get_object_path): New.  Future revision of this code should
	support real "path" to the node object.  The current version
	only dumps the type and the name of the node itself.
	(node_link): Support for non-DOM nodes.

	* Result.pm (add_error): Support for non-DOM nodes.

1 package WebHACC::Output;
2 use strict;
3
4 require IO::Handle;
5 use Scalar::Util qw/refaddr/;
6
7 my $htescape = sub ($) {
8 my $s = $_[0];
9 $s =~ s/&/&amp;/g;
10 $s =~ s/</&lt;/g;
11 $s =~ s/>/&gt;/g;
12 $s =~ s/"/&quot;/g;
13 $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{
14 sprintf '<var>U+%04X</var>', ord $1;
15 }ge;
16 return $s;
17 };
18
19 my $htescape_value = sub ($) {
20 my $s = $_[0];
21 $s =~ s/&/&amp;/g;
22 $s =~ s/</&lt;/g;
23 $s =~ s/>/&gt;/g;
24 $s =~ s/"/&quot;/g;
25 return $s;
26 };
27
28 sub new ($) {
29 require WebHACC::Input;
30 return bless {nav => [], section_rank => 1,
31 input => WebHACC::Input->new}, shift;
32 } # new
33
34 sub input ($;$) {
35 if (@_ > 1) {
36 if (defined $_[1]) {
37 $_[0]->{input} = $_[1];
38 } else {
39 $_[0]->{input} = WebHACC::Input->new;
40 }
41 }
42
43 return $_[0]->{input};
44 } # input
45
46 sub handle ($;$) {
47 if (@_ > 1) {
48 if (defined $_[1]) {
49 $_[0]->{handle} = $_[1];
50 } else {
51 delete $_[0]->{handle};
52 }
53 }
54
55 return $_[0]->{handle};
56 } # handle
57
58 sub set_utf8 ($) {
59 binmode shift->{handle}, ':utf8';
60 } # set_utf8
61
62 sub set_flush ($) {
63 shift->{handle}->autoflush (1);
64 } # set_flush
65
66 sub unset_flush ($) {
67 shift->{handle}->autoflush (0);
68 } # unset_flush
69
70 sub html ($$) {
71 shift->{handle}->print (shift);
72 } # html
73
74 sub text ($$) {
75 shift->html ($htescape->(shift));
76 } # text
77
78 sub url ($$%) {
79 my ($self, $url, %opt) = @_;
80 $self->html (q[<code class=uri>&lt;]);
81 $self->link ($url, %opt, url => $url);
82 $self->html (q[></code>]);
83 } # url
84
85 sub start_tag ($$%) {
86 my ($self, $tag_name, %opt) = @_;
87 $self->html ('<' . $htescape_value->($tag_name)); # escape for safety
88 if (exists $opt{id}) {
89 my $id = $self->input->id_prefix . $opt{id};
90 $self->html (' id="' . $htescape_value->($id) . '"');
91 delete $opt{id};
92 }
93 for (keys %opt) { # for safety
94 $self->html (' ' . $htescape_value->($_) . '="' .
95 $htescape_value->($opt{$_}) . '"');
96 }
97 $self->html ('>');
98 } # start_tag
99
100 sub end_tag ($$) {
101 shift->html ('</' . $htescape_value->(shift) . '>');
102 } # end_tag
103
104 sub start_section ($%) {
105 my ($self, %opt) = @_;
106
107 if (defined $opt{role}) {
108 if ($opt{role} eq 'parse-errors') {
109 $opt{id} ||= 'parse-errors';
110 $opt{title} ||= 'Parse Errors Section';
111 $opt{short_title} ||= 'Parse Errors';
112 delete $opt{role};
113 } elsif ($opt{role} eq 'structure-errors') {
114 $opt{id} ||= 'document-errors';
115 $opt{title} ||= 'Structural Errors';
116 $opt{short_title} ||= 'Struct. Errors';
117 delete $opt{role};
118 } elsif ($opt{role} eq 'reformatted') {
119 $opt{id} ||= 'document-tree';
120 $opt{title} ||= 'Reformatted Document Source';
121 $opt{short_title} ||= 'Reformatted';
122 delete $opt{role}
123 } elsif ($opt{role} eq 'tree') {
124 $opt{id} ||= 'document-tree';
125 $opt{title} ||= 'Document Tree';
126 $opt{short_title} ||= 'Tree';
127 delete $opt{role};
128 } elsif ($opt{role} eq 'structure') {
129 $opt{id} ||= 'document-structure';
130 $opt{title} ||= 'Document Structure';
131 $opt{short_title} ||= 'Structure';
132 delete $opt{role};
133 }
134 }
135
136 $self->{section_rank}++;
137 $self->html ('<div class=section');
138 if (defined $opt{id}) {
139 my $id = $self->input->id_prefix . $opt{id};
140 $self->html (' id="' . $htescape->($id) . '"');
141 push @{$self->{nav}},
142 [$id => $opt{short_title} || $opt{title} => $opt{text}]
143 if $self->{section_rank} == 2;
144 }
145 my $section_rank = $self->{section_rank};
146 $section_rank = 6 if $section_rank > 6;
147 $self->html ('><h' . $section_rank . '>');
148 $self->nl_text ($opt{title}, text => $opt{text});
149 $self->html ('</h' . $section_rank . '>');
150 } # start_section
151
152 sub end_section ($) {
153 my $self = shift;
154 $self->html ('</div>');
155 $self->{handle}->flush;
156 $self->{section_rank}--;
157 } # end_section
158
159 sub start_error_list ($%) {
160 my ($self, %opt) = @_;
161
162 if (defined $opt{role}) {
163 if ($opt{role} eq 'parse-errors') {
164 $opt{id} ||= 'parse-errors-list';
165 delete $opt{role};
166 } elsif ($opt{role} eq 'structure-errors') {
167 $opt{id} ||= 'document-errors-list';
168 delete $opt{role};
169 }
170 }
171
172 $self->start_tag ('dl', %opt);
173 } # start_error_list
174
175 sub end_error_list ($%) {
176 my ($self, %opt) = @_;
177
178 if (defined $opt{role}) {
179 if ($opt{role} eq 'parse-errors') {
180 delete $opt{role};
181 $self->end_tag ('dl');
182 ## NOTE: For parse error list, the |add_source_to_parse_error_list|
183 ## method is invoked at the end of |generate_source_string_section|,
184 ## since that generation method is invoked after the error list
185 ## is generated.
186 } elsif ($opt{role} eq 'structure-errors') {
187 delete $opt{role};
188 $self->end_tag ('dl');
189 $self->add_source_to_parse_error_list ('document-errors-list');
190 } else {
191 $self->end_tag ('dl');
192 }
193 } else {
194 $self->end_tag ('dl');
195 }
196 } # end_error_list
197
198 sub add_source_to_parse_error_list ($$) {
199 my $self = shift;
200
201 $self->script (q[addSourceToParseErrorList ('] . $self->input->id_prefix .
202 q[', '] . shift () . q[')]);
203 } # add_source_to_parse_error_list
204
205 sub start_code_block ($) {
206 shift->html ('<pre><code>');
207 } # start_code_block
208
209 sub end_code_block ($) {
210 shift->html ('</code></pre>');
211 } # end_code_block
212
213 sub code ($$;%) {
214 my ($self, $content, %opt) = @_;
215 $self->start_tag ('code', %opt);
216 $self->text ($content);
217 $self->html ('</code>');
218 } # code
219
220 sub script ($$;%) {
221 my ($self, $content, %opt) = @_;
222 $self->start_tag ('script', %opt);
223 $self->html ($content);
224 $self->html ('</script>');
225 } # script
226
227 sub dt ($$;%) {
228 my ($self, $content, %opt) = @_;
229 $self->start_tag ('dt', %opt);
230 $self->nl_text ($content, text => $opt{text});
231 } # dt
232
233 sub select ($$%) {
234 my ($self, $options, %opt) = @_;
235
236 my $selected = $opt{selected};
237 delete $opt{selected};
238
239 $self->start_tag ('select', %opt);
240
241 my @options = @$options;
242 while (@options) {
243 my $opt = shift @options;
244 if ($opt->{options}) {
245 $self->html ('<optgroup label="');
246 $self->nl_text ($opt->{label});
247 $self->html ('">');
248 unshift @options, @{$opt->{options}}, {end_options => 1};
249 } elsif ($opt->{end_options}) {
250 $self->end_tag ('optgroup');
251 } else {
252 $self->start_tag ('option', value => $opt->{value},
253 ((defined $selected and $opt->{value} eq $selected)
254 ? (selected => '') : ()));
255 $self->nl_text (defined $opt->{label} ? $opt->{label} : $opt->{value});
256 }
257 }
258
259 $self->end_tag ('select');
260 } # select
261
262 sub link ($$%) {
263 my ($self, $content, %opt) = @_;
264 $self->start_tag ('a', %opt, href => $opt{url});
265 $self->text ($content);
266 $self->html ('</a>');
267 } # link
268
269 sub xref ($$%) {
270 my ($self, $content, %opt) = @_;
271 $self->html ('<a href="#' . $htescape->($self->input->id_prefix . $opt{target}) . '">');
272 $self->nl_text ($content, text => $opt{text});
273 $self->html ('</a>');
274 } # xref
275
276 sub link_to_webhacc ($$%) {
277 my ($self, $content, %opt) = @_;
278 $opt{url} = './?uri=' . $self->encode_url_component ($opt{url});
279 $self->link ($content, %opt);
280 } # link_to_webhacc
281
282 my $get_node_path = sub ($) {
283 my $node = shift;
284 my @r;
285 while (defined $node) {
286 my $rs;
287 if ($node->node_type == 1) {
288 $rs = $node->node_name;
289 $node = $node->parent_node;
290 } elsif ($node->node_type == 2) {
291 $rs = '@' . $node->node_name;
292 $node = $node->owner_element;
293 } elsif ($node->node_type == 3) {
294 $rs = '"' . $node->data . '"';
295 $node = $node->parent_node;
296 } elsif ($node->node_type == 9) {
297 @r = ('') unless @r;
298 $rs = '';
299 $node = $node->parent_node;
300 } else {
301 $rs = '#' . $node->node_type;
302 $node = $node->parent_node;
303 }
304 unshift @r, $rs;
305 }
306 return join '/', @r;
307 }; # $get_node_path
308
309 my $get_object_path = sub ($) {
310 my $node = shift;
311 my @r;
312 while (defined $node) {
313 my $ref = ref $node;
314 $ref =~ /([^:]+)$/;
315 my $rs = $1;
316 my $node_name = $node->node_name;
317 if (defined $node_name) {
318 $rs .= ' <code>' . $htescape->($node_name) . '</code>';
319 }
320 $node = undef;
321 unshift @r, $rs;
322 }
323 return join '/', @r;
324 }; # $get_object_path
325
326 sub node_link ($$) {
327 my ($self, $node) = @_;
328 if ($node->isa ('Message::IF::Node')) {
329 $self->xref ($get_node_path->($node), target => 'node-' . refaddr $node);
330 } else {
331 $self->html ($get_object_path->($node));
332 }
333 } # node_link
334
335 {
336 my $Msg = {};
337
338 sub load_text_catalog ($$) {
339 my $self = shift;
340
341 my $lang = shift; # MUST be a canonical lang name
342 my $file_name = qq[cc-msg.$lang.txt];
343 $lang = 'en' unless -f $file_name;
344 $self->{primary_language} = $lang;
345
346 open my $file, '<:utf8', $file_name or die "$0: $file_name: $!";
347 while (<$file>) {
348 if (s/^([^;]+);([^;]*);//) {
349 my ($type, $cls, $msg) = ($1, $2, $_);
350 $msg =~ tr/\x0D\x0A//d;
351 $Msg->{$type} = [$cls, $msg];
352 }
353 }
354 } # load_text_catalog
355
356 sub nl_text ($$;%) {
357 my ($self, $type, %opt) = @_;
358 my $node = $opt{node};
359
360 my @arg;
361 {
362 if (defined $Msg->{$type}) {
363 my $msg = $Msg->{$type}->[1];
364 if ($msg =~ /<var>/) {
365 $msg =~ s{<var>\$([0-9]+)</var>}{
366 defined $arg[$1] ? $htescape->($arg[$1]) : '(undef)';
367 }ge;
368 $msg =~ s{<var>{\@([A-Za-z0-9:_.-]+)}</var>}{
369 UNIVERSAL::can ($node, 'get_attribute_ns')
370 ? $htescape->($node->get_attribute_ns (undef, $1)) : ''
371 }ge;
372 $msg =~ s{<var>{\@}</var>}{
373 UNIVERSAL::can ($node, 'value') ? $htescape->($node->value) : ''
374 }ge;
375 $msg =~ s{<var>{text}</var>}{
376 defined $opt{text} ? $htescape->($opt{text}) : ''
377 }ge;
378 $msg =~ s{<var>{local-name}</var>}{
379 UNIVERSAL::can ($node, 'manakai_local_name')
380 ? $htescape->($node->manakai_local_name) : ''
381 }ge;
382 $msg =~ s{<var>{element-local-name}</var>}{
383 (UNIVERSAL::can ($node, 'owner_element') and
384 $node->owner_element)
385 ? $htescape->($node->owner_element->manakai_local_name) : ''
386 }ge;
387 }
388 $self->html ($msg);
389 return;
390 } elsif ($type =~ s/:([^:]*)$//) {
391 unshift @arg, $1;
392 redo;
393 }
394 }
395 $self->text ($type);
396 } # nl_text
397
398 }
399
400 sub nav_list ($) {
401 my $self = shift;
402 $self->html (q[<ul class="navigation" id="nav-items">]);
403 for (@{$self->{nav}}) {
404 $self->html (qq[<li><a href="#@{[$htescape->($_->[0])]}">]);
405 $self->nl_text ($_->[1], text => $_->[2]);
406 $self->html ('</a>');
407 }
408 $self->html ('</ul>');
409 } # nav_list
410
411 sub http_header ($) {
412 shift->html (qq[Content-Type: text/html; charset=utf-8\n\n]);
413 } # http_header
414
415 sub http_error ($$) {
416 my $self = shift;
417 my $code = 0+shift;
418 my $text = {
419 404 => 'Not Found',
420 }->{$code};
421 $self->html (qq[Status: $code $text\nContent-Type: text/html ; charset=us-ascii\n\n$code $text]);
422 } # http_error
423
424 sub html_header ($) {
425 my $self = shift;
426 $self->html (q[<!DOCTYPE html>]);
427 $self->start_tag ('html', lang => $self->{primary_language});
428 $self->html (q[<head><title>]);
429 $self->nl_text (q[WebHACC:Title]);
430 $self->html (q[</title>
431 <link rel="stylesheet" href="../cc-style.css" type="text/css">
432 <script src="../cc-script.js"></script>
433 </head>
434 <body>
435 <h1>]);
436 $self->nl_text (q[WebHACC:Heading]);
437 $self->html ('</h1>');
438 } # html_header
439
440 sub generate_input_section ($$) {
441 my ($out, $cgi) = @_;
442
443 my $options = sub ($) {
444 my $context = shift;
445
446 $out->html (q[<div class=details><p class=legend onclick="nextSibling.style.display = nextSibling.style.display == 'none' ? 'block' : 'none'">]);
447 $out->nl_text (q[Options]);
448 $out->start_tag ('div');
449
450 if ($context eq 'url') {
451 $out->start_tag ('p');
452 $out->start_tag ('label');
453 $out->start_tag ('input', type => 'checkbox', name => 'error-page',
454 value => 1,
455 ($cgi->get_parameter ('error-page')
456 ? (checked => '') : ()));
457 $out->nl_text ('Check error page');
458 $out->end_tag ('label');
459 }
460
461 $out->start_tag ('p');
462 $out->start_tag ('label');
463 $out->nl_text (q[Content type]);
464 $out->text (': ');
465 $out->select ([
466 {value => '', label => 'As specified'},
467 {value => 'application/atom+xml'},
468 {value => 'application/xhtml+xml'},
469 {value => 'application/xml'},
470 {value => 'text/html'},
471 {value => 'text/xml'},
472 {value => 'text/css'},
473 {value => 'text/cache-manifest'},
474 {value => 'text/x-webidl'},
475 ], name => 'i', selected => scalar $cgi->get_parameter ('i'));
476 $out->end_tag ('label');
477
478 if ($context ne 'text') {
479 $out->start_tag ('p');
480 $out->start_tag ('label');
481 $out->nl_text (q[Charset]);
482 $out->text (q[: ]);
483 $out->select ([
484 {value => '', label => 'As specified'},
485 {label => 'Japanese charsets', options => [
486 {value => 'Windows-31J'},
487 {value => 'Shift_JIS'},
488 {value => 'EUC-JP'},
489 {value => 'ISO-2022-JP'},
490 ]},
491 {label => 'European charsets', options => [
492 {value => 'Windows-1252'},
493 {value => 'ISO-8859-1'},
494 {value => 'US-ASCII'},
495 ]},
496 {label => 'Asian charsets', options => [
497 {value => 'Windows-874'},
498 {value => 'ISO-8859-11'},
499 {value => 'TIS-620'},
500 ]},
501 {label => 'Unicode charsets', options => [
502 {value => 'UTF-8'},
503 {value => 'UTF-8n'},
504 ]},
505 ], name => 'charset',
506 selected => scalar $cgi->get_parameter ('charset'));
507 $out->end_tag ('label');
508 }
509
510 if ($context eq 'text') {
511 $out->start_tag ('p');
512 $out->start_tag ('label');
513 $out->nl_text ('Setting innerHTML');
514 $out->text (': ');
515 $out->start_tag ('input', name => 'e',
516 value => scalar $cgi->get_parameter ('e'));
517 $out->end_tag ('label');
518 }
519
520 $out->html (q[</div></div>]);
521 }; # $options
522
523 $out->start_section (id => 'input', title => 'Input');
524
525 $out->start_section (id => 'input-url', title => 'By URL');
526 $out->start_tag ('form', action => './#result-summary',
527 'accept-charset' => 'utf-8',
528 method => 'get');
529 $out->start_tag ('input', type => 'hidden', name => '_charset_');
530
531 $out->start_tag ('p');
532 $out->start_tag ('label');
533 $out->nl_text ('URL');
534 $out->text (': ');
535 $out->start_tag ('input',
536 name => 'uri',
537 type => 'url',
538 value => $cgi->get_parameter ('uri'));
539 $out->end_tag ('label');
540
541 $options->('url');
542
543 $out->start_tag ('p');
544 $out->start_tag ('button', type => 'submit');
545 $out->nl_text ('Check');
546
547 $out->end_tag ('form');
548 $out->end_section;
549
550 $out->end_tag ('fieldset');
551
552 ## TODO: File upload
553
554 $out->start_section (id => 'input-text', title => 'By direct input');
555 $out->start_tag ('form', action => './#result-summary',
556 'accept-charset' => 'utf-8',
557 method => 'post');
558 $out->start_tag ('input', type => 'hidden', name => '_charset_');
559
560 $out->start_tag ('p');
561 $out->start_tag ('label');
562 $out->nl_text ('Document source to check');
563 $out->text (': ');
564 $out->start_tag ('br');
565 $out->start_tag ('textarea',
566 name => 's');
567 my $s = $cgi->get_parameter ('s');
568 $out->html ($htescape_value->($s)) if defined $s;
569 $out->end_tag ('textarea');
570 $out->end_tag ('label');
571
572 $options->('text');
573
574 $out->start_tag ('p');
575 $out->start_tag ('button', type => 'submit',
576 onclick => 'form.method = form.s.value.length > 512 ? "post" : "get"');
577 $out->nl_text ('Check');
578 $out->end_tag ('button');
579
580 $out->end_tag ('form');
581 $out->end_section;
582
583 $out->end_section;
584 } # generate_input_section
585
586 sub encode_url_component ($$) {
587 shift;
588 require Encode;
589 my $s = Encode::encode ('utf8', shift);
590 $s =~ s/([^0-9A-Za-z_.~-])/sprintf '%%%02X', ord $1/ge;
591 return $s;
592 } # encode_url_component
593
594 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24