Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Using HTML::Parser to edit files in place

by rpc (Monk)
on Mar 01, 2001 at 04:46 UTC ( [id://61479]=note: print w/replies, xml ) Need Help??


in reply to Using HTML::Parser to edit files in place

The logic would be like this:

  1. Set up a default handler (default_h) which has access to the tag name and unparsed HTML.
  2. Set up a callback to look for table cells.
  3. If you haven't found a table cell, spit out the unparsed HTML.
  4. If you have found the start tag of a table cell, set a flag. You know any concurrent calls to your callback will be content within the table cell, until you hit a closing tag.

use HTML::Parser; my $found = 0; sub callback { my($tagname, $text) = @_; if($tagname and $tagname eq 'td') { if(not $found) { # start tag. $found = 1; } else { # end tag. $found = 0; } return; } print $text and return unless $found; # If you're here, then the markup is within the td tags. } my $p = HTML::Parser->new(api_version => 3, default_h => [\&callback,'tagname, text']);

Hope this helps..

Replies are listed 'Best First'.
Re: Re: Using HTML::Parser to edit files in place
by merlyn (Sage) on Mar 01, 2001 at 05:47 UTC
    This is starting in the right direction, but it breaks on unbalanced-but-legal TD tags. (And you guys all wonder why XML always has to be properly balanced? Because writing code with optional tags is a royal pain.)

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://61479]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found