]>
iEval git - html-element-library.git/blob - lib/HTML/Element/Library.pm
1 package HTML
::Element
::Library
;
5 our $VERSION = '5.210000';
8 use Array
::Group
':all';
11 use Data
::Rmap
'rmap_array';
14 use List
::MoreUtils
':all';
15 use List
::Rotation
::Cycle
;
16 use List
::Util
'first';
17 use Params
::Validate
':all';
20 # https://rt.cpan.org/Ticket/Display.html?id=44105
21 sub HTML
::Element
::fillinform
{
22 my ($tree, $hashref, $return_tree, $guts) = @_;
23 (ref $hashref) eq 'HASH' or confess
'hashref not supplied as argument' ;
25 my $html = $tree->as_HTML;
26 my $new_html = HTML
::FillInForm
->fill(\
$html, $hashref);
29 $tree = HTML
::TreeBuilder
->new_from_content($new_html);
30 $tree = $guts ?
$tree->guts : $tree ;
36 sub HTML
::Element
::siblings
{
38 my $p = $element->parent;
43 sub HTML
::Element
::defmap
{
44 my($tree, $attr, $hashref, $debug) = @_;
46 while (my ($k, $v) = (each %$hashref)) {
47 warn "defmap looks for ($attr => $k)" if $debug;
48 my $found = $tree->look_down($attr => $k);
50 warn "($attr => $k) was found.. replacing with '$v'" if $debug;
51 $found->replace_content( $v );
56 sub HTML
::Element
::_only_empty_content
{
58 my @c = $self->content_list;
59 my $length = scalar @c;
61 scalar @c == 1 and not length $c[0];
64 sub HTML
::Element
::prune
{
67 for my $c ($self->content_list) {
73 $self->delete if ($self->is_empty or $self->_only_empty_content);
77 sub HTML
::Element
::newchild
{
78 my ($lol, $parent_label, @newchild) = @_;
80 if ($_->[0] eq $parent_label) {
81 $_ = [ $parent_label => @newchild ];
89 sub HTML
::Element
::crunch
{ ## no critic (RequireArgUnpacking)
90 my $container = shift;
92 my %p = validate
(@_, {
93 look_down
=> { type
=> ARRAYREF
},
94 leave
=> { default => 1 },
97 my @look_down = @
{$p{look_down
}} ;
98 my @elem = $container->look_down(@look_down) ;
102 for my $elem (@elem) {
103 $elem->detach if $detached++ >= $p{leave
};
107 sub HTML
::Element
::hash_map
{ ## no critic (RequireArgUnpacking)
108 my $container = shift;
110 my %p = validate
(@_, {
111 hash
=> { type
=> HASHREF
},
113 excluding
=> { type
=> ARRAYREF
, default => [] },
114 debug
=> { default => 0 },
117 warn 'The container tag is ', $container->tag if $p{debug
} ;
118 warn 'hash' . Dumper
($p{hash
}) if $p{debug
} ;
119 #warn 'at_under' . Dumper(\@_) if $p{debug} ;
121 my @same_as = $container->look_down( $p{to_attr
} => qr/.+/s ) ;
123 warn 'Found ' . scalar(@same_as) . ' nodes' if $p{debug
} ;
125 for my $same_as (@same_as) {
126 my $attr_val = $same_as->attr($p{to_attr
}) ;
127 if (first
{ $attr_val eq $_ } @
{$p{excluding
}}) {
128 warn "excluding $attr_val" if $p{debug
} ;
131 warn "processing $attr_val" if $p{debug
} ;
132 $same_as->replace_content($p{hash
}->{$attr_val});
136 sub HTML
::Element
::hashmap
{
137 my ($container, $attr_name, $hashref, $excluding, $debug) = @_;
141 $container->hash_map(
143 to_attr
=> $attr_name,
144 excluding
=> $excluding,
149 sub HTML
::Element
::passover
{
150 my ($tree, @to_preserve) = @_;
152 warn "ARGS: my ($tree, @to_preserve)" if $DEBUG;
153 warn $tree->as_HTML(undef, ' ') if $DEBUG;
155 my $exodus = $tree->look_down(id
=> $to_preserve[0]);
157 warn "E: $exodus" if $DEBUG;
159 my @s = HTML
::Element
::siblings
($exodus);
163 $s->delete unless first
{ $s->attr('id') eq $_ } @to_preserve;
166 return $exodus; # Goodbye Egypt! http://en.wikipedia.org/wiki/Passover
169 sub HTML
::Element
::sibdex
{
171 firstidx
{ $_ eq $element } $element->siblings
174 sub HTML
::Element
::addr
{ goto &HTML
::Element
::sibdex
}
176 sub HTML
::Element
::replace_content
{
178 $elem->delete_content;
179 $elem->push_content(@_);
182 sub HTML
::Element
::wrap_content
{
183 my($self, $wrap) = @_;
184 my $content = $self->content;
186 $wrap->push_content(@
$content);
190 $self->push_content($wrap);
195 sub HTML
::Element
::Library
::super_literal
{
197 HTML
::Element
->new('~literal', text
=> $text);
200 sub HTML
::Element
::position
{
201 # Report coordinates by chasing addr's up the
202 # HTML::ElementSuper tree. We know we've reached
203 # the top when a) there is no parent, or b) the
204 # parent is some HTML::Element unable to report
210 unshift @pos, $a if defined $a;
216 sub HTML
::Element
::content_handler
{
217 my ($tree, %content_hash) = @_;
219 for my $k (keys %content_hash) {
220 $tree->set_child_content(id
=> $k, $content_hash{$k});
224 sub HTML
::Element
::assign
{ goto &HTML
::Element
::content_handler
}
233 sub HTML
::Element
::iter
{
234 my ($tree, $p, @data) = @_;
236 # warn 'P: ' , $p->attr('id') ;
237 # warn 'H: ' , $p->as_HTML;
239 # my $id_incr = make_counter;
241 my $new_item = clone
$p;
242 $new_item->replace_content($_);
246 $p->replace_with(@item);
249 sub HTML
::Element
::iter2
{ ## no critic (RequireArgUnpacking)
252 #warn "INPUT TO TABLE2: ", Dumper \@_;
256 wrapper_ld
=> { default => ['_tag' => 'dl'] },
258 wrapper_proc
=> { default => undef },
263 $tr->look_down('_tag' => 'dt'),
264 $tr->look_down('_tag' => 'dd')
269 my ($wrapper_data) = @_;
270 shift @
{$wrapper_data};
274 my ($item_elems, $item_data, $row_count) = @_;
275 $item_elems->[$_]->replace_content($item_data->[$_]) for (0,1) ;
280 my ($container, @item_elems) = @_;
281 $container->splice_content(0, 2, @item_elems);
284 debug
=> {default => 0}
288 warn 'wrapper_data: ' . Dumper
$p{wrapper_data
} if $p{debug
} ;
290 my $container = ref_or_ld
($tree, $p{wrapper_ld
});
291 warn 'container: ' . $container if $p{debug
} ;
292 warn 'wrapper_(preproc): ' . $container->as_HTML if $p{debug
} ;
293 $p{wrapper_proc
}->($container) if defined $p{wrapper_proc
} ;
294 warn 'wrapper_(postproc): ' . $container->as_HTML if $p{debug
} ;
296 my $_item_elems = $p{item_ld
}->($container);
301 my $item_data = $p{item_data
}->($p{wrapper_data
});
302 last unless defined $item_data;
304 warn Dumper
('item_data', $item_data) if $p{debug
};
306 my $item_elems = [ map { $_->clone } @
{$_item_elems} ] ;
309 for (@
{$item_elems}) {
310 warn 'ITEM_ELEMS ', $_->as_HTML if $p{debug
};
314 my $new_item_elems = $p{item_proc
}->($item_elems, $item_data, ++$row_count);
317 for (@
{$new_item_elems}) {
318 warn 'NEWITEM_ELEMS ', $_->as_HTML if $p{debug
};
322 push @item_elem, @
{$new_item_elems} ;
325 warn 'pushing ' . @item_elem . ' elems' if $p{debug
} ;
327 $p{splice}->($container, @item_elem);
330 sub HTML
::Element
::dual_iter
{
331 my ($parent, $data) = @_;
333 my ($prototype_a, $prototype_b) = $parent->content_list;
335 # my $id_incr = make_counter;
339 @
$data %2 == 0 or confess
'dataset does not contain an even number of members';
341 my @iterable_data = ngroup
2 => @
$data;
344 my ($new_a, $new_b) = map { clone
$_ } ($prototype_a, $prototype_b) ;
345 $new_a->splice_content(0,1, $_->[0]);
346 $new_b->splice_content(0,1, $_->[1]);
347 #$_->attr('id', $id_incr->($_->attr('id'))) for ($new_a, $new_b) ;
351 $parent->splice_content(0, 2, @item);
354 sub HTML
::Element
::set_child_content
{ ## no critic (RequireArgUnpacking)
359 my $content_tag = $tree->look_down(@look_down);
361 unless ($content_tag) {
362 warn "criteria [@look_down] not found";
366 $content_tag->replace_content($content);
369 sub HTML
::Element
::highlander
{
370 my ($tree, $local_root_id, $aref, @arg) = @_;
372 ref $aref eq 'ARRAY' or confess
'must supply array reference';
375 @aref % 2 == 0 or confess
'supplied array ref must have an even number of entries';
377 warn __PACKAGE__
if $DEBUG;
380 while (my ($id, $test) = splice @aref, 0, 2) {
388 my @id_survivor = (id
=> $survivor);
389 my $survivor_node = $tree->look_down(@id_survivor);
391 # warn $local_root_id;
394 warn "survivor: $survivor" if $DEBUG;
395 warn 'tree: ' . $tree->as_HTML if $DEBUG;
397 $survivor_node or die "search for @id_survivor failed in tree($tree): " . $tree->as_HTML;
399 my $survivor_node_parent = $survivor_node->parent;
400 $survivor_node = $survivor_node->clone;
401 $survivor_node_parent->replace_content($survivor_node);
403 warn 'new tree: ' . $tree->as_HTML if $DEBUG;
408 sub HTML
::Element
::highlander2
{ ## no critic (RequireArgUnpacking)
411 my %p = validate
(@_, {
412 cond
=> { type
=> ARRAYREF
},
417 debug
=> { default => 0 }
420 my @cond = @
{$p{cond
}};
421 @cond % 2 == 0 or confess
'supplied array ref must have an even number of entries';
423 warn __PACKAGE__
if $p{debug
};
425 my @cond_arg = @
{$p{cond_arg
}};
427 my $survivor; my $then;
428 while (my ($id, $if_then) = splice @cond, 0, 2) {
429 warn $id if $p{debug
};
432 if (ref $if_then eq 'ARRAY') {
433 ($if, $_then) = @
$if_then;
435 ($if, $_then) = ($if_then, sub {});
438 if ($if->(@cond_arg)) {
445 my @ld = (ref $survivor eq 'ARRAY') ? @
$survivor : (id
=> $survivor);
447 warn 'survivor: ', $survivor if $p{debug
};
448 warn 'survivor_ld: ', Dumper \
@ld if $p{debug
};
450 my $survivor_node = $tree->look_down(@ld);
452 $survivor_node or confess
"search for @ld failed in tree($tree): " . $tree->as_HTML;
454 my $survivor_node_parent = $survivor_node->parent;
455 $survivor_node = $survivor_node->clone;
456 $survivor_node_parent->replace_content($survivor_node);
458 # **************** NEW FUNCTIONALITY *******************
459 # apply transforms on survivor node
461 warn 'SURV::pre_trans ' . $survivor_node->as_HTML if $p{debug
};
462 $then->($survivor_node, @cond_arg);
463 warn 'SURV::post_trans ' . $survivor_node->as_HTML if $p{debug
};
464 # **************** NEW FUNCTIONALITY *******************
469 sub overwrite_action
{
470 my ($mute_node, %X) = @_;
472 $mute_node->attr($X{local_attr
}{name
} => $X{local_attr
}{value
}{new
});
475 sub HTML
::Element
::overwrite_attr
{
478 $tree->mute_elem(@_, \
&overwrite_action
);
481 sub HTML
::Element
::mute_elem
{
482 my ($tree, $mute_attr, $closures, $post_hook) = @_;
484 my @mute_node = $tree->look_down($mute_attr => qr/.*/s) ;
486 for my $mute_node (@mute_node) {
487 my ($local_attr,$mute_key) = split /\s+/s, $mute_node->attr($mute_attr);
488 my $local_attr_value_current = $mute_node->attr($local_attr);
489 my $local_attr_value_new = $closures->{$mute_key}->($tree, $mute_node, $local_attr_value_current);
496 current
=> $local_attr_value_current,
497 new
=> $local_attr_value_new
506 sub HTML
::Element
::table
{
507 my ($s, %table) = @_;
510 # Get the table element
511 $table->{table_node
} = $s->look_down(id
=> $table{gi_table
});
512 $table->{table_node
} or confess
"table tag not found via (id => $table{gi_table}";
514 # Get the prototype tr element(s)
515 my @table_gi_tr = listify
$table{gi_tr
} ;
516 my @iter_node = map {
517 my $tr = $table->{table_node
}->look_down(id
=> $_);
518 $tr or confess
"tr with id => $_ not found";
522 warn 'found ' . @iter_node . ' iter nodes ' if $DEBUG;
523 my $iter_node = List
::Rotation
::Cycle
->new(@iter_node);
526 warn Dumper
($iter_node, \
@iter_node) if $DEBUG;
528 # $table->{content} = $table{content};
529 # $table->{parent} = $table->{table_node}->parent;
531 # $table->{table_node}->detach;
532 # $_->detach for @iter_node;
537 my $row = $table{tr_data
}->($table, $table{table_data
});
538 last unless defined $row;
540 # get a sample table row and clone it.
541 my $I = $iter_node->next;
542 warn "I: $I" if $DEBUG;
543 my $new_iter_node = $I->clone;
545 $table{td_data
}->($new_iter_node, $row);
546 push @table_rows, $new_iter_node;
550 my $replace_with_elem = $s->look_down(id
=> shift @table_gi_tr) ;
551 $s->look_down(id
=> $_)->detach for @table_gi_tr;
552 $replace_with_elem->replace_with(@table_rows);
557 my ($tree, $slot) = @_;
559 if (ref($slot) eq 'CODE') {
562 $tree->look_down(@
$slot);
566 sub HTML
::Element
::table2
{ ## no critic (RequireArgUnpacking)
571 table_ld
=> { default => ['_tag' => 'table'] },
573 table_proc
=> { default => undef },
574 tr_ld
=> { default => ['_tag' => 'tr'] },
577 my ($self, $data) = @_;
580 tr_base_id
=> { default => undef },
581 tr_proc
=> { default => sub {} },
583 debug
=> {default => 0}
587 warn 'INPUT TO TABLE2: ', Dumper \
@_ if $p{debug
};
588 warn 'table_data: ' . Dumper
$p{table_data
} if $p{debug
} ;
592 # Get the table element
593 $table->{table_node
} = ref_or_ld
( $tree, $p{table_ld
} ) ;
594 $table->{table_node
} or confess
'table tag not found via ' . Dumper
($p{table_ld
}) ;
596 warn 'table: ' . $table->{table_node
}->as_HTML if $p{debug
};
598 # Get the prototype tr element(s)
599 my @proto_tr = ref_or_ld
( $table->{table_node
}, $p{tr_ld
} ) ;
601 warn 'found ' . @proto_tr . ' iter nodes' if $p{debug
};
603 return unless @proto_tr;
606 warn $_->as_HTML for @proto_tr;
608 my $proto_tr = List
::Rotation
::Cycle
->new(@proto_tr);
610 my $tr_parent = $proto_tr[0]->parent;
611 warn 'parent element of trs: ' . $tr_parent->as_HTML if $p{debug
};
618 my $row = $p{tr_data
}->($table, $p{table_data
}, $row_count);
619 warn 'data row: ' . Dumper
$row if $p{debug
};
620 last unless defined $row;
622 # wont work: my $new_iter_node = $table->{iter_node}->clone;
623 my $new_tr_node = $proto_tr->next->clone;
624 warn "new_tr_node: $new_tr_node" if $p{debug
};
626 $p{tr_proc
}->($tree, $new_tr_node, $row, $p{tr_base_id
}, ++$row_count) if defined $p{tr_proc
};
628 warn 'data row redux: ' . Dumper
$row if $p{debug
};
630 $p{td_proc
}->($new_tr_node, $row);
631 push @table_rows, $new_tr_node;
634 $_->detach for @proto_tr;
636 $tr_parent->push_content(@table_rows) if (@table_rows) ;
639 sub HTML
::Element
::unroll_select
{
640 my ($s, %select) = @_;
643 warn 'Select Hash: ' . Dumper
(\
%select) if $select{debug
};
645 my $select_node = $s->look_down(id
=> $select{select_label
});
646 warn "Select Node: $select_node" if $select{debug
};
648 unless ($select{append
}) {
649 for my $option ($select_node->look_down('_tag' => 'option')) {
654 my $option = HTML
::Element
->new('option');
655 warn "Option Node: $option" if $select{debug
};
659 while (my $row = $select{data_iter
}->($select{data
})) {
660 warn 'Data Row: ' . Dumper
($row) if $select{debug
};
661 my $o = $option->clone;
662 $o->attr('value', $select{option_value
}->($row));
663 $o->attr('SELECTED', 1) if (exists $select{option_selected
} and $select{option_selected
}->($row));
665 $o->replace_content($select{option_content
}->($row));
666 $select_node->push_content($o);
667 warn $o->as_HTML if $select{debug
};
671 sub HTML
::Element
::set_sibling_content
{
672 my ($elt, $content) = @_;
674 $elt->parent->splice_content($elt->pindex + 1, 1, $content);
677 sub HTML
::TreeBuilder
::parse_string
{
678 my ($package, $string) = @_;
680 my $h = HTML
::TreeBuilder
->new;
681 HTML
::TreeBuilder
->parse($string);
691 HTML::Element::Library - HTML::Element convenience functions
695 use HTML::Element::Library;
696 use HTML::TreeBuilder;
700 HTML:::Element::Library provides extra methods for HTML::Element.
704 =head2 Positional Querying Methods
706 =head3 $elem->siblings
708 Return a list of all nodes under the same parent.
712 Return the index of C<$elem> into the array of siblings of which it is
713 a part. L<HTML::ElementSuper> calls this method C<addr> but I don't
714 think that is a descriptive name. And such naming is deceptively close
715 to the C<address> function of C<HTML::Element>. HOWEVER, in the
716 interest of backwards compatibility, both methods are available.
722 =head3 $elem->position()
724 Returns the coordinates of this element in the tree it inhabits. This
725 is accomplished by succesively calling addr() on ancestor elements
726 until either a) an element that does not support these methods is
727 found, or b) there are no more parents. The resulting list is the
728 n-dimensional coordinates of the element in the tree.
730 =head2 Element Decoration Methods
732 =head3 HTML::Element::Library::super_literal($text)
734 In L<HTML::Element>, Sean Burke discusses super-literals. They are
735 text which does not get escaped. Great for includng Javascript in
736 HTML. Also great for including foreign language into a document.
738 So, you basically toss C<super_literal> your text and back comes your
739 text wrapped in a C<~literal> element.
741 One of these days, I'll around to writing a nice C<EXPORT> section.
743 =head2 Tree Rewriting Methods
745 =head3 "de-prepping" HTML
747 Oftentimes, the HTML to be worked with will have multiple sample rows:
756 But, before you begin to rewrite the HTML with your model data, you
757 typically only want 1 or 2 sample rows.
759 Thus, you want to "crunch" the multiple sample rows to a specified
760 amount. Hence the C<crunch> method:
762 $tree->crunch(look_down => [ '_tag' => 'li' ], leave => 2) ;
764 The C<leave> argument defaults to 1 if not given. The call above would
765 "crunch" the above 4 sample rows to:
772 =head3 Simplifying calls to HTML::FillInForm
774 Since HTML::FillInForm gets and returns strings, using HTML::Element
775 instances becomes tedious:
777 1. Seamstress has an HTML tree that it wants the form filled in on
778 2. Seamstress converts this tree to a string
779 3. FillInForm parses the string into an HTML tree and then fills in the form
780 4. FillInForm converts the HTML tree to a string
781 5. Seamstress re-parses the HTML for additional processing
783 I've filed a bug about this:
784 L<https://rt.cpan.org/Ticket/Display.html?id=44105>
786 This function, fillinform, allows you to pass a tree to fillinform
787 (along with your data structure) and get back a tree:
789 my $new_tree = $html_tree->fillinform($data_structure);
791 =head3 Mapping a hashref to HTML elements
793 It is very common to get a hashref of data from some external source -
794 flat file, database, XML, etc. Therefore, it is important to have a
795 convenient way of mapping this data to HTML.
797 As it turns out, there are 3 ways to do this in
798 HTML::Element::Library. The most strict and structured way to do this
799 is with C<content_handler>. Two other methods, C<hashmap> and
800 C<datamap> require less manual mapping and may prove even more easy to
801 use in certain cases.
803 As is usual with Perl, a practical example is always best. So let's
804 take some sample HTML:
807 <span id="name">?</span>
808 <span id="email">?</span>
809 <span id="gender">?</span>
811 Now, let's say our data structure is this:
813 $ref = { email => 'jim@beam.com', gender => 'lots' } ;
815 And let's start with the most strict way to get what you want:
817 $tree->content_handler(email => $ref->{email} , gender => $ref->{gender}) ;
819 In this case, you manually state the mapping between id tags and
820 hashref keys and then C<content_handler> retrieves the hashref data
821 and pops it in the specified place.
823 Now let's look at the two (actually 2 and a half) other hash-mapping
826 $tree->hashmap(id => $ref);
828 Now, what this function does is super-destructive. It finds every
829 element in the tree with an attribute named id (since 'id' is a
830 parameter, it could find every element with some other attribute also)
831 and replaces the content of those elements with the hashref value.
833 So, in the case above, the
835 <span id="name">?</span>
839 <span id="name"></span>
841 (it would be blank) - because there is nothing in the hash with that
842 value, so it substituted
846 which was blank and emptied the contents.
848 Now, let's assume we want to protect name from being auto-assigned.
851 $tree->hashmap(id => $ref, ['name']);
853 That last array ref is an exclusion list.
855 But wouldnt it be nice if you could do a hashmap, but only assigned
856 things which are defined in the hashref? C<< defmap() >> to the
859 $tree->defmap(id => $ref);
863 <span id="name">?</span>
867 =head4 $elem->hashmap($attr_name, \%hashref, \@excluded, $debug)
869 This method is designed to take a hashref and populate a series of
870 elements. For example:
873 <tr sclass="tr" class="alt" align="left" valign="top">
874 <td smap="people_id">1</td>
875 <td smap="phone">(877) 255-3239</td>
876 <td smap="password">*********</td>
880 In the table above, there are several attributes named C<< smap >>. If
881 we have a hashref whose keys are the same:
883 my %data = (people_id => 888, phone => '444-4444', password => 'dont-you-dare-render');
885 Then a single API call allows us to populate the HTML while excluding
888 $tree->hashmap(smap => \%data, ['password']);
890 Note: the other way to prevent rendering some of the hash mapping is
891 to not give that element the attr you plan to use for hash mapping.
893 Also note: the function C<< hashmap >> has a simple easy-to-type API.
894 Interally, it calls C<< hash_map >> (which has a more verbose keyword
895 calling API). Thus, the above call to C<hashmap()> results in this
898 $tree->hash_map(hash => \%data, to_attr => 'sid', excluding => ['password']);
900 =head4 $elem->defmap($attr_name, \%hashref, $debug)
902 C<defmap> was described above.
904 =head3 $elem->replace_content(@new_elem)
906 Replaces all of C<$elem>'s content with C<@new_elem>.
908 =head3 $elem->wrap_content($wrapper_element)
910 Wraps the existing content in the provided element. If the provided
911 element happens to be a non-element, a push_content is performed
914 =head3 $elem->set_child_content(@look_down, $content)
916 This method looks down $tree using the criteria specified in
917 @look_down using the the HTML::Element look_down() method.
919 After finding the node, it detaches the node's content and pushes
920 $content as the node's content.
922 =head3 $tree->content_handler(%id_content)
924 This is a convenience method. Because the look_down criteria will
931 <a id=fixme href=http://www.somesite.org>replace_content</a>
933 You can call this method to shorten your typing a bit. You can simply
936 $elem->content_handler( fixme => 'new text' )
940 $elem->set_child_content(sid => 'fixme', 'new text')
942 ALSO NOTE: you can pass a hash whose keys are C<id>s and whose values
943 are the content you want there and it will perform the replacement on
946 my %id_content = (name => "Terrence Brannon",
947 email => 'tbrannon@in.com',
949 content => $main_content);
950 $tree->content_handler(%id_content);
952 =head3 $tree->highlander($subtree_span_id, $conditionals, @conditionals_args)
954 This allows for "if-then-else" style processing. Highlander was a
955 movie in which only one would survive. Well, in terms of a tree when
956 looking at a structure that you want to process in C<if-then-else>
957 style, only one child will survive. For example, given this HTML
960 <span klass="highlander" id="age_dialog">
962 Hello, does your mother know you're
963 using her AOL account?
966 Sorry, you're not old enough to enter
967 (and too dumb to lie about your age)
974 We only want one child of the C<span> tag with id C<age_dialog> to
975 remain based on the age of the person visiting the page.
977 So, let's setup a call that will prune the subtree as a function of
982 my $tree = HTML::TreeBuilder->new_from_file('t/html/highlander.html');
987 under10 => sub { $_[0] < 10},
988 under18 => sub { $_[0] < 18},
994 And there we have it. If the age is less than 10, then the node with
995 id C<under10> remains. For age less than 18, the node with id
996 C<under18> remains. Otherwise our "else" condition fires and the child
997 with id C<welcome> remains.
999 =head3 $tree->passover(@id_of_element)
1001 In some cases, you know exactly which element(s) should survive. In
1002 this case, you can simply call C<passover> to remove it's (their)
1003 siblings. For the HTML above, you could delete C<under10> and
1004 C<welcome> by simply calling:
1006 $tree->passover('under18');
1008 Because passover takes an array, you can specify several children to
1011 =head3 $tree->highlander2($tree, $conditionals, @conditionals_args)
1013 Right around the same time that C<table2()> came into being,
1014 Seamstress began to tackle tougher and tougher processing problems. It
1015 became clear that a more powerful highlander was needed... one that
1016 not only snipped the tree of the nodes that should not survive, but
1017 one that allows for post-processing of the survivor node. And one that
1018 was more flexible with how to find the nodes to snip.
1020 Thus (drum roll) C<highlander2()>.
1022 So let's look at our HTML which requires post-selection processing:
1024 <span klass="highlander" id="age_dialog">
1026 Hello, little <span id=age>AGE</span>-year old,
1027 does your mother know you're using her AOL account?
1030 Sorry, you're only <span id=age>AGE</span>
1031 (and too dumb to lie about your age)
1034 Welcome, isn't it good to be <span id=age>AGE</span> years old?
1038 In this case, a branch survives, but it has dummy data in it. We must
1039 take the surviving segment of HTML and rewrite the age C<span> with
1040 the age. Here is how we use C<highlander2()> to do so:
1045 $branch->look_down(id => 'age')->replace_content($age);
1048 my $if_then = $tree->look_down(id => 'age_dialog');
1050 $if_then->highlander2(
1065 cond_arg => [ $age ]
1068 We pass it the tree (C<$if_then>), an arrayref of conditions (C<cond>)
1069 and an arrayref of arguments which are passed to the C<cond>s and to
1070 the replacement subs.
1072 The C<under10>, C<under18> and C<welcome> are id attributes in the
1073 tree of the siblings of which only one will survive. However, should
1074 you need to do more complex look-downs to find the survivor, then
1075 supply an array ref instead of a simple scalar:
1077 $if_then->highlander2(
1079 [class => 'r12'] => [
1083 [class => 'z22'] => [
1087 [class => 'w88'] => [
1092 cond_arg => [ $age ]
1095 =head3 $tree->overwrite_attr($mutation_attr => $mutating_closures)
1097 This method is designed for taking a tree and reworking a set of nodes
1098 in a stereotyped fashion. For instance let's say you have 3 remote
1099 image archives, but you don't want to put long URLs in your img src
1100 tags for reasons of abstraction, re-use and brevity. So instead you do
1103 <img src="/img/smiley-face.jpg" fixup="src lnc">
1104 <img src="/img/hot-babe.jpg" fixup="src playboy">
1105 <img src="/img/footer.jpg" fixup="src foobar">
1107 and then when the tree of HTML is being processed, you make this call:
1110 lnc => sub { my ($tree, $mute_node, $attr_value)= @_; "http://lnc.usc.edu$attr_value" },
1111 playboy => sub { my ($tree, $mute_node, $attr_value)= @_; "http://playboy.com$attr_value" }
1112 foobar => sub { my ($tree, $mute_node, $attr_value)= @_; "http://foobar.info$attr_value" }
1115 $tree->overwrite_attr(fixup => \%closures) ;
1117 and the tags come out modified like so:
1119 <img src="http://lnc.usc.edu/img/smiley-face.jpg" fixup="src lnc">
1120 <img src="http://playboy.com/img/hot-babe.jpg" fixup="src playboy">
1121 <img src="http://foobar.info/img/footer.jpg" fixup="src foobar">
1123 =head3 $tree->mute_elem($mutation_attr => $mutating_closures, [ $post_hook ] )
1125 This is a generalization of C<overwrite_attr>. C<overwrite_attr>
1126 assumes the return value of the closure is supposed overwrite an
1127 attribute value and does it for you. C<mute_elem> is a more general
1128 function which does nothing but hand the closure the element and let
1129 it mutate it as it jolly well pleases :)
1131 In fact, here is the implementation of C<overwrite_attr> to give you a
1132 taste of how C<mute_attr> is used:
1134 sub overwrite_action {
1135 my ($mute_node, %X) = @_;
1137 $mute_node->attr($X{local_attr}{name} => $X{local_attr}{value}{new});
1141 sub HTML::Element::overwrite_attr {
1144 $tree->mute_elem(@_, \&overwrite_action);
1147 =head2 Tree-Building Methods
1149 =head3 Unrolling an array via a single sample element (<ul> container)
1151 This is best described by example. Given this HTML:
1153 <strong>Here are the things I need from the store:</strong>
1155 <li class="store_items">Sample item</li>
1158 We can unroll it like so:
1160 my $li = $tree->look_down(class => 'store_items');
1162 my @items = qw(bread butter vodka);
1164 $tree->iter($li => @items);
1170 <body>Here are the things I need from the store:
1172 <li class="store_items">bread</li>
1173 <li class="store_items">butter</li>
1174 <li class="store_items">vodka</li>
1179 Now, you might be wondering why the API call is:
1181 $tree->iter($li => @items)
1187 and there is no good answer. The latter would be more concise and it
1188 is what I should have done.
1190 =head3 Unrolling an array via n sample elements (<dl> container)
1192 C<iter()> was fine for awhile, but some things (e.g. definition lists)
1193 need a more general function to make them easy to do. Hence
1194 C<iter2()>. This function will be explained by example of unrolling a
1195 simple definition list.
1197 So here's our mock-up HTML from the designer:
1199 <dl class="dual_iter" id="service_plan">
1201 <dd>A person who draws blood.</dd>
1204 <dd>A clone of Iggy Pop.</dd>
1207 <dd>A relative of Edgar Allan Poe.</dd>
1209 <dt class="adstyle">sample header</dt>
1210 <dd class="adstyle2">sample data</dd>
1214 And we want to unroll our data set:
1217 ['the pros' => 'never have to worry about service again'],
1218 ['the cons' => 'upfront extra charge on purchase'],
1219 ['our choice' => 'go with the extended service plan']
1223 Now, let's make this problem a bit harder to show off the power of
1224 C<iter2()>. Let's assume that we want only the last <dt> and it's
1225 accompanying <dd> (the one with "sample data") to be used as the
1226 sample data for unrolling with our data set. Let's further assume that
1227 we want them to remain in the final output.
1229 So now, the API to C<iter2()> will be discussed and we will explain
1230 how our goal of getting our data into HTML fits into the API.
1236 This is how to look down and find the container of all the elements we
1237 will be unrolling. The <dl> tag is the container for the dt and dd
1238 tags we will be unrolling.
1240 If you pass an anonymous subroutine, then it is presumed that
1241 execution of this subroutine will return the HTML::Element
1242 representing the container tag. If you pass an array ref, then this
1243 will be dereferenced and passed to C<HTML::Element::look_down()>.
1245 default value: C<< ['_tag' => 'dl'] >>
1247 Based on the mock HTML above, this default is fine for finding our
1248 container tag. So let's move on.
1250 =item * wrapper_data
1252 This is an array reference of data that we will be putting into the
1253 container. You must supply this. C<@items> above is our
1256 =item * wrapper_proc
1258 After we find the container via C<wrapper_ld>, we may want to
1259 pre-process some aspect of this tree. In our case the first two sets
1260 of dt and dd need to be removed, leaving the last dt and dd. So, we
1261 supply a C<wrapper_proc> which will do this.
1267 This anonymous subroutine returns an array ref of C<HTML::Element>s
1268 that will be cloned and populated with item data (item data is a "row"
1269 of C<wrapper_data>).
1271 default: returns an arrayref consisting of the dt and dd element
1272 inside the container.
1276 This is a subroutine that takes C<wrapper_data> and retrieves one
1277 "row" to be "pasted" into the array ref of C<HTML::Element>s found via
1278 C<item_ld>. I hope that makes sense.
1280 default: shifts C<wrapper_data>.
1284 This is a subroutine that takes the C<item_data> and the
1285 C<HTML::Element>s found via C<item_ld> and produces an arrayref of
1286 C<HTML::Element>s which will eventually be spliced into the container.
1288 Note that this subroutine MUST return the new items. This is done So
1289 that more items than were passed in can be returned. This is useful
1290 when, for example, you must return 2 dts for an input data item. And
1291 when would you do this? When a single term has multiple spellings for
1294 default: expects C<item_data> to be an arrayref of two elements and
1295 C<item_elems> to be an arrayref of two C<HTML::Element>s. It replaces
1296 the content of the C<HTML::Element>s with the C<item_data>.
1300 After building up an array of C<@item_elems>, the subroutine passed as
1301 C<splice> will be given the parent container HTML::Element and the
1302 C<@item_elems>. How the C<@item_elems> end up in the container is up
1303 to this routine: it could put half of them in. It could unshift them
1306 default: C<< $container->splice_content(0, 2, @item_elems) >> In other
1307 words, kill the 2 sample elements with the newly generated @item_elems
1311 So now that we have documented the API, let's see the call we need:
1314 # default wrapper_ld ok.
1315 wrapper_data => \@items,
1316 wrapper_proc => sub {
1317 my ($container) = @_;
1319 # only keep the last 2 dts and dds
1320 my @content_list = $container->content_list;
1321 $container->splice_content(0, @content_list - 2);
1324 # default item_ld is fine.
1325 # default item_data is fine.
1326 # default item_proc is fine.
1328 my ($container, @item_elems) = @_;
1329 $container->unshift_content(@item_elems);
1334 =head3 Select Unrolling
1336 The C<unroll_select> method has this API:
1338 $tree->unroll_select(
1339 select_label => $id_label,
1340 option_value => $closure, # how to get option value from data row
1341 option_content => $closure, # how to get option content from data row
1342 option_selected => $closure, # boolean to decide if SELECTED
1343 data => $data # the data to be put into the SELECT
1344 data_iter => $closure # the thing that will get a row of data
1346 append => $boolean, # remove the sample <OPTION> data or append?
1351 $tree->unroll_select(
1352 select_label => 'clan_list',
1353 option_value => sub { my $row = shift; $row->clan_id },
1354 option_content => sub { my $row = shift; $row->clan_name },
1355 option_selected => sub { my $row = shift; $row->selected },
1356 data => \@query_results,
1357 data_iter => sub { my $data = shift; $data->next },
1362 =head2 Tree-Building Methods: Table Generation
1364 Matthew Sisk has a much more intuitive (imperative) way to generate
1365 tables via his module L<HTML::ElementTable|HTML::ElementTable>.
1367 However, for those with callback fever, the following method is
1368 available. First, we look at a nuts and bolts way to build a table
1369 using only standard L<HTML::Tree> API calls. Then the C<table> method
1370 available here is discussed.
1374 package Simple::Class;
1378 my @name = qw(bob bill brian babette bobo bix);
1379 my @age = qw(99 12 44 52 12 43);
1380 my @weight = qw(99 52 80 124 120 230);
1385 bless {}, ref($this) || $this;
1393 age => $age[rand $#age] + int rand 20,
1394 name => shift @name,
1395 weight => $weight[rand $#weight] + int rand 40
1399 Set::Array->new(@data);
1404 =head4 Sample Usage:
1406 my $data = Simple::Class->load_data;
1407 ++$_->{age} for @$data
1409 =head3 Inline Code to Unroll a Table
1414 <table id="load_data">
1415 <tr> <th>name</th><th>age</th><th>weight</th> </tr>
1417 <td id="name"> NATURE BOY RIC FLAIR </td>
1418 <td id="age"> 35 </td>
1419 <td id="weight"> 220 </td>
1425 =head4 The manual way (*NOT* recommended)
1427 require 'simple-class.pl';
1428 use HTML::Seamstress;
1431 my $seamstress = HTML::Seamstress->new_from_file('simple.html');
1434 my $o = Simple::Class->new;
1435 my $data = $o->load_data;
1437 # find the <table> and <tr>
1438 my $table_node = $seamstress->look_down('id', 'load_data');
1439 my $iter_node = $table_node->look_down('id', 'iterate');
1440 my $table_parent = $table_node->parent;
1443 # drop the sample <table> and <tr> from the HTML
1444 # only add them in if there is data in the model
1445 # this is achieved via the $add_table flag
1447 $table_node->detach;
1451 # Get a row of model data
1452 while (my $row = shift @$data) {
1454 # We got row data. Set the flag indicating ok to hook the table into the HTML
1457 # clone the sample <tr>
1458 my $new_iter_node = $iter_node->clone;
1460 # find the tags labeled name age and weight and
1461 # set their content to the row data
1462 $new_iter_node->content_handler($_ => $row->{$_})
1463 for qw(name age weight);
1465 $table_node->push_content($new_iter_node);
1469 # reattach the table to the HTML tree if we loaded data into some table rows
1471 $table_parent->push_content($table_node) if $add_table;
1473 print $seamstress->as_HTML;
1475 =head3 $tree->table() : API call to Unroll a Table
1477 require 'simple-class.pl';
1478 use HTML::Seamstress;
1481 my $seamstress = HTML::Seamstress->new_from_file('simple.html');
1483 my $o = Simple::Class->new;
1487 # tell seamstress where to find the table, via the method call
1488 # ->look_down('id', $gi_table). Seamstress detaches the table from the
1489 # HTML tree automatically if no table rows can be built
1491 gi_table => 'load_data',
1493 # tell seamstress where to find the tr. This is a bit useless as
1494 # the <tr> usually can be found as the first child of the parent
1498 # the model data to be pushed into the table
1500 table_data => $o->load_data,
1502 # the way to take the model data and obtain one row
1503 # if the table data were a hashref, we would do:
1504 # my $key = (keys %$data)[0]; my $val = $data->{$key}; delete $data->{$key}
1507 my ($self, $data) = @_;
1511 # the way to take a row of data and fill the <td> tags
1514 my ($tr_node, $tr_data) = @_;
1515 $tr_node->content_handler($_ => $tr_data->{$_})
1516 for qw(name age weight)
1520 print $seamstress->as_HTML;
1522 =head4 Looping over Multiple Sample Rows
1527 <table id="load_data" CELLPADDING=8 BORDER=2>
1528 <tr> <th>name</th><th>age</th><th>weight</th> </tr>
1529 <tr id="iterate1" BGCOLOR="white" >
1530 <td id="name"> NATURE BOY RIC FLAIR </td>
1531 <td id="age"> 35 </td>
1532 <td id="weight"> 220 </td>
1534 <tr id="iterate2" BGCOLOR="#CCCC99">
1535 <td id="name"> NATURE BOY RIC FLAIR </td>
1536 <td id="age"> 35 </td>
1537 <td id="weight"> 220 </td>
1542 * Only one change to last API call.
1550 gi_tr => ['iterate1', 'iterate2']
1552 =head3 $tree->table2() : New API Call to Unroll a Table
1554 After 2 or 3 years with C<table()>, I began to develop production
1555 websites with it and decided it needed a cleaner interface,
1556 particularly in the area of handling the fact that C<id> tags will be
1557 the same after cloning a table row.
1559 First, I will give a dry listing of the function's argument
1560 parameters. This will not be educational most likely. A better way to
1561 understand how to use the function is to read through the incremental
1562 unrolling of the function's interface given in conversational style
1563 after the dry listing. But take your pick. It's the same information
1564 given in two different ways.
1566 =head4 Dry/technical parameter documentation
1568 C<< $tree->table2(%param) >> takes the following arguments:
1572 =item * C<< table_ld => $look_down >> : optional
1574 How to find the C<table> element in C<$tree>. If C<$look_down> is an
1575 arrayref, then use C<look_down>. If it is a CODE ref, then call it,
1576 passing it C<$tree>.
1578 Defaults to C<< ['_tag' => 'table'] >> if not passed in.
1580 =item * C<< table_data => $tabular_data >> : required
1582 The data to fill the table with. I<Must> be passed in.
1584 =item * C<< table_proc => $code_ref >> : not implemented
1586 A subroutine to do something to the table once it is found. Not
1587 currently implemented. Not obviously necessary. Just created because
1588 there is a C<tr_proc> and C<td_proc>.
1590 =item * C<< tr_ld => $look_down >> : optional
1592 Same as C<table_ld> but for finding the table row elements. Please
1593 note that the C<tr_ld> is done on the table node that was found
1594 I<instead> of the whole HTML tree. This makes sense. The C<tr>s that
1595 you want exist below the table that was just found.
1597 Defaults to C<< ['_tag' => 'tr'] >> if not passed in.
1599 =item * C<< tr_data => $code_ref >> : optional
1601 How to take the C<table_data> and return a row. Defaults to:
1603 sub { my ($self, $data) = @_;
1607 =item * C<< tr_proc => $code_ref >> : optional
1609 Something to do to the table row we are about to add to the table we
1610 are making. Defaults to a routine which makes the C<id> attribute
1614 my ($self, $tr, $tr_data, $tr_base_id, $row_count) = @_;
1615 $tr->attr(id => sprintf "%s_%d", $tr_base_id, $row_count);
1618 =item * C<< td_proc => $code_ref >> : required
1620 This coderef will take the row of data and operate on the C<td> cells
1621 that are children of the C<tr>. See C<t/table2.t> for several usage
1624 Here's a sample one:
1627 my ($tr, $data) = @_;
1628 my @td = $tr->look_down('_tag' => 'td');
1629 for my $i (0..$#td) {
1630 $td[$i]->splice_content(0, 1, $data->[$i]);
1636 =head4 Conversational parameter documentation
1638 The first thing you need is a table. So we need a look down for that.
1639 If you don't give one, it defaults to
1643 What good is a table to display in without data to display?! So you
1644 must supply a scalar representing your tabular data source. This
1645 scalar might be an array reference, a C<next>able iterator, a DBI
1646 statement handle. Whatever it is, it can be iterated through to build
1647 up rows of table data. These two required fields (the way to find the
1648 table and the data to display in the table) are C<table_ld> and
1649 C<table_data> respectively. A little more on C<table_ld>. If this
1650 happens to be a CODE ref, then execution of the code ref is presumed
1651 to return the C<HTML::Element> representing the table in the HTML
1654 Next, we get the row or rows which serve as sample C<tr> elements by
1655 doing a C<look_down> from the C<table_elem>. While normally one sample
1656 row is enough to unroll a table, consider when you have alternating
1657 table rows. This API call would need one of each row so that it can
1658 cycle through the sample rows as it loops through the data.
1659 Alternatively, you could always just use one row and make the
1660 necessary changes to the single C<tr> row by mutating the element in
1661 C<tr_proc>, discussed below. The default C<tr_ld> is C<< ['_tag' =>
1662 'tr'] >> but you can overwrite it. Note well, if you overwrite it with
1663 a subroutine, then it is expected that the subroutine will return the
1664 C<HTML::Element>(s) which are C<tr> element(s). The reason a
1665 subroutine might be preferred is in the case that the HTML designers
1666 gave you 8 sample C<tr> rows but only one prototype row is needed. So
1667 you can write a subroutine, to splice out the 7 rows you don't need
1668 and leave the one sample row remaining so that this API call can clone
1669 it and supply it to the C<tr_proc> and C<td_proc> calls.
1671 Now, as we move through the table rows with table data, we need to do
1672 two different things on each table row:
1676 =item * get one row of data from the C<table_data> via C<tr_data>
1678 The default procedure assumes the C<table_data> is an array reference
1679 and shifts a row off of it:
1682 my ($self, $data) = @_;
1686 Your function MUST return undef when there is no more rows to lay out.
1688 =item * take the C<tr> element and mutate it via C<tr_proc>
1690 The default procedure simply makes the id of the table row unique:
1693 my ($self, $tr, $tr_data, $row_count, $root_id) = @_;
1694 $tr->attr(id => sprintf "%s_%d", $root_id, $row_count);
1699 Now that we have our row of data, we call C<td_proc> so that it can
1700 take the data and the C<td> cells in this C<tr> and process them. This
1701 function I<must> be supplied.
1703 =head3 Whither a Table with No Rows
1705 Often when a table has no rows, we want to display a message
1706 indicating this to the view. Use conditional processing to decide what
1710 <table><tr><td>No Data is Good Data</td></tr></table>
1714 <table id="load_data">
1715 <tr> <th>name</th><th>age</th><th>weight</th> </tr>
1717 <td id="name"> NATURE BOY RIC FLAIR </td>
1718 <td id="age"> 35 </td>
1719 <td id="weight"> 220 </td>
1725 =head2 Tree-Killing Methods
1729 This removes any nodes from the tree which consist of nothing or
1730 nothing but whitespace. See also delete_ignorable_whitespace in
1733 =head2 Loltree Functions
1735 A loltree is an arrayref consisting of arrayrefs which is used by C<<
1736 new_from__lol >> in L<HTML::Element> to produce HTML trees. The CPAN
1737 distro L<XML::Element::Tolol> creates such XML trees by parsing XML
1738 files, analagous to L<XML::Toolkit>. The purpose of the functions in
1739 this section is to allow you manipulate a loltree programmatically.
1741 These could not be methods because if you bless a loltree, then
1742 HTML::Tree will barf.
1744 =head3 HTML::Element::newchild($lol, $parent_label, @newchild)
1746 Given this initial loltree:
1748 my $initial_lol = [ note => [ shopping => [ item => 'sample' ] ] ];
1752 sub shopping_items {
1753 my @shopping_items = map { [ item => _ ] } qw(bread butter beans);
1757 my $new_lol = HTML::Element::newnode($initial_lol, item => shopping_items());
1759 will replace the single sample with a list of shopping items:
1781 Thanks to kcott and the other Perlmonks in this thread:
1782 http://www.perlmonks.org/?node_id=912416
1787 =head2 L<HTML::Tree>
1789 A perl package for creating and manipulating HTML trees.
1791 =head2 L<HTML::ElementTable>
1793 An L<HTML::Tree> - based module which allows for manipulation of HTML
1794 trees using cartesian coordinations.
1796 =head2 L<HTML::Seamstress>
1798 An L<HTML::Tree> - based module inspired by XMLC
1799 (L<http://xmlc.enhydra.org>), allowing for dynamic HTML generation via
1802 =head2 Push-style templating systems
1804 A comprehensive cross-language
1805 L<list of push-style templating systems|http://perlmonks.org/?node_id=674225>.
1813 currently the API expects the subtrees to survive or be pruned to be
1816 $if_then->highlander2([
1817 under10 => sub { $_[0] < 10} ,
1818 under18 => sub { $_[0] < 18} ,
1823 $branch->look_down(id => 'age')->replace_content($age);
1828 but, it should be more flexible. the C<under10>, and C<under18> are
1829 expected to be ids in the tree... but it is not hard to have a check
1830 to see if this field is an array reference and if it, then to do a
1833 $if_then->highlander2([
1834 [class => 'under10'] => sub { $_[0] < 10} ,
1835 [class => 'under18'] => sub { $_[0] < 18} ,
1836 [class => 'welcome'] => [
1840 $branch->look_down(id => 'age')->replace_content($age);
1849 Original author Terrence Brannon, E<lt>tbone@cpan.orgE<gt>.
1851 Adopted by Marius Gavrilescu C<< <marius@ieval.ro> >>.
1853 I appreciate the feedback from M. David Moussa Leo Keita regarding
1854 some issues with the test suite, namely (1) CRLF leading to test
1855 breakage in F<t/crunch.t> and (2) using the wrong module in
1856 F<t/prune.t> thus not having the right functionality available.
1858 Many thanks to BARBIE for his RT bug report.
1860 Many thanks to perlmonk kcott for his work on array rewriting:
1861 L<http://www.perlmonks.org/?node_id=912416>. It was crucial in the
1862 development of newchild.
1864 =head1 COPYRIGHT AND LICENSE
1866 Coypright (C) 2014-2015 by Marius Gavrilescu
1868 Copyright (C) 2004-2012 by Terrence Brannon
1870 This library is free software; you can redistribute it and/or modify
1871 it under the same terms as Perl itself, either Perl version 5.8.4 or,
1872 at your option, any later version of Perl 5 you may have available.
This page took 0.199 seconds and 4 git commands to generate.