]>
Commit | Line | Data |
---|---|---|
1 | package HTML::Element::Library; | |
2 | ||
3 | use 5.006001; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | ||
8 | our $DEBUG = 0; | |
9 | #our $DEBUG = 1; | |
10 | ||
11 | use Array::Group qw(:all); | |
12 | use Carp qw(confess); | |
13 | use Data::Dumper; | |
14 | use HTML::Element; | |
15 | use List::MoreUtils qw/:all/; | |
16 | use Params::Validate qw(:all); | |
17 | use Scalar::Listify; | |
18 | #use Tie::Cycle; | |
19 | use List::Rotation::Cycle; | |
20 | ||
21 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | |
22 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |
23 | our @EXPORT = qw(); | |
24 | ||
25 | ||
26 | ||
27 | our $VERSION = '3.53'; | |
28 | ||
29 | ||
30 | # Preloaded methods go here. | |
31 | ||
32 | sub HTML::Element::siblings { | |
33 | my $element = shift; | |
34 | my $p = $element->parent; | |
35 | return () unless $p; | |
36 | $p->content_list; | |
37 | } | |
38 | ||
39 | sub HTML::Element::passover { | |
40 | my ($tree, $child_id) = @_; | |
41 | ||
42 | #warn "ARGS: my ($tree, $child)"; | |
43 | ||
44 | my $exodus = $tree->look_down(id => $child_id); | |
45 | ||
46 | my @s = HTML::Element::siblings($exodus); | |
47 | ||
48 | for my $s (@s) { | |
49 | next unless ref $s; | |
50 | if ($s->attr('id') eq $child_id) { | |
51 | ; | |
52 | } else { | |
53 | $s->delete; | |
54 | } | |
55 | } | |
56 | ||
57 | return $exodus; # Goodbye Egypt! http://en.wikipedia.org/wiki/Passover | |
58 | ||
59 | } | |
60 | ||
61 | sub HTML::Element::sibdex { | |
62 | ||
63 | my $element = shift; | |
64 | firstidx { $_ eq $element } $element->siblings | |
65 | ||
66 | } | |
67 | ||
68 | sub HTML::Element::addr { goto &HTML::Element::sibdex } | |
69 | ||
70 | sub HTML::Element::replace_content { | |
71 | my $elem = shift; | |
72 | $elem->delete_content; | |
73 | $elem->push_content(@_); | |
74 | } | |
75 | ||
76 | sub HTML::Element::wrap_content { | |
77 | my($self, $wrap) = @_; | |
78 | my $content = $self->content; | |
79 | if (ref $content) { | |
80 | $wrap->push_content(@$content); | |
81 | @$content = ($wrap); | |
82 | } | |
83 | else { | |
84 | $self->push_content($wrap); | |
85 | } | |
86 | $wrap; | |
87 | } | |
88 | ||
89 | sub HTML::Element::Library::super_literal { | |
90 | my($text) = @_; | |
91 | ||
92 | HTML::Element->new('~literal', text => $text); | |
93 | } | |
94 | ||
95 | ||
96 | sub HTML::Element::position { | |
97 | # Report coordinates by chasing addr's up the | |
98 | # HTML::ElementSuper tree. We know we've reached | |
99 | # the top when a) there is no parent, or b) the | |
100 | # parent is some HTML::Element unable to report | |
101 | # it's position. | |
102 | my $p = shift; | |
103 | my @pos; | |
104 | while ($p) { | |
105 | my $a = $p->addr; | |
106 | unshift(@pos, $a) if defined $a; | |
107 | $p = $p->parent; | |
108 | } | |
109 | @pos; | |
110 | } | |
111 | ||
112 | ||
113 | sub HTML::Element::content_handler { | |
114 | my ($tree, $id_name, $content) = @_; | |
115 | ||
116 | $tree->set_child_content(id => $id_name, $content); | |
117 | ||
118 | } | |
119 | ||
120 | ||
121 | sub make_counter { | |
122 | my $i = 1; | |
123 | sub { | |
124 | shift() . ':' . $i++ | |
125 | } | |
126 | } | |
127 | ||
128 | ||
129 | sub HTML::Element::iter { | |
130 | my ($tree, $p, @data) = @_; | |
131 | ||
132 | # warn 'P: ' , $p->attr('id') ; | |
133 | # warn 'H: ' , $p->as_HTML; | |
134 | ||
135 | # my $id_incr = make_counter; | |
136 | my @item = map { | |
137 | my $new_item = clone $p; | |
138 | $new_item->replace_content($_); | |
139 | # $new_item->attr('id', $id_incr->( $p->attr('id') )); | |
140 | $new_item; | |
141 | } @data; | |
142 | ||
143 | $p->replace_with(@item); | |
144 | ||
145 | } | |
146 | ||
147 | ||
148 | sub HTML::Element::iter2 { | |
149 | ||
150 | my $tree = shift; | |
151 | ||
152 | #warn "INPUT TO TABLE2: ", Dumper \@_; | |
153 | ||
154 | my %p = validate( | |
155 | @_, { | |
156 | wrapper_ld => { default => ['_tag' => 'dl'] }, | |
157 | wrapper_data => 1, | |
158 | wrapper_proc => { default => undef }, | |
159 | item_ld => { default => sub { | |
160 | my $tree = shift; | |
161 | [ | |
162 | $tree->look_down('_tag' => 'dt'), | |
163 | $tree->look_down('_tag' => 'dd') | |
164 | ]; | |
165 | } | |
166 | }, | |
167 | item_data => { default => sub { my ($wrapper_data) = @_; | |
168 | shift(@{$wrapper_data}) ; | |
169 | }}, | |
170 | item_proc => { | |
171 | default => sub { | |
172 | my ($item_elems, $item_data, $row_count) = @_; | |
173 | $item_elems->[$_]->replace_content($item_data->[$_]) for (0,1) ; | |
174 | $item_elems; | |
175 | }}, | |
176 | splice => { default => sub { | |
177 | my ($container, @item_elems) = @_; | |
178 | $container->splice_content(0, 2, @item_elems); | |
179 | } | |
180 | }, | |
181 | debug => {default => 0} | |
182 | } | |
183 | ); | |
184 | ||
185 | warn "wrapper_data: " . Dumper $p{wrapper_data} if $p{debug} ; | |
186 | ||
187 | my $container = ref_or_ld($tree, $p{wrapper_ld}); | |
188 | warn "wrapper_(preproc): " . $container->as_HTML if $p{debug} ; | |
189 | $p{wrapper_proc}->($container) if defined $p{wrapper_proc} ; | |
190 | warn "wrapper_(postproc): " . $container->as_HTML if $p{debug} ; | |
191 | ||
192 | my $_item_elems = $p{item_ld}->($container); | |
193 | ||
194 | ||
195 | ||
196 | my $row_count; | |
197 | my @item_elem; | |
198 | { | |
199 | my $item_data = $p{item_data}->($p{wrapper_data}); | |
200 | last unless defined $item_data; | |
201 | ||
202 | warn Dumper("item_data", $item_data); | |
203 | ||
204 | ||
205 | my $item_elems = [ map { $_->clone } @{$_item_elems} ] ; | |
206 | ||
207 | if ($p{debug}) { | |
208 | for (@{$item_elems}) { | |
209 | warn "ITEM_ELEMS ", $_->as_HTML; | |
210 | } | |
211 | } | |
212 | ||
213 | my $new_item_elems = $p{item_proc}->($item_elems, $item_data, ++$row_count); | |
214 | ||
215 | if ($p{debug}) { | |
216 | for (@{$new_item_elems}) { | |
217 | warn "NEWITEM_ELEMS ", $_->as_HTML; | |
218 | } | |
219 | } | |
220 | ||
221 | ||
222 | push @item_elem, @{$new_item_elems} ; | |
223 | ||
224 | redo; | |
225 | } | |
226 | ||
227 | warn "pushing " . @item_elem . " elems " if $p{debug} ; | |
228 | ||
229 | $p{splice}->($container, @item_elem); | |
230 | ||
231 | } | |
232 | ||
233 | sub HTML::Element::dual_iter { | |
234 | my ($parent, $data) = @_; | |
235 | ||
236 | my ($prototype_a, $prototype_b) = $parent->content_list; | |
237 | ||
238 | # my $id_incr = make_counter; | |
239 | ||
240 | my $i; | |
241 | ||
242 | @$data %2 == 0 or | |
243 | confess 'dataset does not contain an even number of members'; | |
244 | ||
245 | my @iterable_data = ngroup 2 => @$data; | |
246 | ||
247 | my @item = map { | |
248 | my ($new_a, $new_b) = map { clone $_ } ($prototype_a, $prototype_b) ; | |
249 | $new_a->splice_content(0,1, $_->[0]); | |
250 | $new_b->splice_content(0,1, $_->[1]); | |
251 | #$_->attr('id', $id_incr->($_->attr('id'))) for ($new_a, $new_b) ; | |
252 | ($new_a, $new_b) | |
253 | } @iterable_data; | |
254 | ||
255 | $parent->splice_content(0, 2, @item); | |
256 | ||
257 | } | |
258 | ||
259 | ||
260 | sub HTML::Element::set_child_content { | |
261 | my $tree = shift; | |
262 | my $content = pop; | |
263 | my @look_down = @_; | |
264 | ||
265 | my $content_tag = $tree->look_down(@look_down); | |
266 | ||
267 | unless ($content_tag) { | |
268 | warn "criteria [@look_down] not found"; | |
269 | return; | |
270 | } | |
271 | ||
272 | $content_tag->replace_content($content); | |
273 | ||
274 | } | |
275 | ||
276 | sub HTML::Element::highlander { | |
277 | my ($tree, $local_root_id, $aref, @arg) = @_; | |
278 | ||
279 | ref $aref eq 'ARRAY' or confess | |
280 | "must supply array reference"; | |
281 | ||
282 | my @aref = @$aref; | |
283 | @aref % 2 == 0 or confess | |
284 | "supplied array ref must have an even number of entries"; | |
285 | ||
286 | warn __PACKAGE__ if $DEBUG; | |
287 | ||
288 | my $survivor; | |
289 | while (my ($id, $test) = splice @aref, 0, 2) { | |
290 | warn $id if $DEBUG; | |
291 | if ($test->(@arg)) { | |
292 | $survivor = $id; | |
293 | last; | |
294 | } | |
295 | } | |
296 | ||
297 | ||
298 | my @id_survivor = (id => $survivor); | |
299 | my $survivor_node = $tree->look_down(@id_survivor); | |
300 | # warn $survivor; | |
301 | # warn $local_root_id; | |
302 | # warn $node; | |
303 | ||
304 | warn "survivor: $survivor" if $DEBUG; | |
305 | warn "tree: " . $tree->as_HTML if $DEBUG; | |
306 | ||
307 | $survivor_node or die "search for @id_survivor failed in tree($tree): " . $tree->as_HTML; | |
308 | ||
309 | my $survivor_node_parent = $survivor_node->parent; | |
310 | $survivor_node = $survivor_node->clone; | |
311 | $survivor_node_parent->replace_content($survivor_node); | |
312 | ||
313 | warn "new tree: " . $tree->as_HTML if $DEBUG; | |
314 | ||
315 | $survivor_node; | |
316 | } | |
317 | ||
318 | ||
319 | sub HTML::Element::highlander2 { | |
320 | my $tree = shift; | |
321 | ||
322 | my %p = validate(@_, { | |
323 | cond => { type => ARRAYREF }, | |
324 | cond_arg => { type => ARRAYREF, | |
325 | default => [] | |
326 | }, | |
327 | debug => { default => 0 } | |
328 | } | |
329 | ); | |
330 | ||
331 | ||
332 | my @cond = @{$p{cond}}; | |
333 | @cond % 2 == 0 or confess | |
334 | "supplied array ref must have an even number of entries"; | |
335 | ||
336 | warn __PACKAGE__ if $p{debug}; | |
337 | ||
338 | my @cond_arg = @{$p{cond_arg}}; | |
339 | ||
340 | my $survivor; my $then; | |
341 | while (my ($id, $if_then) = splice @cond, 0, 2) { | |
342 | ||
343 | warn $id if $p{debug}; | |
344 | my ($if, $_then); | |
345 | ||
346 | if (ref $if_then eq 'ARRAY') { | |
347 | ($if, $_then) = @$if_then; | |
348 | } else { | |
349 | ($if, $_then) = ($if_then, sub {}); | |
350 | } | |
351 | ||
352 | if ($if->(@cond_arg)) { | |
353 | $survivor = $id; | |
354 | $then = $_then; | |
355 | last; | |
356 | } | |
357 | ||
358 | } | |
359 | ||
360 | my @ld = (ref $survivor eq 'ARRAY') | |
361 | ? @$survivor | |
362 | : (id => $survivor) | |
363 | ; | |
364 | ||
365 | warn "survivor: ", $survivor if $p{debug}; | |
366 | warn "survivor_ld: ", Dumper \@ld if $p{debug}; | |
367 | ||
368 | ||
369 | my $survivor_node = $tree->look_down(@ld); | |
370 | ||
371 | $survivor_node or confess | |
372 | "search for @ld failed in tree($tree): " . $tree->as_HTML; | |
373 | ||
374 | my $survivor_node_parent = $survivor_node->parent; | |
375 | $survivor_node = $survivor_node->clone; | |
376 | $survivor_node_parent->replace_content($survivor_node); | |
377 | ||
378 | ||
379 | # **************** NEW FUNCTIONALITY ******************* | |
380 | ||
381 | # apply transforms on survivor node | |
382 | ||
383 | ||
384 | warn "SURV::pre_trans " . $survivor_node->as_HTML if $p{debug}; | |
385 | $then->($survivor_node, @cond_arg); | |
386 | warn "SURV::post_trans " . $survivor_node->as_HTML if $p{debug}; | |
387 | ||
388 | # **************** NEW FUNCTIONALITY ******************* | |
389 | ||
390 | ||
391 | ||
392 | ||
393 | $survivor_node; | |
394 | } | |
395 | ||
396 | ||
397 | sub overwrite_action { | |
398 | my ($mute_node, %X) = @_; | |
399 | ||
400 | $mute_node->attr($X{local_attr}{name} => $X{local_attr}{value}{new}); | |
401 | } | |
402 | ||
403 | ||
404 | sub HTML::Element::overwrite_attr { | |
405 | my $tree = shift; | |
406 | ||
407 | $tree->mute_elem(@_, \&overwrite_action); | |
408 | } | |
409 | ||
410 | ||
411 | ||
412 | sub HTML::Element::mute_elem { | |
413 | my ($tree, $mute_attr, $closures, $post_hook) = @_; | |
414 | ||
415 | warn "my mute_node = $tree->look_down($mute_attr => qr/.*/) ;"; | |
416 | my @mute_node = $tree->look_down($mute_attr => qr/.*/) ; | |
417 | ||
418 | for my $mute_node (@mute_node) { | |
419 | my ($local_attr,$mute_key) = split /\s+/, $mute_node->attr($mute_attr); | |
420 | my $local_attr_value_current = $mute_node->attr($local_attr); | |
421 | my $local_attr_value_new = $closures->{$mute_key}->($tree, $mute_node, $local_attr_value_current); | |
422 | $post_hook->( | |
423 | $mute_node, | |
424 | tree => $tree, | |
425 | local_attr => { | |
426 | name => $local_attr, | |
427 | value => { | |
428 | current => $local_attr_value_current, | |
429 | new => $local_attr_value_new | |
430 | } | |
431 | } | |
432 | ) if ($post_hook) ; | |
433 | } | |
434 | } | |
435 | ||
436 | ||
437 | ||
438 | sub HTML::Element::table { | |
439 | ||
440 | my ($s, %table) = @_; | |
441 | ||
442 | my $table = {}; | |
443 | ||
444 | # use Data::Dumper; warn Dumper \%table; | |
445 | ||
446 | # ++$DEBUG if $table{debug} ; | |
447 | ||
448 | ||
449 | # Get the table element | |
450 | $table->{table_node} = $s->look_down(id => $table{gi_table}); | |
451 | $table->{table_node} or confess | |
452 | "table tag not found via (id => $table{gi_table}"; | |
453 | ||
454 | # Get the prototype tr element(s) | |
455 | my @table_gi_tr = listify $table{gi_tr} ; | |
456 | my @iter_node = map | |
457 | { | |
458 | my $tr = $table->{table_node}->look_down(id => $_); | |
459 | $tr or confess "tr with id => $_ not found"; | |
460 | $tr; | |
461 | } @table_gi_tr; | |
462 | ||
463 | warn "found " . @iter_node . " iter nodes " if $DEBUG; | |
464 | # tie my $iter_node, 'Tie::Cycle', \@iter_node; | |
465 | my $iter_node = List::Rotation::Cycle->new(@iter_node); | |
466 | ||
467 | # warn $iter_node; | |
468 | warn Dumper ($iter_node, \@iter_node) if $DEBUG; | |
469 | ||
470 | # $table->{content} = $table{content}; | |
471 | #$table->{parent} = $table->{table_node}->parent; | |
472 | ||
473 | ||
474 | # $table->{table_node}->detach; | |
475 | # $_->detach for @iter_node; | |
476 | ||
477 | my @table_rows; | |
478 | ||
479 | { | |
480 | my $row = $table{tr_data}->($table, $table{table_data}); | |
481 | last unless defined $row; | |
482 | ||
483 | # get a sample table row and clone it. | |
484 | my $I = $iter_node->next; | |
485 | warn "I: $I" if $DEBUG; | |
486 | my $new_iter_node = $I->clone; | |
487 | ||
488 | ||
489 | $table{td_data}->($new_iter_node, $row); | |
490 | push @table_rows, $new_iter_node; | |
491 | ||
492 | redo; | |
493 | } | |
494 | ||
495 | if (@table_rows) { | |
496 | ||
497 | my $replace_with_elem = $s->look_down(id => shift @table_gi_tr) ; | |
498 | for (@table_gi_tr) { | |
499 | $s->look_down(id => $_)->detach; | |
500 | } | |
501 | ||
502 | $replace_with_elem->replace_with(@table_rows); | |
503 | ||
504 | } | |
505 | ||
506 | } | |
507 | ||
508 | sub ref_or_ld { | |
509 | ||
510 | my ($tree, $slot) = @_; | |
511 | ||
512 | if (ref($slot) eq 'CODE') { | |
513 | $slot->($tree); | |
514 | } else { | |
515 | $tree->look_down(@$slot); | |
516 | } | |
517 | } | |
518 | ||
519 | ||
520 | ||
521 | sub HTML::Element::table2 { | |
522 | ||
523 | my $tree = shift; | |
524 | ||
525 | ||
526 | ||
527 | my %p = validate( | |
528 | @_, { | |
529 | table_ld => { default => ['_tag' => 'table'] }, | |
530 | table_data => 1, | |
531 | table_proc => { default => undef }, | |
532 | ||
533 | tr_ld => { default => ['_tag' => 'tr'] }, | |
534 | tr_data => { default => sub { my ($self, $data) = @_; | |
535 | shift(@{$data}) ; | |
536 | }}, | |
537 | tr_base_id => { default => undef }, | |
538 | tr_proc => { default => sub {} }, | |
539 | td_proc => 1, | |
540 | debug => {default => 0} | |
541 | } | |
542 | ); | |
543 | ||
544 | warn "INPUT TO TABLE2: ", Dumper \@_ if $p{debug}; | |
545 | ||
546 | warn "table_data: " . Dumper $p{table_data} if $p{debug} ; | |
547 | ||
548 | my $table = {}; | |
549 | ||
550 | # use Data::Dumper; warn Dumper \%table; | |
551 | ||
552 | # ++$DEBUG if $table{debug} ; | |
553 | ||
554 | # Get the table element | |
555 | #warn 1; | |
556 | $table->{table_node} = ref_or_ld( $tree, $p{table_ld} ) ; | |
557 | #warn 2; | |
558 | $table->{table_node} or confess | |
559 | "table tag not found via " . Dumper($p{table_ld}) ; | |
560 | ||
561 | warn "table: " . $table->{table_node}->as_HTML if $p{debug}; | |
562 | ||
563 | ||
564 | # Get the prototype tr element(s) | |
565 | my @proto_tr = ref_or_ld( $table->{table_node}, $p{tr_ld} ) ; | |
566 | ||
567 | warn "found " . @proto_tr . " iter nodes " if $p{debug}; | |
568 | ||
569 | @proto_tr or return ; | |
570 | ||
571 | if ($p{debug}) { | |
572 | warn $_->as_HTML for @proto_tr; | |
573 | } | |
574 | my $proto_tr = List::Rotation::Cycle->new(@proto_tr); | |
575 | ||
576 | my $tr_parent = $proto_tr[0]->parent; | |
577 | warn "parent element of trs: " . $tr_parent->as_HTML if $p{debug}; | |
578 | ||
579 | my $row_count; | |
580 | ||
581 | my @table_rows; | |
582 | ||
583 | { | |
584 | my $row = $p{tr_data}->($table, $p{table_data}, $row_count); | |
585 | warn "data row: " . Dumper $row if $p{debug}; | |
586 | last unless defined $row; | |
587 | ||
588 | # wont work: my $new_iter_node = $table->{iter_node}->clone; | |
589 | my $new_tr_node = $proto_tr->next->clone; | |
590 | warn "new_tr_node: $new_tr_node" if $p{debug}; | |
591 | ||
592 | $p{tr_proc}->($tree, $new_tr_node, $row, $p{tr_base_id}, ++$row_count) | |
593 | if defined $p{tr_proc}; | |
594 | ||
595 | warn "data row redux: " . Dumper $row if $p{debug}; | |
596 | #warn 3.3; | |
597 | ||
598 | $p{td_proc}->($new_tr_node, $row); | |
599 | push @table_rows, $new_tr_node; | |
600 | ||
601 | #warn 4.4; | |
602 | ||
603 | redo; | |
604 | } | |
605 | ||
606 | $_->detach for @proto_tr; | |
607 | ||
608 | $tr_parent->push_content(@table_rows) if (@table_rows) ; | |
609 | ||
610 | } | |
611 | ||
612 | ||
613 | sub HTML::Element::unroll_select { | |
614 | ||
615 | my ($s, %select) = @_; | |
616 | ||
617 | my $select = {}; | |
618 | ||
619 | my $select_node = $s->look_down(id => $select{select_label}); | |
620 | ||
621 | my $option = $select_node->look_down('_tag' => 'option'); | |
622 | ||
623 | # warn $option; | |
624 | ||
625 | ||
626 | $option->detach; | |
627 | ||
628 | while (my $row = $select{data_iter}->($select{data})) | |
629 | { | |
630 | # warn Dumper($row); | |
631 | my $o = $option->clone; | |
632 | $o->attr('value', $select{option_value}->($row)); | |
633 | $o->attr('SELECTED', 1) if ($select{option_selected}->($row)) ; | |
634 | ||
635 | $o->replace_content($select{option_content}->($row)); | |
636 | $select_node->push_content($o); | |
637 | } | |
638 | ||
639 | ||
640 | } | |
641 | ||
642 | ||
643 | ||
644 | sub HTML::Element::set_sibling_content { | |
645 | my ($elt, $content) = @_; | |
646 | ||
647 | $elt->parent->splice_content($elt->pindex + 1, 1, $content); | |
648 | ||
649 | } | |
650 | ||
651 | sub HTML::TreeBuilder::parse_string { | |
652 | my ($package, $string) = @_; | |
653 | ||
654 | my $h = HTML::TreeBuilder->new; | |
655 | HTML::TreeBuilder->parse($string); | |
656 | ||
657 | } | |
658 | ||
659 | ||
660 | ||
661 | 1; | |
662 | __END__ | |
663 | # Below is stub documentation for your module. You'd better edit it! | |
664 | ||
665 | =head1 NAME | |
666 | ||
667 | HTML::Element::Library - HTML::Element convenience functions | |
668 | ||
669 | =head1 SYNOPSIS | |
670 | ||
671 | use HTML::Element::Library; | |
672 | use HTML::TreeBuilder; | |
673 | ||
674 | =head1 DESCRIPTION | |
675 | ||
676 | This method provides API calls for common actions on trees when using | |
677 | L<HTML::Tree>. | |
678 | ||
679 | =head1 METHODS | |
680 | ||
681 | The test suite contains examples of each of these methods in a | |
682 | file C<t/$method.t> | |
683 | ||
684 | =head2 Positional Querying Methods | |
685 | ||
686 | =head3 $elem->siblings | |
687 | ||
688 | Return a list of all nodes under the same parent. | |
689 | ||
690 | =head3 $elem->sibdex | |
691 | ||
692 | Return the index of C<$elem> into the array of siblings of which it is | |
693 | a part. L<HTML::ElementSuper> calls this method C<addr> but I don't think | |
694 | that is a descriptive name. And such naming is deceptively close to the | |
695 | C<address> function of C<HTML::Element>. HOWEVER, in the interest of | |
696 | backwards compatibility, both methods are available. | |
697 | ||
698 | =head3 $elem->addr | |
699 | ||
700 | Same as sibdex | |
701 | ||
702 | =head3 $elem->position() | |
703 | ||
704 | Returns the coordinates of this element in the tree it inhabits. | |
705 | This is accomplished by succesively calling addr() on ancestor | |
706 | elements until either a) an element that does not support these | |
707 | methods is found, or b) there are no more parents. The resulting | |
708 | list is the n-dimensional coordinates of the element in the tree. | |
709 | ||
710 | =head2 Element Decoration Methods | |
711 | ||
712 | =head3 HTML::Element::Library::super_literal($text) | |
713 | ||
714 | In L<HTML::Element>, Sean Burke discusses super-literals. They are | |
715 | text which does not get escaped. Great for includng Javascript in | |
716 | HTML. Also great for including foreign language into a document. | |
717 | ||
718 | So, you basically toss C<super_literal> your text and back comes | |
719 | your text wrapped in a C<~literal> element. | |
720 | ||
721 | One of these days, I'll around to writing a nice C<EXPORT> section. | |
722 | ||
723 | =head2 Tree Rewriting Methods | |
724 | ||
725 | =head3 $elem->replace_content(@new_elem) | |
726 | ||
727 | Replaces all of C<$elem>'s content with C<@new_elem>. | |
728 | ||
729 | =head3 $elem->wrap_content($wrapper_element) | |
730 | ||
731 | Wraps the existing content in the provided element. If the provided element | |
732 | happens to be a non-element, a push_content is performed instead. | |
733 | ||
734 | =head3 $elem->set_child_content(@look_down, $content) | |
735 | ||
736 | This method looks down $tree using the criteria specified in @look_down using the the HTML::Element look_down() method. | |
737 | ||
738 | After finding the node, it detaches the node's content and pushes $content as the node's content. | |
739 | ||
740 | =head3 $tree->content_handler($sid_value , $content) | |
741 | ||
742 | This is a convenience method. Because the look_down criteria will often simply be: | |
743 | ||
744 | id => 'fixme' | |
745 | ||
746 | to find things like: | |
747 | ||
748 | <a id=fixme href=http://www.somesite.org>replace_content</a> | |
749 | ||
750 | You can call this method to shorten your typing a bit. You can simply type | |
751 | ||
752 | $elem->content_handler( fixme => 'new text' ) | |
753 | ||
754 | Instead of typing: | |
755 | ||
756 | $elem->set_child_content(sid => 'fixme', 'new text') | |
757 | ||
758 | =head3 $tree->highlander($subtree_span_id, $conditionals, @conditionals_args) | |
759 | ||
760 | This allows for "if-then-else" style processing. Highlander was a movie in | |
761 | which only one would survive. Well, in terms of a tree when looking at a | |
762 | structure that you want to process in C<if-then-else> style, only one child | |
763 | will survive. For example, given this HTML template: | |
764 | ||
765 | <span klass="highlander" id="age_dialog"> | |
766 | <span id="under10"> | |
767 | Hello, does your mother know you're | |
768 | using her AOL account? | |
769 | </span> | |
770 | <span id="under18"> | |
771 | Sorry, you're not old enough to enter | |
772 | (and too dumb to lie about your age) | |
773 | </span> | |
774 | <span id="welcome"> | |
775 | Welcome | |
776 | </span> | |
777 | </span> | |
778 | ||
779 | We only want one child of the C<span> tag with id C<age_dialog> to remain | |
780 | based on the age of the person visiting the page. | |
781 | ||
782 | So, let's setup a call that will prune the subtree as a function of age: | |
783 | ||
784 | sub process_page { | |
785 | my $age = shift; | |
786 | my $tree = HTML::TreeBuilder->new_from_file('t/html/highlander.html'); | |
787 | ||
788 | $tree->highlander | |
789 | (age_dialog => | |
790 | [ | |
791 | under10 => sub { $_[0] < 10} , | |
792 | under18 => sub { $_[0] < 18} , | |
793 | welcome => sub { 1 } | |
794 | ], | |
795 | $age | |
796 | ); | |
797 | ||
798 | And there we have it. If the age is less than 10, then the node with | |
799 | id C<under10> remains. For age less than 18, the node with id C<under18> | |
800 | remains. | |
801 | Otherwise our "else" condition fires and the child with id C<welcome> remains. | |
802 | ||
803 | =head3 $tree->passover($id_of_element) | |
804 | ||
805 | In some cases, you know exactly which element should survive. In this case, | |
806 | you can simply call C<passover> to remove it's siblings. For the HTML | |
807 | above, you could delete C<under10> and C<welcome> by simply calling: | |
808 | ||
809 | $tree->passover('under18'); | |
810 | ||
811 | =head3 $tree->highlander2($tree, $conditionals, @conditionals_args) | |
812 | ||
813 | Right around the same time that C<table2()> came into being, Seamstress | |
814 | began to tackle tougher and tougher processing problems. It became clear that | |
815 | a more powerful highlander was needed... one that not only snipped the tree | |
816 | of the nodes that should not survive, but one that allows for | |
817 | post-processing of the survivor node. And one that was more flexible with | |
818 | how to find the nodes to snip. | |
819 | ||
820 | Thus (drum roll) C<highlander2()>. | |
821 | ||
822 | So let's look at our HTML which requires post-selection processing: | |
823 | ||
824 | <span klass="highlander" id="age_dialog"> | |
825 | <span id="under10"> | |
826 | Hello, little <span id=age>AGE</span>-year old, | |
827 | does your mother know you're using her AOL account? | |
828 | </span> | |
829 | <span id="under18"> | |
830 | Sorry, you're only <span id=age>AGE</span> | |
831 | (and too dumb to lie about your age) | |
832 | </span> | |
833 | <span id="welcome"> | |
834 | Welcome, isn't it good to be <span id=age>AGE</span> years old? | |
835 | </span> | |
836 | </span> | |
837 | ||
838 | In this case, a branch survives, but it has dummy data in it. We must take | |
839 | the surviving segment of HTML and rewrite the age C<span> with the age. | |
840 | Here is how we use C<highlander2()> to do so: | |
841 | ||
842 | sub replace_age { | |
843 | my $branch = shift; | |
844 | my $age = shift; | |
845 | $branch->look_down(id => 'age')->replace_content($age); | |
846 | } | |
847 | ||
848 | my $if_then = $tree->look_down(id => 'age_dialog'); | |
849 | ||
850 | $if_then->highlander2( | |
851 | cond => [ | |
852 | under10 => [ | |
853 | sub { $_[0] < 10} , | |
854 | \&replace_age | |
855 | ], | |
856 | under18 => [ | |
857 | sub { $_[0] < 18} , | |
858 | \&replace_age | |
859 | ], | |
860 | welcome => [ | |
861 | sub { 1 }, | |
862 | \&replace_age | |
863 | ] | |
864 | ], | |
865 | cond_arg => [ $age ] | |
866 | ); | |
867 | ||
868 | We pass it the tree (C<$if_then>), an arrayref of conditions | |
869 | (C<cond>) and an arrayref of arguments which are passed to the | |
870 | C<cond>s and to the replacement subs. | |
871 | ||
872 | The C<under10>, C<under18> and C<welcome> are id attributes in the | |
873 | tree of the siblings of which only one will survive. However, | |
874 | should you need to do | |
875 | more complex look-downs to find the survivor, | |
876 | then supply an array ref instead of a simple | |
877 | scalar: | |
878 | ||
879 | ||
880 | $if_then->highlander2( | |
881 | cond => [ | |
882 | [class => 'r12'] => [ | |
883 | sub { $_[0] < 10} , | |
884 | \&replace_age | |
885 | ], | |
886 | [class => 'z22'] => [ | |
887 | sub { $_[0] < 18} , | |
888 | \&replace_age | |
889 | ], | |
890 | [class => 'w88'] => [ | |
891 | sub { 1 }, | |
892 | \&replace_age | |
893 | ] | |
894 | ], | |
895 | cond_arg => [ $age ] | |
896 | ); | |
897 | ||
898 | ||
899 | =head3 $tree->overwrite_attr($mutation_attr => $mutating_closures) | |
900 | ||
901 | This method is designed for taking a tree and reworking a set of nodes in | |
902 | a stereotyped fashion. For instance let's say you have 3 remote image | |
903 | archives, but you don't want to put long URLs in your img src | |
904 | tags for reasons of abstraction, re-use and brevity. So instead you do this: | |
905 | ||
906 | <img src="/img/smiley-face.jpg" fixup="src lnc"> | |
907 | <img src="/img/hot-babe.jpg" fixup="src playboy"> | |
908 | <img src="/img/footer.jpg" fixup="src foobar"> | |
909 | ||
910 | and then when the tree of HTML is being processed, you make this call: | |
911 | ||
912 | my %closures = ( | |
913 | lnc => sub { my ($tree, $mute_node, $attr_value)= @_; "http://lnc.usc.edu$attr_value" }, | |
914 | playboy => sub { my ($tree, $mute_node, $attr_value)= @_; "http://playboy.com$attr_value" } | |
915 | foobar => sub { my ($tree, $mute_node, $attr_value)= @_; "http://foobar.info$attr_value" } | |
916 | ) | |
917 | ||
918 | $tree->overwrite_attr(fixup => \%closures) ; | |
919 | ||
920 | and the tags come out modified like so: | |
921 | ||
922 | <img src="http://lnc.usc.edu/img/smiley-face.jpg" fixup="src lnc"> | |
923 | <img src="http://playboy.com/img/hot-babe.jpg" fixup="src playboy"> | |
924 | <img src="http://foobar.info/img/footer.jpg" fixup="src foobar"> | |
925 | ||
926 | =head3 $tree->mute_elem($mutation_attr => $mutating_closures, [ $post_hook ] ) | |
927 | ||
928 | This is a generalization of C<overwrite_attr>. C<overwrite_attr> | |
929 | assumes the return value of the | |
930 | closure is supposed overwrite an attribute value and does it for you. | |
931 | C<mute_elem> is a more general function which does nothing but | |
932 | hand the closure the element and let it mutate it as it jolly well pleases :) | |
933 | ||
934 | In fact, here is the implementation of C<overwrite_attr> | |
935 | to give you a taste of how C<mute_attr> is used: | |
936 | ||
937 | sub overwrite_action { | |
938 | my ($mute_node, %X) = @_; | |
939 | ||
940 | $mute_node->attr($X{local_attr}{name} => $X{local_attr}{value}{new}); | |
941 | } | |
942 | ||
943 | ||
944 | sub HTML::Element::overwrite_attr { | |
945 | my $tree = shift; | |
946 | ||
947 | $tree->mute_elem(@_, \&overwrite_action); | |
948 | } | |
949 | ||
950 | ||
951 | ||
952 | ||
953 | =head2 Tree-Building Methods: Unrolling an array via a single sample element (<ul> container) | |
954 | ||
955 | This is best described by example. Given this HTML: | |
956 | ||
957 | <strong>Here are the things I need from the store:</strong> | |
958 | <ul> | |
959 | <li class="store_items">Sample item</li> | |
960 | </ul> | |
961 | ||
962 | We can unroll it like so: | |
963 | ||
964 | my $li = $tree->look_down(class => 'store_items'); | |
965 | ||
966 | my @items = qw(bread butter vodka); | |
967 | ||
968 | $tree->iter($li => @items); | |
969 | ||
970 | To produce this: | |
971 | ||
972 | ||
973 | <html> | |
974 | <head></head> | |
975 | <body>Here are the things I need from the store: | |
976 | <ul> | |
977 | <li class="store_items">bread</li> | |
978 | <li class="store_items">butter</li> | |
979 | <li class="store_items">vodka</li> | |
980 | </ul> | |
981 | </body> | |
982 | </html> | |
983 | ||
984 | =head2 Tree-Building Methods: Unrolling an array via n sample elements (<dl> container) | |
985 | ||
986 | C<iter()> was fine for awhile, but some things | |
987 | (e.g. definition lists) need a more general function to make them easy to | |
988 | do. Hence C<iter2()>. This function will be explained by example of unrolling | |
989 | a simple definition list. | |
990 | ||
991 | So here's our mock-up HTML from the designer: | |
992 | ||
993 | <dl class="dual_iter" id="service_plan"> | |
994 | <dt> | |
995 | Artist | |
996 | </dt> | |
997 | <dd> | |
998 | A person who draws blood. | |
999 | </dd> | |
1000 | ||
1001 | <dt> | |
1002 | Musician | |
1003 | </dt> | |
1004 | <dd> | |
1005 | A clone of Iggy Pop. | |
1006 | </dd> | |
1007 | ||
1008 | <dt> | |
1009 | Poet | |
1010 | </dt> | |
1011 | <dd> | |
1012 | A relative of Edgar Allan Poe. | |
1013 | </dd> | |
1014 | ||
1015 | <dt class="adstyle">sample header</dt> | |
1016 | <dd class="adstyle2">sample data</dd> | |
1017 | ||
1018 | </dl> | |
1019 | ||
1020 | ||
1021 | And we want to unroll our data set: | |
1022 | ||
1023 | my @items = ( | |
1024 | ['the pros' => 'never have to worry about service again'], | |
1025 | ['the cons' => 'upfront extra charge on purchase'], | |
1026 | ['our choice' => 'go with the extended service plan'] | |
1027 | ); | |
1028 | ||
1029 | ||
1030 | Now, let's make this problem a bit harder to show off the power of C<iter2()>. | |
1031 | Let's assume that we want only the last <dt> and it's accompanying <dd> | |
1032 | (the one with "sample data") to be used as the sample data | |
1033 | for unrolling with our data set. Let's further assume that we want them to | |
1034 | remain in the final output. | |
1035 | ||
1036 | So now, the API to C<iter2()> will be discussed and we will explain how our | |
1037 | goal of getting our data into HTML fits into the API. | |
1038 | ||
1039 | =over 4 | |
1040 | ||
1041 | =item * wrapper_ld | |
1042 | ||
1043 | This is how to look down and find the container of all the elements we will | |
1044 | be unrolling. The <dl> tag is the container for the dt and dd tags we will be | |
1045 | unrolling. | |
1046 | ||
1047 | If you pass an anonymous subroutine, then it is presumed that execution of | |
1048 | this subroutine will return the HTML::Element representing the container tag. | |
1049 | If you pass an array ref, then this will be dereferenced and passed to | |
1050 | C<HTML::Element::look_down()>. | |
1051 | ||
1052 | default value: C<< ['_tag' => 'dl'] >> | |
1053 | ||
1054 | Based on the mock HTML above, this default is fine for finding our container | |
1055 | tag. So let's move on. | |
1056 | ||
1057 | =item * wrapper_data | |
1058 | ||
1059 | This is an array reference of data that we will be putting into the container. | |
1060 | You must supply this. C<@items> above is our C<wrapper_data>. | |
1061 | ||
1062 | =item * wrapper_proc | |
1063 | ||
1064 | After we find the container via C<wrapper_ld>, we may want to pre-process | |
1065 | some aspect of this tree. In our case the first two sets of dt and dd need | |
1066 | to be removed, leaving the last dt and dd. So, we supply a C<wrapper_proc> | |
1067 | which will do this. | |
1068 | ||
1069 | default: undef | |
1070 | ||
1071 | =item * item_ld | |
1072 | ||
1073 | This anonymous subroutine returns an array ref of C<HTML::Element>s that will | |
1074 | be cloned and populated with item data | |
1075 | (item data is a "row" of C<wrapper_data>). | |
1076 | ||
1077 | default: returns an arrayref consisting of the dt and dd element inside the | |
1078 | container. | |
1079 | ||
1080 | =item * item_data | |
1081 | ||
1082 | This is a subroutine that takes C<wrapper_data> and retrieves one "row" | |
1083 | to be "pasted" into the array ref of C<HTML::Element>s found via C<item_ld>. | |
1084 | I hope that makes sense. | |
1085 | ||
1086 | default: shifts C<wrapper_data>. | |
1087 | ||
1088 | =item * item_proc | |
1089 | ||
1090 | This is a subroutine that takes the C<item_data> and the C<HTML::Element>s | |
1091 | found via C<item_ld> and produces an arrayref of C<HTML::Element>s which will | |
1092 | eventually be spliced into the container. | |
1093 | ||
1094 | Note that this subroutine MUST return the new items. This is done | |
1095 | So that more items than were passed in can be returned. This is | |
1096 | useful when, for example, you must return 2 dts for an input data item. | |
1097 | And when would you do this? When a single term has multiple spellings | |
1098 | for instance. | |
1099 | ||
1100 | default: expects C<item_data> to be an arrayref of two elements and | |
1101 | C<item_elems> to be an arrayref of two C<HTML::Element>s. It replaces the | |
1102 | content of the C<HTML::Element>s with the C<item_data>. | |
1103 | ||
1104 | =item * splice | |
1105 | ||
1106 | After building up an array of C<@item_elems>, the subroutine passed as | |
1107 | C<splice> will be given the parent container HTML::Element and the | |
1108 | C<@item_elems>. How the C<@item_elems> end up in the container is up to this | |
1109 | routine: it could put half of them in. It could unshift them or whatever. | |
1110 | ||
1111 | default: C<< $container->splice_content(0, 2, @item_elems) >> | |
1112 | In other words, kill the 2 sample elements with the newly generated | |
1113 | @item_elems | |
1114 | ||
1115 | =back | |
1116 | ||
1117 | So now that we have documented the API, let's see the call we need: | |
1118 | ||
1119 | $tree->iter2( | |
1120 | # default wrapper_ld ok. | |
1121 | wrapper_data => \@items, | |
1122 | wrapper_proc => sub { | |
1123 | my ($container) = @_; | |
1124 | ||
1125 | # only keep the last 2 dts and dds | |
1126 | my @content_list = $container->content_list; | |
1127 | $container->splice_content(0, @content_list - 2); | |
1128 | }, | |
1129 | ||
1130 | # default item_ld is fine. | |
1131 | # default item_data is fine. | |
1132 | # default item_proc is fine. | |
1133 | splice => sub { | |
1134 | my ($container, @item_elems) = @_; | |
1135 | $container->unshift_content(@item_elems); | |
1136 | }, | |
1137 | debug => 1, | |
1138 | ); | |
1139 | ||
1140 | ||
1141 | =head2 Tree-Building Methods: Select Unrolling | |
1142 | ||
1143 | The C<unroll_select> method has this API: | |
1144 | ||
1145 | $tree->unroll_select( | |
1146 | select_label => $id_label, | |
1147 | option_value => $closure, # how to get option value from data row | |
1148 | option_content => $closure, # how to get option content from data row | |
1149 | option_selected => $closure, # boolean to decide if SELECTED | |
1150 | data => $data # the data to be put into the SELECT | |
1151 | data_iter => $closure # the thing that will get a row of data | |
1152 | ); | |
1153 | ||
1154 | Here's an example: | |
1155 | ||
1156 | $tree->unroll_select( | |
1157 | select_label => 'clan_list', | |
1158 | option_value => sub { my $row = shift; $row->clan_id }, | |
1159 | option_content => sub { my $row = shift; $row->clan_name }, | |
1160 | option_selected => sub { my $row = shift; $row->selected }, | |
1161 | data => \@query_results, | |
1162 | data_iter => sub { my $data = shift; $data->next } | |
1163 | ) | |
1164 | ||
1165 | ||
1166 | ||
1167 | =head2 Tree-Building Methods: Table Generation | |
1168 | ||
1169 | Matthew Sisk has a much more intuitive (imperative) | |
1170 | way to generate tables via his module | |
1171 | L<HTML::ElementTable|HTML::ElementTable>. | |
1172 | However, for those with callback fever, the following | |
1173 | method is available. First, we look at a nuts and bolts way to build a table | |
1174 | using only standard L<HTML::Tree> API calls. Then the C<table> method | |
1175 | available here is discussed. | |
1176 | ||
1177 | =head3 Sample Model | |
1178 | ||
1179 | package Simple::Class; | |
1180 | ||
1181 | use Set::Array; | |
1182 | ||
1183 | my @name = qw(bob bill brian babette bobo bix); | |
1184 | my @age = qw(99 12 44 52 12 43); | |
1185 | my @weight = qw(99 52 80 124 120 230); | |
1186 | ||
1187 | ||
1188 | sub new { | |
1189 | my $this = shift; | |
1190 | bless {}, ref($this) || $this; | |
1191 | } | |
1192 | ||
1193 | sub load_data { | |
1194 | my @data; | |
1195 | ||
1196 | for (0 .. 5) { | |
1197 | push @data, { | |
1198 | age => $age[rand $#age] + int rand 20, | |
1199 | name => shift @name, | |
1200 | weight => $weight[rand $#weight] + int rand 40 | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | Set::Array->new(@data); | |
1205 | } | |
1206 | ||
1207 | ||
1208 | 1; | |
1209 | ||
1210 | ||
1211 | =head4 Sample Usage: | |
1212 | ||
1213 | my $data = Simple::Class->load_data; | |
1214 | ++$_->{age} for @$data | |
1215 | ||
1216 | =head3 Inline Code to Unroll a Table | |
1217 | ||
1218 | =head4 HTML | |
1219 | ||
1220 | <html> | |
1221 | ||
1222 | <table id="load_data"> | |
1223 | ||
1224 | <tr> <th>name</th><th>age</th><th>weight</th> </tr> | |
1225 | ||
1226 | <tr id="iterate"> | |
1227 | ||
1228 | <td id="name"> NATURE BOY RIC FLAIR </td> | |
1229 | <td id="age"> 35 </td> | |
1230 | <td id="weight"> 220 </td> | |
1231 | ||
1232 | </tr> | |
1233 | ||
1234 | </table> | |
1235 | ||
1236 | </html> | |
1237 | ||
1238 | ||
1239 | =head4 The manual way (*NOT* recommended) | |
1240 | ||
1241 | require 'simple-class.pl'; | |
1242 | use HTML::Seamstress; | |
1243 | ||
1244 | # load the view | |
1245 | my $seamstress = HTML::Seamstress->new_from_file('simple.html'); | |
1246 | ||
1247 | # load the model | |
1248 | my $o = Simple::Class->new; | |
1249 | my $data = $o->load_data; | |
1250 | ||
1251 | # find the <table> and <tr> | |
1252 | my $table_node = $seamstress->look_down('id', 'load_data'); | |
1253 | my $iter_node = $table_node->look_down('id', 'iterate'); | |
1254 | my $table_parent = $table_node->parent; | |
1255 | ||
1256 | ||
1257 | # drop the sample <table> and <tr> from the HTML | |
1258 | # only add them in if there is data in the model | |
1259 | # this is achieved via the $add_table flag | |
1260 | ||
1261 | $table_node->detach; | |
1262 | $iter_node->detach; | |
1263 | my $add_table; | |
1264 | ||
1265 | # Get a row of model data | |
1266 | while (my $row = shift @$data) { | |
1267 | ||
1268 | # We got row data. Set the flag indicating ok to hook the table into the HTML | |
1269 | ++$add_table; | |
1270 | ||
1271 | # clone the sample <tr> | |
1272 | my $new_iter_node = $iter_node->clone; | |
1273 | ||
1274 | # find the tags labeled name age and weight and | |
1275 | # set their content to the row data | |
1276 | $new_iter_node->content_handler($_ => $row->{$_}) | |
1277 | for qw(name age weight); | |
1278 | ||
1279 | $table_node->push_content($new_iter_node); | |
1280 | ||
1281 | } | |
1282 | ||
1283 | # reattach the table to the HTML tree if we loaded data into some table rows | |
1284 | ||
1285 | $table_parent->push_content($table_node) if $add_table; | |
1286 | ||
1287 | print $seamstress->as_HTML; | |
1288 | ||
1289 | ||
1290 | ||
1291 | =head3 $tree->table() : API call to Unroll a Table | |
1292 | ||
1293 | require 'simple-class.pl'; | |
1294 | use HTML::Seamstress; | |
1295 | ||
1296 | # load the view | |
1297 | my $seamstress = HTML::Seamstress->new_from_file('simple.html'); | |
1298 | # load the model | |
1299 | my $o = Simple::Class->new; | |
1300 | ||
1301 | $seamstress->table | |
1302 | ( | |
1303 | # tell seamstress where to find the table, via the method call | |
1304 | # ->look_down('id', $gi_table). Seamstress detaches the table from the | |
1305 | # HTML tree automatically if no table rows can be built | |
1306 | ||
1307 | gi_table => 'load_data', | |
1308 | ||
1309 | # tell seamstress where to find the tr. This is a bit useless as | |
1310 | # the <tr> usually can be found as the first child of the parent | |
1311 | ||
1312 | gi_tr => 'iterate', | |
1313 | ||
1314 | # the model data to be pushed into the table | |
1315 | ||
1316 | table_data => $o->load_data, | |
1317 | ||
1318 | # the way to take the model data and obtain one row | |
1319 | # if the table data were a hashref, we would do: | |
1320 | # my $key = (keys %$data)[0]; my $val = $data->{$key}; delete $data->{$key} | |
1321 | ||
1322 | tr_data => sub { my ($self, $data) = @_; | |
1323 | shift(@{$data}) ; | |
1324 | }, | |
1325 | ||
1326 | # the way to take a row of data and fill the <td> tags | |
1327 | ||
1328 | td_data => sub { my ($tr_node, $tr_data) = @_; | |
1329 | $tr_node->content_handler($_ => $tr_data->{$_}) | |
1330 | for qw(name age weight) } | |
1331 | ||
1332 | ); | |
1333 | ||
1334 | ||
1335 | print $seamstress->as_HTML; | |
1336 | ||
1337 | ||
1338 | ||
1339 | =head4 Looping over Multiple Sample Rows | |
1340 | ||
1341 | * HTML | |
1342 | ||
1343 | <html> | |
1344 | ||
1345 | <table id="load_data" CELLPADDING=8 BORDER=2> | |
1346 | ||
1347 | <tr> <th>name</th><th>age</th><th>weight</th> </tr> | |
1348 | ||
1349 | <tr id="iterate1" BGCOLOR="white" > | |
1350 | ||
1351 | <td id="name"> NATURE BOY RIC FLAIR </td> | |
1352 | <td id="age"> 35 </td> | |
1353 | <td id="weight"> 220 </td> | |
1354 | ||
1355 | </tr> | |
1356 | <tr id="iterate2" BGCOLOR="#CCCC99"> | |
1357 | ||
1358 | <td id="name"> NATURE BOY RIC FLAIR </td> | |
1359 | <td id="age"> 35 </td> | |
1360 | <td id="weight"> 220 </td> | |
1361 | ||
1362 | </tr> | |
1363 | ||
1364 | </table> | |
1365 | ||
1366 | </html> | |
1367 | ||
1368 | ||
1369 | * Only one change to last API call. | |
1370 | ||
1371 | This: | |
1372 | ||
1373 | gi_tr => 'iterate', | |
1374 | ||
1375 | becomes this: | |
1376 | ||
1377 | gi_tr => ['iterate1', 'iterate2'] | |
1378 | ||
1379 | =head3 $tree->table2() : New API Call to Unroll a Table | |
1380 | ||
1381 | After 2 or 3 years with C<table()>, I began to develop | |
1382 | production websites with it and decided it needed a cleaner | |
1383 | interface, particularly in the area of handling the fact that | |
1384 | C<id> tags will be the same after cloning a table row. | |
1385 | ||
1386 | First, I will give a dry listing of the function's argument parameters. | |
1387 | This will not be educational most likely. A better way to understand how | |
1388 | to use the function is to read through the incremental unrolling of the | |
1389 | function's interface given in conversational style after the dry listing. | |
1390 | But take your pick. It's the same information given in two different | |
1391 | ways. | |
1392 | ||
1393 | =head4 Dry/technical parameter documentation | |
1394 | ||
1395 | C<< $tree->table2(%param) >> takes the following arguments: | |
1396 | ||
1397 | =over | |
1398 | ||
1399 | =item * C<< table_ld => $look_down >> : optional | |
1400 | ||
1401 | How to find the C<table> element in C<$tree>. If C<$look_down> is an | |
1402 | arrayref, then use C<look_down>. If it is a CODE ref, then call it, | |
1403 | passing it C<$tree>. | |
1404 | ||
1405 | Defaults to C<< ['_tag' => 'table'] >> if not passed in. | |
1406 | ||
1407 | =item * C<< table_data => $tabular_data >> : required | |
1408 | ||
1409 | The data to fill the table with. I<Must> be passed in. | |
1410 | ||
1411 | =item * C<< table_proc => $code_ref >> : not implemented | |
1412 | ||
1413 | A subroutine to do something to the table once it is found. | |
1414 | Not currently implemented. Not obviously necessary. Just | |
1415 | created because there is a C<tr_proc> and C<td_proc>. | |
1416 | ||
1417 | =item * C<< tr_ld => $look_down >> : optional | |
1418 | ||
1419 | Same as C<table_ld> but for finding the table row elements. Please note | |
1420 | that the C<tr_ld> is done on the table node that was found I<instead> | |
1421 | of the whole HTML tree. This makes sense. The C<tr>s that you want exist | |
1422 | below the table that was just found. | |
1423 | ||
1424 | Defaults to C<< ['_tag' => 'tr'] >> if not passed in. | |
1425 | ||
1426 | =item * C<< tr_data => $code_ref >> : optional | |
1427 | ||
1428 | How to take the C<table_data> and return a row. Defaults to: | |
1429 | ||
1430 | sub { my ($self, $data) = @_; | |
1431 | shift(@{$data}) ; | |
1432 | } | |
1433 | ||
1434 | =item * C<< tr_proc => $code_ref >> : optional | |
1435 | ||
1436 | Something to do to the table row we are about to add to the | |
1437 | table we are making. Defaults to a routine which makes the C<id> | |
1438 | attribute unique: | |
1439 | ||
1440 | sub { | |
1441 | my ($self, $tr, $tr_data, $tr_base_id, $row_count) = @_; | |
1442 | $tr->attr(id => sprintf "%s_%d", $tr_base_id, $row_count); | |
1443 | } | |
1444 | ||
1445 | =item * C<< td_proc => $code_ref >> : required | |
1446 | ||
1447 | This coderef will take the row of data and operate on the C<td> cells that | |
1448 | are children of the C<tr>. See C<t/table2.t> for several usage examples. | |
1449 | ||
1450 | Here's a sample one: | |
1451 | ||
1452 | sub { | |
1453 | my ($tr, $data) = @_; | |
1454 | my @td = $tr->look_down('_tag' => 'td'); | |
1455 | for my $i (0..$#td) { | |
1456 | $td[$i]->splice_content(0, 1, $data->[$i]); | |
1457 | } | |
1458 | } | |
1459 | ||
1460 | =cut | |
1461 | ||
1462 | =head4 Conversational parameter documentation | |
1463 | ||
1464 | The first thing you need is a table. So we need a look down for that. If you | |
1465 | don't give one, it defaults to | |
1466 | ||
1467 | ['_tag' => 'table'] | |
1468 | ||
1469 | What good is a table to display in without data to display?! | |
1470 | So you must supply a scalar representing your tabular | |
1471 | data source. This scalar might be an array reference, a C<next>able iterator, | |
1472 | a DBI statement handle. Whatever it is, it can be iterated through to build | |
1473 | up rows of table data. | |
1474 | These two required fields (the way to find the table and the data to | |
1475 | display in the table) are C<table_ld> and C<table_data> | |
1476 | respectively. A little more on C<table_ld>. If this happens to be a CODE ref, | |
1477 | then execution | |
1478 | of the code ref is presumed to return the C<HTML::Element> | |
1479 | representing the table in the HTML tree. | |
1480 | ||
1481 | Next, we get the row or rows which serve as sample C<tr> elements by doing | |
1482 | a C<look_down> from the C<table_elem>. While normally one sample row | |
1483 | is enough to unroll a table, consider when you have alternating | |
1484 | table rows. This API call would need one of each row so that it can | |
1485 | cycle through the | |
1486 | sample rows as it loops through the data. | |
1487 | Alternatively, you could always just use one row and | |
1488 | make the necessary changes to the single C<tr> row by | |
1489 | mutating the element in C<tr_proc>, | |
1490 | discussed below. The default C<tr_ld> is | |
1491 | C<< ['_tag' => 'tr'] >> but you can overwrite it. Note well, if you overwrite | |
1492 | it with a subroutine, then it is expected that the subroutine will return | |
1493 | the C<HTML::Element>(s) | |
1494 | which are C<tr> element(s). | |
1495 | The reason a subroutine might be preferred is in the case | |
1496 | that the HTML designers gave you 8 sample C<tr> rows but only one | |
1497 | prototype row is needed. | |
1498 | So you can write a subroutine, to splice out the 7 rows you don't need | |
1499 | and leave the one sample | |
1500 | row remaining so that this API call can clone it and supply it to | |
1501 | the C<tr_proc> and C<td_proc> calls. | |
1502 | ||
1503 | Now, as we move through the table rows with table data, | |
1504 | we need to do two different things on | |
1505 | each table row: | |
1506 | ||
1507 | =over 4 | |
1508 | ||
1509 | =item * get one row of data from the C<table_data> via C<tr_data> | |
1510 | ||
1511 | The default procedure assumes the C<table_data> is an array reference and | |
1512 | shifts a row off of it: | |
1513 | ||
1514 | sub { my ($self, $data) = @_; | |
1515 | shift(@{$data}) ; | |
1516 | } | |
1517 | ||
1518 | Your function MUST return undef when there is no more rows to lay out. | |
1519 | ||
1520 | =item * take the C<tr> element and mutate it via C<tr_proc> | |
1521 | ||
1522 | The default procedure simply makes the id of the table row unique: | |
1523 | ||
1524 | sub { my ($self, $tr, $tr_data, $row_count, $root_id) = @_; | |
1525 | $tr->attr(id => sprintf "%s_%d", $root_id, $row_count); | |
1526 | } | |
1527 | ||
1528 | =back | |
1529 | ||
1530 | Now that we have our row of data, we call C<td_proc> so that it can | |
1531 | take the data and the C<td> cells in this C<tr> and process them. | |
1532 | This function I<must> be supplied. | |
1533 | ||
1534 | ||
1535 | =head3 Whither a Table with No Rows | |
1536 | ||
1537 | Often when a table has no rows, we want to display a message | |
1538 | indicating this to the view. Use conditional processing to decide what | |
1539 | to display: | |
1540 | ||
1541 | <span id=no_data> | |
1542 | <table><tr><td>No Data is Good Data</td></tr></table> | |
1543 | </span> | |
1544 | <span id=load_data> | |
1545 | <html> | |
1546 | ||
1547 | <table id="load_data"> | |
1548 | ||
1549 | <tr> <th>name</th><th>age</th><th>weight</th> </tr> | |
1550 | ||
1551 | <tr id="iterate"> | |
1552 | ||
1553 | <td id="name"> NATURE BOY RIC FLAIR </td> | |
1554 | <td id="age"> 35 </td> | |
1555 | <td id="weight"> 220 </td> | |
1556 | ||
1557 | </tr> | |
1558 | ||
1559 | </table> | |
1560 | ||
1561 | </html> | |
1562 | ||
1563 | </span> | |
1564 | ||
1565 | ||
1566 | ||
1567 | ||
1568 | =head1 SEE ALSO | |
1569 | ||
1570 | =over | |
1571 | ||
1572 | =item * L<HTML::Tree> | |
1573 | ||
1574 | A perl package for creating and manipulating HTML trees | |
1575 | ||
1576 | =item * L<HTML::ElementTable> | |
1577 | ||
1578 | An L<HTML::Tree> - based module which allows for manipulation of HTML | |
1579 | trees using cartesian coordinations. | |
1580 | ||
1581 | =item * L<HTML::Seamstress> | |
1582 | ||
1583 | An L<HTML::Tree> - based module inspired by | |
1584 | XMLC (L<http://xmlc.enhydra.org>), allowing for dynamic | |
1585 | HTML generation via tree rewriting. | |
1586 | ||
1587 | =head1 TODO | |
1588 | ||
1589 | =over | |
1590 | ||
1591 | =item * highlander2 | |
1592 | ||
1593 | currently the API expects the subtrees to survive or be pruned to be | |
1594 | identified by id: | |
1595 | ||
1596 | $if_then->highlander2([ | |
1597 | under10 => sub { $_[0] < 10} , | |
1598 | under18 => sub { $_[0] < 18} , | |
1599 | welcome => [ | |
1600 | sub { 1 }, | |
1601 | sub { | |
1602 | my $branch = shift; | |
1603 | $branch->look_down(id => 'age')->replace_content($age); | |
1604 | } | |
1605 | ] | |
1606 | ], | |
1607 | $age | |
1608 | ); | |
1609 | ||
1610 | but, it should be more flexible. the C<under10>, and C<under18> are | |
1611 | expected to be ids in the tree... but it is not hard to have a check to | |
1612 | see if this field is an array reference and if it, then to do a look | |
1613 | down instead: | |
1614 | ||
1615 | $if_then->highlander2([ | |
1616 | [class => 'under10'] => sub { $_[0] < 10} , | |
1617 | [class => 'under18'] => sub { $_[0] < 18} , | |
1618 | [class => 'welcome'] => [ | |
1619 | sub { 1 }, | |
1620 | sub { | |
1621 | my $branch = shift; | |
1622 | $branch->look_down(id => 'age')->replace_content($age); | |
1623 | } | |
1624 | ] | |
1625 | ], | |
1626 | $age | |
1627 | ); | |
1628 | ||
1629 | ||
1630 | ||
1631 | =cut | |
1632 | ||
1633 | =head1 SEE ALSO | |
1634 | ||
1635 | L<HTML::Seamstress> | |
1636 | ||
1637 | =head1 AUTHOR | |
1638 | ||
1639 | Terrence Brannon, E<lt>tbone@cpan.orgE<gt> | |
1640 | ||
1641 | Many thanks to BARBIE for his RT bug report. | |
1642 | ||
1643 | =head1 COPYRIGHT AND LICENSE | |
1644 | ||
1645 | Copyright (C) 2004 by Terrence Brannon | |
1646 | ||
1647 | This library is free software; you can redistribute it and/or modify | |
1648 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | |
1649 | at your option, any later version of Perl 5 you may have available. | |
1650 | ||
1651 | ||
1652 | =cut |