Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Keeping bad HTML bad by ducky
in thread Keeping bad HTML bad by trs80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found