/[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.12 - (show annotations) (download)
Thu Aug 14 07:19:44 2008 UTC (16 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.11: +23 -8 lines
++ ChangeLog	14 Aug 2008 07:18:47 -0000
2008-08-14  Wakaba  <wakaba@suika.fam.cx>

	* cc-script.js, cc-style.js: Support for tab styling
	of the "input" subsections.  Support for the "details"
	widget.

++ html/WebHACC/Language/ChangeLog	14 Aug 2008 07:19:38 -0000
2008-08-14  Wakaba  <wakaba@suika.fam.cx>

	* CacheManifest.pm: Compile errors fixed.

++ html/WebHACC/ChangeLog	14 Aug 2008 07:19:14 -0000
2008-08-14  Wakaba  <wakaba@suika.fam.cx>

	* Output.pm: Support for inner tabs for "input" subsections.
	Improved support for "details" widget.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24