/[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 - (hide 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 wakaba 1.1 package WebHACC::Output;
2     use strict;
3 wakaba 1.3
4 wakaba 1.1 require IO::Handle;
5 wakaba 1.3 use Scalar::Util qw/refaddr/;
6 wakaba 1.1
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 wakaba 1.10 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 wakaba 1.1 sub new ($) {
29 wakaba 1.9 require WebHACC::Input;
30     return bless {nav => [], section_rank => 1,
31     input => WebHACC::Input->new}, shift;
32 wakaba 1.1 } # new
33    
34     sub input ($;$) {
35     if (@_ > 1) {
36     if (defined $_[1]) {
37     $_[0]->{input} = $_[1];
38     } else {
39 wakaba 1.9 $_[0]->{input} = WebHACC::Input->new;
40 wakaba 1.1 }
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 wakaba 1.10 $self->html ('<' . $htescape_value->($tag_name)); # escape for safety
88 wakaba 1.1 if (exists $opt{id}) {
89     my $id = $self->input->id_prefix . $opt{id};
90 wakaba 1.10 $self->html (' id="' . $htescape_value->($id) . '"');
91 wakaba 1.1 delete $opt{id};
92     }
93     for (keys %opt) { # for safety
94 wakaba 1.10 $self->html (' ' . $htescape_value->($_) . '="' .
95     $htescape_value->($opt{$_}) . '"');
96 wakaba 1.1 }
97     $self->html ('>');
98     } # start_tag
99    
100     sub end_tag ($$) {
101 wakaba 1.10 shift->html ('</' . $htescape_value->(shift) . '>');
102 wakaba 1.1 } # end_tag
103    
104     sub start_section ($%) {
105     my ($self, %opt) = @_;
106 wakaba 1.4
107 wakaba 1.13 my $class = 'section';
108 wakaba 1.4 if (defined $opt{role}) {
109     if ($opt{role} eq 'parse-errors') {
110     $opt{id} ||= 'parse-errors';
111 wakaba 1.7 $opt{title} ||= 'Parse Errors Section';
112     $opt{short_title} ||= 'Parse Errors';
113 wakaba 1.4 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 wakaba 1.13 } elsif ($opt{role} eq 'subdoc') {
135     $class .= ' subdoc';
136     delete $opt{role};
137 wakaba 1.4 }
138     }
139    
140     $self->{section_rank}++;
141 wakaba 1.13 $self->html (qq[<div class="$class"]);
142 wakaba 1.1 if (defined $opt{id}) {
143 wakaba 1.13 my $prefix = $self->input->id_prefix;
144     $opt{parent_id} ||= $prefix;
145     my $id = $prefix . $opt{id};
146 wakaba 1.11 $self->html (' id="' . $htescape->($id) . '">');
147 wakaba 1.13 if ($self->{section_rank} == 2 or length $opt{parent_id}) {
148 wakaba 1.11 my $st = $opt{short_title} || $opt{title};
149     push @{$self->{nav}},
150     [$id => $st => $opt{text}];
151    
152     $self->start_tag ('script');
153 wakaba 1.13 $self->html (qq[ addSectionLink ('$id', ']);
154 wakaba 1.11 $self->nl_text ($st, text => $opt{text});
155 wakaba 1.12 if (defined $opt{parent_id}) {
156     $self->html (q[', '] . $opt{parent_id});
157     }
158 wakaba 1.11 $self->html (q[') ]);
159     $self->end_tag ('script');
160     }
161     } else {
162     $self->html ('>');
163 wakaba 1.1 }
164 wakaba 1.4 my $section_rank = $self->{section_rank};
165     $section_rank = 6 if $section_rank > 6;
166 wakaba 1.11 $self->html ('<h' . $section_rank . '>');
167 wakaba 1.7 $self->nl_text ($opt{title}, text => $opt{text});
168     $self->html ('</h' . $section_rank . '>');
169 wakaba 1.1 } # start_section
170    
171     sub end_section ($) {
172     my $self = shift;
173     $self->html ('</div>');
174     $self->{handle}->flush;
175 wakaba 1.4 $self->{section_rank}--;
176 wakaba 1.1 } # end_section
177 wakaba 1.4
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 wakaba 1.9 q[', '] . shift () . q[')]);
222 wakaba 1.4 } # add_source_to_parse_error_list
223 wakaba 1.1
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 wakaba 1.3 sub code ($$;%) {
233     my ($self, $content, %opt) = @_;
234     $self->start_tag ('code', %opt);
235     $self->text ($content);
236     $self->html ('</code>');
237 wakaba 1.1 } # code
238    
239 wakaba 1.3 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 wakaba 1.7 $self->nl_text ($content, text => $opt{text});
250 wakaba 1.3 } # dt
251    
252 wakaba 1.9 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 wakaba 1.1 sub link ($$%) {
282     my ($self, $content, %opt) = @_;
283 wakaba 1.2 $self->start_tag ('a', %opt, href => $opt{url});
284 wakaba 1.1 $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 wakaba 1.7 $self->nl_text ($content, text => $opt{text});
292 wakaba 1.1 $self->html ('</a>');
293     } # xref
294    
295 wakaba 1.2 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 wakaba 1.3 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 wakaba 1.10 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 wakaba 1.3 sub node_link ($$) {
346     my ($self, $node) = @_;
347 wakaba 1.10 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 wakaba 1.3 } # node_link
353    
354 wakaba 1.7 {
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 wakaba 1.1 sub nav_list ($) {
420     my $self = shift;
421     $self->html (q[<ul class="navigation" id="nav-items">]);
422     for (@{$self->{nav}}) {
423 wakaba 1.7 $self->html (qq[<li><a href="#@{[$htescape->($_->[0])]}">]);
424     $self->nl_text ($_->[1], text => $_->[2]);
425     $self->html ('</a>');
426 wakaba 1.1 }
427     $self->html ('</ul>');
428     } # nav_list
429 wakaba 1.2
430 wakaba 1.5 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 wakaba 1.7 $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 wakaba 1.5 <link rel="stylesheet" href="../cc-style.css" type="text/css">
451 wakaba 1.8 <script src="../cc-script.js"></script>
452 wakaba 1.5 </head>
453 wakaba 1.11 <body onclick=" return onbodyclick (event) " onload=" onbodyload () ">
454 wakaba 1.7 <h1>]);
455     $self->nl_text (q[WebHACC:Heading]);
456 wakaba 1.11 $self->html (q[</h1><script> insertNavSections () </script>]);
457 wakaba 1.5 } # html_header
458 wakaba 1.9
459     sub generate_input_section ($$) {
460     my ($out, $cgi) = @_;
461    
462     my $options = sub ($) {
463     my $context = shift;
464    
465 wakaba 1.12 $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 wakaba 1.9 $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 wakaba 1.12 $out->html (q[<script> insertNavSections ('input') </script>]);
544 wakaba 1.9
545 wakaba 1.12 $out->start_section (id => 'input-url', title => 'By URL',
546     parent_id => 'input');
547 wakaba 1.10 $out->start_tag ('form', action => './#result-summary',
548     'accept-charset' => 'utf-8',
549 wakaba 1.9 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 wakaba 1.12 $out->end_tag ('button');
566    
567     $options->('url');
568 wakaba 1.9
569     $out->end_tag ('form');
570     $out->end_section;
571    
572     $out->end_tag ('fieldset');
573    
574     ## TODO: File upload
575    
576 wakaba 1.12 $out->start_section (id => 'input-text', title => 'By direct input',
577     parent_id => 'input');
578 wakaba 1.10 $out->start_tag ('form', action => './#result-summary',
579     'accept-charset' => 'utf-8',
580 wakaba 1.9 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 wakaba 1.10 $out->html ($htescape_value->($s)) if defined $s;
592 wakaba 1.9 $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 wakaba 1.12 $options->('text');
602    
603 wakaba 1.9 $out->end_tag ('form');
604     $out->end_section;
605 wakaba 1.12
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 wakaba 1.9
614     $out->end_section;
615     } # generate_input_section
616 wakaba 1.2
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 wakaba 1.1
625     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24