/[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.13 - (show annotations) (download)
Thu Aug 14 09:16:52 2008 UTC (16 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.12: +10 -5 lines
++ ChangeLog	14 Aug 2008 09:14:23 -0000
	* cc-script.js, cc-style.css: Support for tab styling
	of subdoc sections.

	* cc-script.js (onbodyload): Scroll to the pointed
	element, if possible.

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

++ html/WebHACC/ChangeLog	14 Aug 2008 09:16:45 -0000
	* Input.pm (id_prefix): Use the parent subdoc's id_prefix
	as the prefix of the subdoc id_prefix (e.g. subdoc-1-subdoc-1-
	for subdoc #1.1).
	(start_section): Set the role of the section element as subdoc.

	* Output.pm (start_section): Support for "subdoc" role.
	Use parent input's id_prefix as parent_id.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24