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