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