agaffney has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; use HTML::Parser (); my $htmltree = [ { tag => 'document', content => [] } ]; my $node = $htmltree->[0]->{content}; my @prevnodes = ($htmltree); sub start { my $tagname = shift; my $attr = shift; my $newnode = {}; $newnode->{tag} = $tagname; foreach my $key(keys %{$attr}) { $newnode->{$key} = $attr->{$key}; } $newnode->{content} = []; push @prevnodes, $node; push @{$node}, $newnode; $node = $newnode->{content}; } sub end { my $tagname = shift; $node = pop @prevnodes; } sub text { my $text = shift; chomp $text; if($text ne '') { push @{$node}, $text; } } my $p = HTML::Parser->new( api_version => 3, start_h => [\&start, "tagname, attr"], end_h => [\&end, "tagname"], text_h => [\&text, "dtext"] ); $p->parse_file("test.html");
Now for the next challenge. I need to be able to know where I am in the tree structure for any node that I am in while I am walking it. I will pass along a value via CGI in the form of '0.0.2.1.2' which another script will translate as: '$htmltree->[0]->{content}->[0]->{content}->[2]->{content}->[1]->{content}->[2]'. Using the above code, and the following code I wrote for walking the tree and generating HTML from it, how can I mark each outputted HTML tag with its position in the tree?<table id="maintable" width="300"> <tr> <td width="200">some content</td> <td width="100">more content</td> </tr> </table>
sub descend_htmltree { my $node = shift; my $withclickiness = shift || 0; foreach my $tmpnode (@{$node}) { if(ref($tmpnode) eq 'HASH') { my $nodeid = ""; # Magic code to generate node's position in tre +e $htmloutput .= "<div style='border: thin solid #bbbbbb' onDblCli +ck=\"alert('you clicked $nodeid')\">" if($withclickiness); $htmloutput .= "<$tmpnode->{tag}"; foreach(keys %{$tmpnode}) { $htmloutput .= " $_=\"$tmpnode->{$_}\"" if($_ ne 'tag' && $_ n +e 'content'); } $htmloutput .= ">"; descend_htmltree($tmpnode->{content}); $htmloutput .= "</$tmpnode->{tag}>"; $htmloutput .= "</div>" if($withclickiness); } else { $htmloutput .= "$tmpnode"; } } } sub htmltree_to_html { my $filename = shift || ''; my $withclickiness = shift || 0; descend_htmltree($htmltree->[0]->{content}, $withclickiness); if($filename ne '') { open HTML, "> $filename" or die "Can't open $filename for HTML out +put"; print HTML $htmloutput; close HTML; } return $htmloutput; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: tracking where I am in a tree structure
by Fletch (Bishop) on Jul 22, 2004 at 19:48 UTC | |
by agaffney (Beadle) on Jul 22, 2004 at 19:50 UTC | |
|
Re: tracking where I am in a tree structure
by stvn (Monsignor) on Jul 22, 2004 at 21:49 UTC | |
by agaffney (Beadle) on Jul 22, 2004 at 22:07 UTC | |
by stvn (Monsignor) on Jul 22, 2004 at 22:20 UTC | |
by agaffney (Beadle) on Jul 23, 2004 at 03:26 UTC | |
by stvn (Monsignor) on Jul 23, 2004 at 04:07 UTC | |
by FoxtrotUniform (Prior) on Jul 23, 2004 at 01:38 UTC | |
by stvn (Monsignor) on Jul 23, 2004 at 03:50 UTC |