implement and test crunch method
authorTerrence Brannon <schemelab@li2-168.members.linode.com>
Sat, 23 Jan 2010 01:31:46 +0000 (20:31 -0500)
committerTerrence Brannon <schemelab@li2-168.members.linode.com>
Sat, 23 Jan 2010 01:31:46 +0000 (20:31 -0500)
MANIFEST
lib/HTML/Element/Library.pm
lib/HTML/Element/Library.pod
t/crunch.t [new file with mode: 0644]
t/html/crunch/crunch.gen [new file with mode: 0644]
t/html/crunch/crunch.initial [new file with mode: 0644]

index c23f2f147cfe59dd04fb4b6da14fc224da52348d..ae60b96bfd37f50a7112bec30b036959e41c04ab 100644 (file)
--- 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
index fa037657ea21e95eda9f5800418f99e31d306215..6e7006a31cc892b5e8c538b53325e3b3c44880dd 100644 (file)
@@ -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;
index 5c7749762bfbbb39759086ca4e4602b478f2bb21..c7cde764c2da5f3504d1b0e54570382bd6167287 100644 (file)
@@ -58,6 +58,31 @@ One of these days, I'll around to writing a nice C<EXPORT> section.
 
 =head2 Tree Rewriting Methods
 
+=head3 "de-prepping" HTML
+
+Oftentimes, the HTML to be worked with will have multiple sample rows:
+
+  <OL>
+   <LI>bread
+   <LI>butter
+   <LI>beer
+   <LI>bacon
+  </OL>
+
+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<crunch> method:
+
+  $tree->crunch(look_down => [ '_tag' => 'li' ], leave => 2) ;
+
+The C<leave> argument defaults to 1 if not given. The call above would "crunch" the above 4 sample rows to:
+
+  <OL>
+   <LI>bread
+   <LI>butter
+  </OL>
+
+
 =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 (file)
index 0000000..b4a42e3
--- /dev/null
@@ -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 (file)
index 0000000..0e6e3e1
--- /dev/null
@@ -0,0 +1,30 @@
+<html>
+  <head>
+<script src="smooth/scripts/mootools-1.2.1-core-yc.js" type="text/javascript"></script>
+<script src="smooth/scripts/mootools-1.2-more.js" type="text/javascript"></script>
+<script src="smooth/scripts/jd.gallery.js" type="text/javascript"></script>
+    <link rel="stylesheet" /="/" href="smooth/css/jd.gallery.css"
+      media="screen" type="text/css">
+  </head>
+  <body>
+<script type="text/javascript">
+function startGallery() {
+var myGallery = new gallery($('myGallery'), {
+timed: true
+});
+}
+window.addEvent('domready', startGallery);
+</script>
+    <div id="myGallery">
+      <div class="imageElement">
+        <h3>Item 1 Title</h3>
+        <p>Item 1 Description</p>
+        <a href="gallery-images/item-1/" title="open image" class="open"></a>
+        <img /="/" alt="Item 1 Title"
+          src="http://smoothgallery.jondesign.net/assets/images/samples/77196_6784-gallery.jpg"
+          class="full"> <img /="/" alt="thumbnail of Item 1 Title"
+          src="http://smoothgallery.jondesign.net/assets/images/samples/77196_6784-mini.jpg"
+          class="thumbnail"></div>
+    </div>
+  </body>
+</html>
diff --git a/t/html/crunch/crunch.initial b/t/html/crunch/crunch.initial
new file mode 100644 (file)
index 0000000..01353d1
--- /dev/null
@@ -0,0 +1,65 @@
+<html>
+  <head>
+
+    <script src="smooth/scripts/mootools-1.2.1-core-yc.js" type="text/javascript"></script>
+    <script src="smooth/scripts/mootools-1.2-more.js" type="text/javascript"></script>
+    <script src="smooth/scripts/jd.gallery.js" type="text/javascript"></script> 
+
+    <link rel="stylesheet" href="smooth/css/jd.gallery.css" type="text/css" media="screen" />
+
+  </head>
+  <body>
+
+<script type="text/javascript">
+function startGallery() {
+var myGallery = new gallery($('myGallery'), {
+timed: true
+});
+}
+window.addEvent('domready', startGallery);
+</script> 
+
+    <div id="myGallery">
+
+      <div class="imageElement">
+       <h3>Item 1 Title</h3>
+       <p>Item 1 Description</p>
+<a href="gallery-images/item-1/" title="open image" class="open"></a>
+
+       <img src="http://smoothgallery.jondesign.net/assets/images/samples/77196_6784-gallery.jpg" class="full" alt="Item 1 Title" />
+       <img src="http://smoothgallery.jondesign.net/assets/images/samples/77196_6784-mini.jpg" class="thumbnail" alt="thumbnail of Item 1 Title" />
+      </div>
+
+      <div class="imageElement">
+       <h3>Item 2 Title</h3>
+       <p>Item 2 Description</p>
+<a href="gallery-images/item-1/" title="open image" class="open"></a>
+
+       <img src="http://lh3.ggpht.com/_Pu5idg9RIVo/S1G0Krsr6iI/AAAAAAAAAB8/Hzl-tKkclmo/s640/IMG_0488.jpg" class="full" alt="Item 2 Title" />
+       <img src="http://lh3.ggpht.com/_Pu5idg9RIVo/S1G0Krsr6iI/AAAAAAAAAB8/Hzl-tKkclmo/s128/IMG_0488.jpg" class="thumbnail" alt="thumbnail of Item 2 Title" />
+      </div>
+
+      <div class="imageElement">
+       <h3>Item 2 Title</h3>
+       <p>Item 2 Description</p>
+<a href="gallery-images/item-1/" title="open image" class="open"></a>
+
+       <img src="http://smoothgallery.jondesign.net/assets/images/samples/165392_5486-gallery.jpg" class="full" alt="Item 2 Title" />
+       <img src="http://smoothgallery.jondesign.net/assets/images/samples/165392_5486-mini.jpg" class="thumbnail" alt="thumbnail of Item 2 Title" />
+      </div>
+
+      <div class="imageElement">
+
+       <h3>Item 3 Title</h3>
+       <p>Item 3 Description</p>
+<a href="gallery-images/item-1/" title="open image" class="open"></a>
+
+       <img src="http://smoothgallery.jondesign.net/assets/images/samples/83808_7948-gallery.jpg" class="full" alt="Item 3 Title" />
+       <img src="http://smoothgallery.jondesign.net/assets/images/samples/83808_7948-mini.jpg" class="thumbnail" alt="thumbnail of Item 3 Title" />
+      </div>
+    </div>
+
+
+  </body>
+
+</html>
This page took 0.019196 seconds and 4 git commands to generate.