.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.29) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "HTML::Element::Library 3pm" .TH HTML::Element::Library 3pm "2016-06-27" "perl v5.22.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" HTML::Element::Library \- HTML::Element convenience functions .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use HTML::Element::Library; \& use HTML::TreeBuilder; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" HTML:::Element::Library provides extra methods for HTML::Element. .SH "METHODS" .IX Header "METHODS" .SS "Aliases" .IX Subsection "Aliases" These are short aliases for common operations: .ie n .IP "\fI\fI$el\fI\fR\->\fBfid\fR(\fI\fI$id\fI\fR)" 4 .el .IP "\fI\f(CI$el\fI\fR\->\fBfid\fR(\fI\f(CI$id\fI\fR)" 4 .IX Item "$el->fid($id)" Finds an element given its id. Equivalent to \f(CW\*(C`$el\->look_down(id => $id)\*(C'\fR. .ie n .IP "\fI\fI$el\fI\fR\->\fBfclass\fR(\fI\fI$class\fI\fR)" 4 .el .IP "\fI\f(CI$el\fI\fR\->\fBfclass\fR(\fI\f(CI$class\fI\fR)" 4 .IX Item "$el->fclass($class)" Finds one or more elements given one of their classes. Equivalent to \f(CW\*(C`$el\->look_down(class => qr/\eb$class\eb/s)\*(C'\fR .SS "Positional Querying Methods" .IX Subsection "Positional Querying Methods" \fI\f(CI$elem\fI\->siblings\fR .IX Subsection "$elem->siblings" .PP Return a list of all nodes under the same parent. .PP \fI\f(CI$elem\fI\->sibdex\fR .IX Subsection "$elem->sibdex" .PP Return the index of \f(CW$elem\fR into the array of siblings of which it is a part. HTML::ElementSuper calls this method \f(CW\*(C`addr\*(C'\fR but I don't think that is a descriptive name. And such naming is deceptively close to the \f(CW\*(C`address\*(C'\fR function of \f(CW\*(C`HTML::Element\*(C'\fR. \s-1HOWEVER,\s0 in the interest of backwards compatibility, both methods are available. .PP \fI\f(CI$elem\fI\->addr\fR .IX Subsection "$elem->addr" .PP Same as sibdex .PP \fI\f(CI$elem\fI\->\fIposition()\fI\fR .IX Subsection "$elem->position()" .PP Returns the coordinates of this element in the tree it inhabits. This is accomplished by succesively calling \fIaddr()\fR on ancestor elements until either a) an element that does not support these methods is found, or b) there are no more parents. The resulting list is the n\-dimensional coordinates of the element in the tree. .SS "Element Decoration Methods" .IX Subsection "Element Decoration Methods" \fIHTML::Element::Library::super_literal($text)\fR .IX Subsection "HTML::Element::Library::super_literal($text)" .PP In HTML::Element, Sean Burke discusses super-literals. They are text which does not get escaped. Great for includng Javascript in \&\s-1HTML.\s0 Also great for including foreign language into a document. .PP So, you basically toss \f(CW\*(C`super_literal\*(C'\fR your text and back comes your text wrapped in a \f(CW\*(C`~literal\*(C'\fR element. .PP One of these days, I'll around to writing a nice \f(CW\*(C`EXPORT\*(C'\fR section. .SS "Tree Rewriting Methods" .IX Subsection "Tree Rewriting Methods" \fI\*(L"de-prepping\*(R" \s-1HTML\s0\fR .IX Subsection "de-prepping HTML" .PP Oftentimes, the \s-1HTML\s0 to be worked with will have multiple sample rows: .PP .Vb 6 \&
    \&
  1. bread \&
  2. butter \&
  3. beer \&
  4. bacon \&
.Ve .PP But, before you begin to rewrite the \s-1HTML\s0 with your model data, you typically only want 1 or 2 sample rows. .PP Thus, you want to \*(L"crunch\*(R" the multiple sample rows to a specified amount. Hence the \f(CW\*(C`crunch\*(C'\fR method: .PP .Vb 1 \& $tree\->crunch(look_down => [ \*(Aq_tag\*(Aq => \*(Aqli\*(Aq ], leave => 2) ; .Ve .PP The \f(CW\*(C`leave\*(C'\fR argument defaults to 1 if not given. The call above would \&\*(L"crunch\*(R" the above 4 sample rows to: .PP .Vb 4 \&
    \&
  1. bread \&
  2. butter \&
.Ve .PP \fISimplifying calls to HTML::FillInForm\fR .IX Subsection "Simplifying calls to HTML::FillInForm" .PP Since HTML::FillInForm gets and returns strings, using HTML::Element instances becomes tedious: .PP .Vb 5 \& 1. Seamstress has an HTML tree that it wants the form filled in on \& 2. Seamstress converts this tree to a string \& 3. FillInForm parses the string into an HTML tree and then fills in the form \& 4. FillInForm converts the HTML tree to a string \& 5. Seamstress re\-parses the HTML for additional processing .Ve .PP I've filed a bug about this: .PP This function, fillinform, allows you to pass a tree to fillinform (along with your data structure) and get back a tree: .PP .Vb 1 \& my $new_tree = $html_tree\->fillinform($data_structure); .Ve .PP \fIMapping a hashref to \s-1HTML\s0 elements\fR .IX Subsection "Mapping a hashref to HTML elements" .PP It is very common to get a hashref of data from some external source \- flat file, database, \s-1XML,\s0 etc. Therefore, it is important to have a convenient way of mapping this data to \s-1HTML.\s0 .PP As it turns out, there are 3 ways to do this in HTML::Element::Library. The most strict and structured way to do this is with \f(CW\*(C`content_handler\*(C'\fR. Two other methods, \f(CW\*(C`hashmap\*(C'\fR and \&\f(CW\*(C`datamap\*(C'\fR require less manual mapping and may prove even more easy to use in certain cases. .PP As is usual with Perl, a practical example is always best. So let's take some sample \s-1HTML:\s0 .PP .Vb 4 \&

user data

\& ? \& ? \& ? .Ve .PP Now, let's say our data structure is this: .PP .Vb 1 \& $ref = { email => \*(Aqjim@beam.com\*(Aq, gender => \*(Aqlots\*(Aq } ; .Ve .PP And let's start with the most strict way to get what you want: .PP .Vb 1 \& $tree\->content_handler(email => $ref\->{email} , gender => $ref\->{gender}) ; .Ve .PP In this case, you manually state the mapping between id tags and hashref keys and then \f(CW\*(C`content_handler\*(C'\fR retrieves the hashref data and pops it in the specified place. .PP Now let's look at the two (actually 2 and a half) other hash-mapping methods. .PP .Vb 1 \& $tree\->hashmap(id => $ref); .Ve .PP Now, what this function does is super-destructive. It finds every element in the tree with an attribute named id (since 'id' is a parameter, it could find every element with some other attribute also) and replaces the content of those elements with the hashref value. .PP So, in the case above, the .PP .Vb 1 \& ? .Ve .PP would come out as .PP .Vb 1 \& .Ve .PP (it would be blank) \- because there is nothing in the hash with that value, so it substituted .PP .Vb 1 \& $ref\->{name} .Ve .PP which was blank and emptied the contents. .PP Now, let's assume we want to protect name from being auto-assigned. Here is what you do: .PP .Vb 1 \& $tree\->hashmap(id => $ref, [\*(Aqname\*(Aq]); .Ve .PP That last array ref is an exclusion list. .PP But wouldnt it be nice if you could do a hashmap, but only assigned things which are defined in the hashref? \f(CW\*(C`defmap()\*(C'\fR to the rescue: .PP .Vb 1 \& $tree\->defmap(id => $ref); .Ve .PP does just that, so .PP .Vb 1 \& ? .Ve .PP would be left alone. .PP \f(CW$elem\fR\->hashmap($attr_name, \e%hashref, \e@excluded, \f(CW$debug\fR) .IX Subsection "$elem->hashmap($attr_name, %hashref, @excluded, $debug)" .PP This method is designed to take a hashref and populate a series of elements. For example: .PP .Vb 7 \& \& \& \& \& \& \&
1(877) 255\-3239*********
.Ve .PP In the table above, there are several attributes named \f(CW\*(C`smap\*(C'\fR. If we have a hashref whose keys are the same: .PP .Vb 1 \& my %data = (people_id => 888, phone => \*(Aq444\-4444\*(Aq, password => \*(Aqdont\-you\-dare\-render\*(Aq); .Ve .PP Then a single \s-1API\s0 call allows us to populate the \s-1HTML\s0 while excluding those ones we don't: .PP .Vb 1 \& $tree\->hashmap(smap => \e%data, [\*(Aqpassword\*(Aq]); .Ve .PP Note: the other way to prevent rendering some of the hash mapping is to not give that element the attr you plan to use for hash mapping. .PP Also note: the function \f(CW\*(C`hashmap\*(C'\fR has a simple easy-to-type \s-1API.\s0 Interally, it calls \f(CW\*(C`hash_map\*(C'\fR (which has a more verbose keyword calling \s-1API\s0). Thus, the above call to \f(CW\*(C`hashmap()\*(C'\fR results in this call: .PP .Vb 1 \& $tree\->hash_map(hash => \e%data, to_attr => \*(Aqsid\*(Aq, excluding => [\*(Aqpassword\*(Aq]); .Ve .PP \f(CW$elem\fR\->defmap($attr_name, \e%hashref, \f(CW$debug\fR) .IX Subsection "$elem->defmap($attr_name, %hashref, $debug)" .PP \&\f(CW\*(C`defmap\*(C'\fR was described above. .PP \fI\f(CI$elem\fI\->replace_content(@new_elem)\fR .IX Subsection "$elem->replace_content(@new_elem)" .PP Replaces all of \f(CW$elem\fR's content with \f(CW@new_elem\fR. .PP \fI\f(CI$elem\fI\->wrap_content($wrapper_element)\fR .IX Subsection "$elem->wrap_content($wrapper_element)" .PP Wraps the existing content in the provided element. If the provided element happens to be a non-element, a push_content is performed instead. .PP \fI\f(CI$elem\fI\->set_child_content(@look_down, \f(CI$content\fI)\fR .IX Subsection "$elem->set_child_content(@look_down, $content)" .PP This method looks down \f(CW$tree\fR using the criteria specified in \&\f(CW@look_down\fR using the the HTML::Element \fIlook_down()\fR method. .PP After finding the node, it detaches the node's content and pushes \&\f(CW$content\fR as the node's content. .PP \fI\f(CI$tree\fI\->content_handler(%id_content)\fR .IX Subsection "$tree->content_handler(%id_content)" .PP This is a convenience method. Because the look_down criteria will often simply be: .PP .Vb 1 \& id => \*(Aqfixme\*(Aq .Ve .PP to find things like: .PP .Vb 1 \& replace_content .Ve .PP You can call this method to shorten your typing a bit. You can simply type .PP .Vb 1 \& $elem\->content_handler( fixme => \*(Aqnew text\*(Aq ) .Ve .PP Instead of typing: .PP .Vb 1 \& $elem\->set_child_content(sid => \*(Aqfixme\*(Aq, \*(Aqnew text\*(Aq) .Ve .PP \&\s-1ALSO NOTE:\s0 you can pass a hash whose keys are \f(CW\*(C`id\*(C'\fRs and whose values are the content you want there and it will perform the replacement on each hash member: .PP .Vb 5 \& my %id_content = (name => "Terrence Brannon", \& email => \*(Aqtbrannon@in.com\*(Aq, \& balance => 666, \& content => $main_content); \& $tree\->content_handler(%id_content); .Ve .PP \fI\f(CI$tree\fI\->highlander($subtree_span_id, \f(CI$conditionals\fI, \f(CI@conditionals_args\fI)\fR .IX Subsection "$tree->highlander($subtree_span_id, $conditionals, @conditionals_args)" .PP This allows for \*(L"if-then-else\*(R" style processing. Highlander was a movie in which only one would survive. Well, in terms of a tree when looking at a structure that you want to process in \f(CW\*(C`if\-then\-else\*(C'\fR style, only one child will survive. For example, given this \s-1HTML\s0 template: .PP .Vb 10 \& \& \& Hello, does your mother know you\*(Aqre \& using her AOL account? \& \& \& Sorry, you\*(Aqre not old enough to enter \& (and too dumb to lie about your age) \& \& \& Welcome \& \& .Ve .PP We only want one child of the \f(CW\*(C`span\*(C'\fR tag with id \f(CW\*(C`age_dialog\*(C'\fR to remain based on the age of the person visiting the page. .PP So, let's setup a call that will prune the subtree as a function of age: .PP .Vb 3 \& sub process_page { \& my $age = shift; \& my $tree = HTML::TreeBuilder\->new_from_file(\*(Aqt/html/highlander.html\*(Aq); \& \& $tree\->highlander \& (age_dialog => \& [ \& under10 => sub { $_[0] < 10}, \& under18 => sub { $_[0] < 18}, \& welcome => sub { 1 } \& ], \& $age \& ); .Ve .PP And there we have it. If the age is less than 10, then the node with id \f(CW\*(C`under10\*(C'\fR remains. For age less than 18, the node with id \&\f(CW\*(C`under18\*(C'\fR remains. Otherwise our \*(L"else\*(R" condition fires and the child with id \f(CW\*(C`welcome\*(C'\fR remains. .PP \fI\f(CI$tree\fI\->passover(@id_of_element)\fR .IX Subsection "$tree->passover(@id_of_element)" .PP In some cases, you know exactly which element(s) should survive. In this case, you can simply call \f(CW\*(C`passover\*(C'\fR to remove it's (their) siblings. For the \s-1HTML\s0 above, you could delete \f(CW\*(C`under10\*(C'\fR and \&\f(CW\*(C`welcome\*(C'\fR by simply calling: .PP .Vb 1 \& $tree\->passover(\*(Aqunder18\*(Aq); .Ve .PP Because passover takes an array, you can specify several children to preserve. .PP \fI\f(CI$tree\fI\->highlander2($tree, \f(CI$conditionals\fI, \f(CI@conditionals_args\fI)\fR .IX Subsection "$tree->highlander2($tree, $conditionals, @conditionals_args)" .PP Right around the same time that \f(CW\*(C`table2()\*(C'\fR came into being, Seamstress began to tackle tougher and tougher processing problems. It became clear that a more powerful highlander was needed... one that not only snipped the tree of the nodes that should not survive, but one that allows for post-processing of the survivor node. And one that was more flexible with how to find the nodes to snip. .PP Thus (drum roll) \f(CW\*(C`highlander2()\*(C'\fR. .PP So let's look at our \s-1HTML\s0 which requires post-selection processing: .PP .Vb 10 \& \& \& Hello, little AGE\-year old, \& does your mother know you\*(Aqre using her AOL account? \& \& \& Sorry, you\*(Aqre only AGE \& (and too dumb to lie about your age) \& \& \& Welcome, isn\*(Aqt it good to be AGE years old? \& \& .Ve .PP In this case, a branch survives, but it has dummy data in it. We must take the surviving segment of \s-1HTML\s0 and rewrite the age \f(CW\*(C`span\*(C'\fR with the age. Here is how we use \f(CW\*(C`highlander2()\*(C'\fR to do so: .PP .Vb 5 \& sub replace_age { \& my $branch = shift; \& my $age = shift; \& $branch\->look_down(id => \*(Aqage\*(Aq)\->replace_content($age); \& } \& \& my $if_then = $tree\->look_down(id => \*(Aqage_dialog\*(Aq); \& \& $if_then\->highlander2( \& cond => [ \& under10 => [ \& sub { $_[0] < 10} , \& \e&replace_age \& ], \& under18 => [ \& sub { $_[0] < 18} , \& \e&replace_age \& ], \& welcome => [ \& sub { 1 }, \& \e&replace_age \& ] \& ], \& cond_arg => [ $age ] \& ); .Ve .PP We pass it the tree (\f(CW$if_then\fR), an arrayref of conditions (\f(CW\*(C`cond\*(C'\fR) and an arrayref of arguments which are passed to the \f(CW\*(C`cond\*(C'\fRs and to the replacement subs. .PP The \f(CW\*(C`under10\*(C'\fR, \f(CW\*(C`under18\*(C'\fR and \f(CW\*(C`welcome\*(C'\fR are id attributes in the tree of the siblings of which only one will survive. However, should you need to do more complex look-downs to find the survivor, then supply an array ref instead of a simple scalar: .PP .Vb 10 \& $if_then\->highlander2( \& cond => [ \& [class => \*(Aqr12\*(Aq] => [ \& sub { $_[0] < 10} , \& \e&replace_age \& ], \& [class => \*(Aqz22\*(Aq] => [ \& sub { $_[0] < 18} , \& \e&replace_age \& ], \& [class => \*(Aqw88\*(Aq] => [ \& sub { 1 }, \& \e&replace_age \& ] \& ], \& cond_arg => [ $age ] \& ); .Ve .PP \fI\f(CI$tree\fI\->overwrite_attr($mutation_attr => \f(CI$mutating_closures\fI)\fR .IX Subsection "$tree->overwrite_attr($mutation_attr => $mutating_closures)" .PP This method is designed for taking a tree and reworking a set of nodes in a stereotyped fashion. For instance let's say you have 3 remote image archives, but you don't want to put long URLs in your img src tags for reasons of abstraction, re-use and brevity. So instead you do this: .PP .Vb 3 \& \& \& .Ve .PP and then when the tree of \s-1HTML\s0 is being processed, you make this call: .PP .Vb 5 \& my %closures = ( \& lnc => sub { my ($tree, $mute_node, $attr_value)= @_; "http://lnc.usc.edu$attr_value" }, \& playboy => sub { my ($tree, $mute_node, $attr_value)= @_; "http://playboy.com$attr_value" } \& foobar => sub { my ($tree, $mute_node, $attr_value)= @_; "http://foobar.info$attr_value" } \& ) \& \& $tree\->overwrite_attr(fixup => \e%closures) ; .Ve .PP and the tags come out modified like so: .PP .Vb 3 \& \& \& .Ve .PP \fI\f(CI$tree\fI\->mute_elem($mutation_attr => \f(CI$mutating_closures\fI, [ \f(CI$post_hook\fI ] )\fR .IX Subsection "$tree->mute_elem($mutation_attr => $mutating_closures, [ $post_hook ] )" .PP This is a generalization of \f(CW\*(C`overwrite_attr\*(C'\fR. \f(CW\*(C`overwrite_attr\*(C'\fR assumes the return value of the closure is supposed overwrite an attribute value and does it for you. \f(CW\*(C`mute_elem\*(C'\fR is a more general function which does nothing but hand the closure the element and let it mutate it as it jolly well pleases :) .PP In fact, here is the implementation of \f(CW\*(C`overwrite_attr\*(C'\fR to give you a taste of how \f(CW\*(C`mute_attr\*(C'\fR is used: .PP .Vb 2 \& sub overwrite_action { \& my ($mute_node, %X) = @_; \& \& $mute_node\->attr($X{local_attr}{name} => $X{local_attr}{value}{new}); \& } \& \& \& sub HTML::Element::overwrite_attr { \& my $tree = shift; \& \& $tree\->mute_elem(@_, \e&overwrite_action); \& } .Ve .SS "Tree-Building Methods" .IX Subsection "Tree-Building Methods" \fIUnrolling an array via a single sample element (