/[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.14 - (show annotations) (download)
Thu Aug 14 11:58:32 2008 UTC (16 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.13: +11 -0 lines
++ ChangeLog	14 Aug 2008 11:57:12 -0000
	* cc-style.css: Revised such that subdocument check results
	do not look stupid and that new class name rules
	for level-* and layer-* is reflected by icons.

2008-08-14  Wakaba  <wakaba@suika.fam.cx>

++ html/WebHACC/Language/ChangeLog	14 Aug 2008 11:58:26 -0000
	* Base.pm: Use "role" for source code sections.

2008-08-14  Wakaba  <wakaba@suika.fam.cx>

++ html/WebHACC/ChangeLog	14 Aug 2008 11:58:10 -0000
	* Output.pm (start_section): Roles set class="", too.  New "source"
	role for source code sections.

2008-08-14  Wakaba  <wakaba@suika.fam.cx>

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24