Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Keeping bad HTML bad

by ducky (Scribe)
on Aug 23, 2002 at 23:40 UTC ( [id://192490]=note: print w/replies, xml ) Need Help??


in reply to Keeping bad HTML bad

After reading your replies to further questions, it sounds like you don't really need something that truely understands html to the fullest, but a parser that groks the concept of text, html tags and their attributes, and html comments.

My suggestion would be to write something that can pick apart those 3 things and do the small edits you need them to and reassemble the file, mal-formedness and all.

If you proceed down the path that the browsers did and try to guess a documents meaning (via your own code or a module), I believe you'll end up far too often fixing html by hand, either on the way in or the way out and much sadness will ensue.

To offer at least something and not leave you with, "Oh ya that sucks. Good luck!" Here's a bit code that might help. It's inefficient, but ok for moderate sized data. (I just happened to run across this yesterday in my playground/lets-see-if-I-can-do-X perl script dir):

sub parse { my $source = shift ; my @output ; while ( $source =~ s/^([<]*)<//s ) { push @output, [ 'text', $1 ] if $1 ne '' ; $source =~ s/^([^\s>]*)([\s>])//s ; my $tag = $1 ; if ( $tag eq '!--' ) { $source =~ s/^(.*?)-->//s ; push @output, [ 'comment', $1 ] ; } else { my $param = '' ; if ( $2 ne '>' ) { while ( $source =~ s/^([^'">]*)(["'>])//s ) { $param .= $1 ; last if $2 eq '>' ; my $quote = $2 ; $source =~ s/^([^$quote]*$quote)// ; $param .= $quote . $1 . $quote ; } } push @output, ['tag', $tag, $param ] ; } } return @output ; }
Keep in mind, it's largely untested, written like 4 years ago mostly as an exercise.

What it does is takes a whole file and returns you an array of array refs. Each array ref contains 2 or 3 elements: what type of data it was (text, tag, comment), the data itself (minus the actual markup in the case of tags), and if the type is a tag, then the 3rd element is tag params.

HTH

-Ducky

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found