/[suikacvs]/test/html-webhacc/WebHACC/Language/DOM.pm
Suika

Contents of /test/html-webhacc/WebHACC/Language/DOM.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Mon Jul 21 09:40:59 2008 UTC (16 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.4: +3 -1 lines
++ ChangeLog	21 Jul 2008 09:38:49 -0000
	* cc.cgi: Code clean-up.

2008-07-21  Wakaba  <wakaba@suika.fam.cx>

++ html/WebHACC/Language/ChangeLog	21 Jul 2008 09:40:52 -0000
	* DOM.pm (generate_table_section): Use hierarhical table
	number for tables in subdocuments.

2008-07-21  Wakaba  <wakaba@suika.fam.cx>

++ html/WebHACC/ChangeLog	21 Jul 2008 09:40:19 -0000
	* Input.pm (full_subdocument_index): New method, for the
	support of hierarchical subdocument numbers.
	(start_section): Use hierarhical subdocument numbers for
	section headings.

	* Output.pm (http_header, http_error, html_header): New methods.

2008-07-21  Wakaba  <wakaba@suika.fam.cx>

1 package WebHACC::Language::DOM;
2 use strict;
3 require WebHACC::Language::Base;
4 push our @ISA, 'WebHACC::Language::Base';
5
6 use Scalar::Util qw[refaddr];
7
8 sub generate_structure_dump_section ($) {
9 my $self = shift;
10
11 my $out = $self->output;
12
13 $out->start_section (role => 'tree');
14
15 $out->start_tag ('ol', class => 'xoxo');
16
17 my @node = ($self->{structure});
18 while (@node) {
19 my $child = shift @node;
20 unless (ref $child) {
21 $out->html ($child);
22 next;
23 }
24
25 my $node_id = 'node-'.refaddr $child;
26 my $nt = $child->node_type;
27 if ($nt == $child->ELEMENT_NODE) {
28 my $child_nsuri = $child->namespace_uri;
29 $out->start_tag ('li', id => $node_id, class => 'tree-element');
30 $out->start_tag ('code',
31 title => defined $child_nsuri ? $child_nsuri : '');
32 $out->text ($child->tag_name); ## TODO: case
33 $out->end_tag ('code');
34
35 if ($child->has_attributes) {
36 $out->start_tag ('ul', class => 'attributes');
37 for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] }
38 @{$child->attributes}) {
39 $out->start_tag ('li', id => $attr->[3], class => 'tree-attribute');
40 $out->start_tag ('code',
41 title => defined $attr->[2] ? $attr->[2] : '');
42 $out->html ($attr->[0]); ## ISSUE: case
43 $out->end_tag ('code');
44 $out->text (' = ');
45 $out->start_tag ('q');
46 $out->text ($attr->[1]); ## TODO: children
47 $out->end_tag ('q');
48 }
49 $out->end_tag ('ul');
50 }
51
52 if ($child->has_child_nodes) {
53 $out->start_tag ('ol', class => 'children');
54 unshift @node, @{$child->child_nodes}, '</ol></li>';
55 }
56 } elsif ($nt == $child->TEXT_NODE) {
57 $out->start_tag ('li', id => $node_id, class => 'tree-text');
58 $out->start_tag ('q', lang => '');
59 $out->text ($child->data);
60 $out->end_tag ('q');
61 } elsif ($nt == $child->CDATA_SECTION_NODE) {
62 $out->start_tag ('li', id => $node_id, class => 'tree-cdata');
63 $out->start_tag ('code');
64 $out->text ('<![CDATA[');
65 $out->end_tag ('code');
66 $out->start_tag ('q', lang => '');
67 $out->text ($child->data);
68 $out->end_tag ('q');
69 $out->start_tag ('code');
70 $out->text (']]>');
71 $out->end_tag ('code');
72 } elsif ($nt == $child->COMMENT_NODE) {
73 $out->start_tag ('li', id => $node_id, class => 'tree-cdata');
74 $out->start_tag ('code');
75 $out->text ('<!--');
76 $out->end_tag ('code');
77 $out->start_tag ('q', lang => '');
78 $out->text ($child->data);
79 $out->end_tag ('q');
80 $out->start_tag ('code');
81 $out->text ('-->');
82 $out->end_tag ('code');
83 } elsif ($nt == $child->DOCUMENT_NODE) {
84 $out->start_tag ('li', id => $node_id, class => 'tree-document');
85 $out->text ('Document');
86 $out->start_tag ('ul', class => 'attributes');
87 my $cp = $child->manakai_charset;
88 if (defined $cp) {
89 $out->html (qq[<li><code>charset</code> parameter = <code>]);
90 $out->text ($cp);
91 $out->html ('</code>');
92 }
93 $out->html (qq[<li><code>inputEncoding</code> = ]);
94 my $ie = $child->input_encoding;
95 if (defined $ie) {
96 $out->code ($ie);
97 if ($child->manakai_has_bom) {
98 $out->html (qq[ (with <code class=charname><abbr>BOM</abbr></code>)]);
99 }
100 } else {
101 $out->html (qq[(<code>null</code>)]);
102 }
103 $out->html (qq[<li>@{[scalar main::get_text ('manakaiIsHTML:'.($child->manakai_is_html?1:0))]}</li>]);
104 $out->html (qq[<li>@{[scalar main::get_text ('manakaiCompatMode:'.$child->manakai_compat_mode)]}</li>]);
105 unless ($child->manakai_is_html) {
106 $out->html (qq[<li>XML version = ]);
107 $out->code ($child->xml_version);
108 if (defined $child->xml_encoding) {
109 $out->html (qq[<li>XML encoding = ]);
110 $out->code ($child->xml_encoding);
111 } else {
112 $out->html (qq[<li>XML encoding = (null)</li>]);
113 }
114 $out->html (qq[<li>XML standalone = @{[$child->xml_standalone ? 'true' : 'false']}</li>]);
115 }
116 $out->end_tag ('ul');
117 if ($child->has_child_nodes) {
118 $out->start_tag ('ol', class => 'children');
119 unshift @node, @{$child->child_nodes}, '</ol></li>';
120 }
121 } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {
122 $out->start_tag ('li', id => $node_id, class => 'tree-doctype');
123 $out->code ('<!DOCTYPE>');
124 $out->start_tag ('ul', class => 'attributes');
125
126 $out->start_tag ('li', class => 'tree-doctype-name');
127 $out->text ('Name = ');
128 $out->code ($child->name);
129
130 $out->start_tag ('li', class => 'tree-doctype-publicid');
131 $out->text ('Public identifier = ');
132 $out->code ($child->public_id);
133
134 $out->start_tag ('li', class => 'tree-doctype-systemid');
135 $out->text ('System identifier = ');
136 $out->code ($child->system_id);
137
138 $out->end_tag ('ul');
139 } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) {
140 $out->start_tag ('li', id => $node_id, class => 'tree-id');
141 $out->code ('<?');
142 $out->code ($child->target);
143 $out->text (' ');
144 $out->code ($child->data);
145 $out->code ('?>');
146 } else { # error
147 $out->start_tag ('li', id => $node_id, class => 'tree-unknown');
148 $out->text ($child->node_type . ' ' . $child->node_name);
149 }
150 }
151 $out->end_tag ('ol');
152
153 $out->end_section;
154 } # generate_structure_dump_section
155
156 sub generate_structure_error_section ($) {
157 my $self = shift;
158
159 my $out = $self->output;
160 $out->start_section (role => 'structure-errors');
161 $out->start_error_list (role => 'structure-errors');
162
163 my $input = $self->input;
164 my $result = $self->result;
165
166 require Whatpm::ContentChecker;
167 my $onerror = sub {
168 $result->add_error (@_, layer => 'structure');
169 };
170
171 my $onsubdoc = $self->onsubdoc;
172 if ($self->{structure}->node_type == $self->{structure}->ELEMENT_NODE) {
173 $self->{add_info} = Whatpm::ContentChecker->check_element
174 ($self->{structure}, $onerror, $onsubdoc);
175 } else {
176 $self->{add_info} = Whatpm::ContentChecker->check_document
177 ($self->{structure}, $onerror, $onsubdoc);
178 }
179
180 $out->end_error_list (role => 'structure-errors');
181 $out->end_section;
182 } # generate_structure_error_section
183
184 sub generate_additional_sections ($) {
185 my $self = shift;
186 $self->SUPER::generate_additional_sections;
187
188 $self->generate_table_section;
189
190 $self->generate_listing_section (
191 key => 'id', id => 'identifiers',
192 short_title => 'IDs', title => 'Identifiers',
193 );
194 $self->generate_listing_section (
195 key => 'term', id => 'terms',
196 short_title => 'Terms', title => 'Terms',
197 );
198 $self->generate_listing_section (
199 key => 'class', id => 'classes',
200 short_title => 'Classes', title => 'Classes',
201 );
202
203 $self->generate_rdf_section;
204 } # generate_additional_sections
205
206 sub generate_table_section ($) {
207 my $self = shift;
208
209 my $tables = $self->{add_info}->{table} || [];
210 return unless @$tables;
211
212 my $out = $self->output;
213 $out->start_section (id => 'tables', title => 'Tables');
214
215 $out->html (q[<!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->
216 <script src="../table-script.js" type="text/javascript"></script>
217 <noscript>
218 <p><em>Structure of tables are visualized here if scripting is enabled.</em></p>
219 </noscript>]);
220
221 require JSON;
222
223 my $i = 0;
224 for my $table (@$tables) {
225 $i++;
226 my $index = $out->input->full_subdocument_index;
227 $index = $index ? $index . '.' . $i : $i;
228 $out->start_section (id => 'table-' . $i,
229 title => 'Table #' . $index);
230
231 $out->start_tag ('dl');
232 $out->dt ('Table Element');
233 $out->start_tag ('dd');
234 $out->node_link ($table->{element});
235 $out->end_tag ('dl');
236 delete $table->{element};
237
238 for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption},
239 @{$table->{row}}) {
240 next unless $_;
241 delete $_->{element};
242 }
243
244 for (@{$table->{row_group}}) {
245 next unless $_;
246 next unless $_->{element};
247 $_->{type} = $_->{element}->manakai_local_name;
248 delete $_->{element};
249 }
250
251 for (@{$table->{cell}}) {
252 next unless $_;
253 for (@{$_}) {
254 next unless $_;
255 for (@$_) {
256 $_->{id} = refaddr $_->{element} if defined $_->{element};
257 delete $_->{element};
258 $_->{is_header} = $_->{is_header} ? 1 : 0;
259 }
260 }
261 }
262
263 my $id_prefix = $self->input->id_prefix;
264 $out->script (q[tableToCanvas (] .
265 JSON::objToJson ($table) .
266 q[, document.getElementById ('] . $id_prefix . 'table-' . $i . q[')] .
267 q[, '] . $id_prefix . q[');]);
268
269 $out->end_section;
270 }
271
272 $out->end_section;
273 } # generate_table_section
274
275 sub generate_listing_section ($%) {
276 my $self = shift;
277 my %opt = @_;
278
279 my $list = $self->{add_info}->{$opt{key}} || {};
280 return unless keys %$list;
281
282 my $out = $self->output;
283
284 $out->start_section (id => $opt{id},
285 title => $opt{title},
286 short_title => $opt{short_title});
287 $out->start_tag ('dl');
288
289 for my $id (sort {$a cmp $b} keys %$list) {
290 $out->start_tag ('dt');
291 $out->code ($id);
292 for (@{$list->{$id}}) {
293 $out->start_tag ('dd');
294 $out->node_link ($_);
295 }
296 }
297
298 $out->end_tag ('dl');
299 $out->end_section;
300 } # generate_listing_section
301
302 my $generate_rdf_resource_html = sub ($$) {
303 my ($resource, $out) = @_;
304
305 if (defined $resource->{uri}) {
306 $out->url ($resource->{uri});
307 } elsif (defined $resource->{bnodeid}) {
308 $out->text ('_:' . $resource->{bnodeid});
309 } elsif ($resource->{nodes}) {
310 $out->text ('(rdf:XMLLiteral)');
311 } elsif (defined $resource->{value}) {
312 $out->start_tag ('q',
313 lang => defined $resource->{language}
314 ? $resource->{language} : '');
315 $out->text ($resource->{value});
316 $out->end_tag ('q');
317
318 if (defined $resource->{datatype}) {
319 $out->text ('^^');
320 $out->url ($resource->{datatype});
321 } elsif (length $resource->{language}) {
322 $out->text ('@' . $resource->{language});
323 }
324 } else {
325 $out->text ('??'); ## NOTE: An error of the implementation.
326 }
327 }; # $generate_rdf_resource_html
328
329 ## TODO: Should we move this method to another module,
330 ## such as Base or RDF?
331 sub generate_rdf_section ($) {
332 my $self = shift;
333
334 my $list = $self->{add_info}->{rdf} || [];
335 return unless @$list;
336
337 my $out = $self->output;
338 $out->start_section (id => 'rdf', short_title => 'RDF',
339 title => 'RDF Triples');
340 $out->start_tag ('dl');
341
342 my $i = 0;
343 for my $rdf (@$list) {
344 $out->start_tag ('dt', id => 'rdf-' . $i++);
345 $out->node_link ($rdf->[0]);
346 $out->start_tag ('dd');
347 $out->start_tag ('dl');
348 for my $triple (@{$rdf->[1]}) {
349 $out->start_tag ('dt');
350 $out->node_link ($triple->[0]);
351 $out->start_tag ('dd');
352 $out->text ('Subject: ');
353 $generate_rdf_resource_html->($triple->[1] => $out);
354 $out->start_tag ('dd');
355 $out->text ('Predicate: ');
356 $generate_rdf_resource_html->($triple->[2] => $out);
357 $out->start_tag ('dd');
358 $out->text ('Object: ');
359 $generate_rdf_resource_html->($triple->[3] => $out);
360 }
361 $out->end_tag ('dl');
362 }
363 $out->end_tag ('dl');
364 $out->end_section;
365 } # generate_rdf_section
366
367 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24