From: Terrence Brannon Date: Sat, 23 Jan 2010 01:31:46 +0000 (-0500) Subject: implement and test crunch method X-Git-Tag: 5.200_001~31 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=d161c455bd0d5835fca07d91ac428da07f47136b;p=html-element-library.git implement and test crunch method --- diff --git a/MANIFEST b/MANIFEST index c23f2f1..ae60b96 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,6 +1,7 @@ Changes CHANGES lib/HTML/Element/Library.pm +lib/HTML/Element/Library.pod lib/HTML/Element/Library/Changes.pod Makefile.PL MANIFEST This list of files @@ -10,16 +11,27 @@ t/00system.t t/Arsenal.pl t/Arsenal.pm t/content_handler.t +t/crunch.t t/data/3dig.dat t/data/4dig.dat t/data/table2.pm +t/defmap.t t/dual_iter.t +t/fillinform.t +t/hashmap.t t/highlander.t t/highlander2.t t/HTML-Element-Library.t +t/html/crunch/crunch.initial +t/html/defmap/defmap.exp +t/html/defmap/defmap.gen +t/html/defmap/defmap.initial t/html/dual_iter.exp t/html/dual_iter.gen t/html/dual_iter.html +t/html/fillinform/fillinform.exp +t/html/fillinform/fillinform.gen +t/html/fillinform/fillinform.initial t/html/highlander-15.exp t/html/highlander-15.gen t/html/highlander-5.exp @@ -33,6 +45,8 @@ t/html/highlander2-27.exp t/html/highlander2-27.gen t/html/highlander2-5.exp t/html/highlander2-5.gen +t/html/highlander2-passover.exp +t/html/highlander2-passover.gen t/html/highlander2.html t/html/iter.exp t/html/iter.gen @@ -40,6 +54,10 @@ t/html/iter.html t/html/iter2.exp t/html/iter2.gen t/html/iter2.html +t/html/same_as.html +t/html/same_as/same_as.exp +t/html/same_as/same_as.gen +t/html/same_as/same_as.initial t/html/table-alt.exp t/html/table-alt.gen t/html/table-alt.html @@ -81,6 +99,7 @@ t/m/SimpleClass.pm t/m/TestUtils.pm t/not-used/Arsenal.pl t/not-used/Arsenal.pm +t/passover.t t/position.t t/q t/replace_content.t diff --git a/lib/HTML/Element/Library.pm b/lib/HTML/Element/Library.pm index fa03765..6e7006a 100644 --- a/lib/HTML/Element/Library.pm +++ b/lib/HTML/Element/Library.pm @@ -72,6 +72,24 @@ sub HTML::Element::defmap { } +sub HTML::Element::crunch { + my $container = shift; + + my %p = validate(@_, { + look_down => { type => ARRAYREF }, + leave => { default => 1 }, + }); + + my @look_down = @{$p{look_down}} ; + my @elem = $container->look_down( @look_down ) ; + + my $left; + + for my $elem (@elem) { + $elem->detach if $left++ >= $p{leave} ; + } + +} sub HTML::Element::hash_map { my $container = shift; diff --git a/lib/HTML/Element/Library.pod b/lib/HTML/Element/Library.pod index 5c77497..c7cde76 100644 --- a/lib/HTML/Element/Library.pod +++ b/lib/HTML/Element/Library.pod @@ -58,6 +58,31 @@ One of these days, I'll around to writing a nice C section. =head2 Tree Rewriting Methods +=head3 "de-prepping" HTML + +Oftentimes, the HTML to be worked with will have multiple sample rows: + +
    +
  1. bread +
  2. butter +
  3. beer +
  4. bacon +
+ +But, before you begin to rewrite the HTML with your model data, you typically only want 1 or 2 sample rows. + +Thus, you want to "crunch" the multiple sample rows to a specified amount. Hence the C method: + + $tree->crunch(look_down => [ '_tag' => 'li' ], leave => 2) ; + +The C argument defaults to 1 if not given. The call above would "crunch" the above 4 sample rows to: + +
    +
  1. bread +
  2. butter +
+ + =head3 Simplifying calls to HTML::FillInForm Since HTML::FillInForm gets and returns strings, using HTML::Element instances @@ -79,6 +104,8 @@ get back a tree: my $new_tree = $html_tree->fillinform($data_structure); + + =head3 Mapping a hashref to HTML elements It is very common to get a hashref of data from some external source - flat file, database, XML, etc. diff --git a/t/crunch.t b/t/crunch.t new file mode 100644 index 0000000..b4a42e3 --- /dev/null +++ b/t/crunch.t @@ -0,0 +1,28 @@ +# This might look like shell script, but it's actually -*- perl -*- +use strict;use warnings; +use lib qw(t/ t/m/); + +use File::Slurp; +use Test::More qw(no_plan); + +use TestUtils; +use HTML::TreeBuilder; +use HTML::Element::Library; + +sub tage { + + my $root = "t/html/crunch/crunch"; + + my $tree = HTML::TreeBuilder->new_from_file("$root.initial")->guts; + + + $tree->crunch(look_down => [ class => 'imageElement' ], leave => 1); + + my $generated_html = ptree($tree, "$root.gen"); + + is ($generated_html, File::Slurp::read_file("$root.exp"), "HTML for crunch"); +} + + +tage(); + diff --git a/t/html/crunch/crunch.gen b/t/html/crunch/crunch.gen new file mode 100644 index 0000000..0e6e3e1 --- /dev/null +++ b/t/html/crunch/crunch.gen @@ -0,0 +1,30 @@ + + + + + + + + + +
+
+

Item 1 Title

+

Item 1 Description

+ + Item 1 Title thumbnail of Item 1 Title
+
+ + diff --git a/t/html/crunch/crunch.initial b/t/html/crunch/crunch.initial new file mode 100644 index 0000000..01353d1 --- /dev/null +++ b/t/html/crunch/crunch.initial @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + +
+ +
+

Item 1 Title

+

Item 1 Description

+ + + Item 1 Title + thumbnail of Item 1 Title +
+ +
+

Item 2 Title

+

Item 2 Description

+ + + Item 2 Title + thumbnail of Item 2 Title +
+ +
+

Item 2 Title

+

Item 2 Description

+ + + Item 2 Title + thumbnail of Item 2 Title +
+ +
+ +

Item 3 Title

+

Item 3 Description

+ + + Item 3 Title + thumbnail of Item 3 Title +
+
+ + + + +