/[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.11 - (show annotations) (download)
Sun Aug 10 11:49:43 2008 UTC (16 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.10: +18 -7 lines
++ ChangeLog	10 Aug 2008 11:48:40 -0000
2008-08-10  Wakaba  <wakaba@suika.fam.cx>

	* cc-script.js: Functions for tabbing are added.

	* cc-style.css: Tab styling rules are added.

++ html/WebHACC/ChangeLog	10 Aug 2008 11:49:39 -0000
2008-08-10  Wakaba  <wakaba@suika.fam.cx>

	* Output.pm (html_header, start_section): Generate
	script elements for tab styling.

	* Result.pm: Link to error list section of transfer-protocol-level
	errors.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24