Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Modules or rules to find ill-formatted HTML

by lihao (Monk)
on Jan 28, 2008 at 22:15 UTC ( #664794=perlquestion: print w/ replies, xml ) Need Help??
lihao has asked for the wisdom of the Perl Monks concerning the following question:

Hi, folks:

Are there any Perl modules or rules that I can follow to find ill-formatted HTML tags? something like an HTML validator with Perl engine. Many thanks for your suggestions

lihao(XC)

Comment on Modules or rules to find ill-formatted HTML
Re: Modules or rules to find ill-formatted HTML
by moritz (Cardinal) on Jan 28, 2008 at 22:48 UTC
Re: Modules or rules to find ill-formatted HTML
by naikonta (Curate) on Jan 28, 2008 at 22:54 UTC
    HTML::Validator seems like a candidate but the latest update was in 2000. You may also check HTML::Parser. But I think you want HTML::Tidy that is "(X)HTML validation in a Perl object".

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Modules or rules to find ill-formatted HTML
by ww (Bishop) on Jan 29, 2008 at 01:03 UTC
    A bit more information would be helpful.

    Under what circumstances are you trying to find ill-formatted HTML tags?

    I ask because one case that occurs to me is a situation where you are building or proof-reading raw html on a local machine. If so, using the w3c validator will be easy, free and produce up-to-date results.

    Using Perl to use the w3c validator will be more complex (in that case), but it's doable. Write your script to

    1. read your html file into local memory
    2. connect to w3c, send the html page (won't work if merely a fragment, IIRC) to the validator (it accepts a URI, file upload or direct input (cut'n'paste), and capture the return (with one of the usual suspects - search the Monastery for 'web scraping' for one set of ideas).
    3. display the returned errors, warnings, or 'good to go' message for the user or spit'em out to dead trees or whatever.

    At a minimum, you can expect standard-based validation this way; Tidy has it's own (configurable within limits) set of notions about valid .html and, as noted above, HTML::Validator may have some outdated notions.

      I recently tried cleaning up following HTML and got problem. The end quote was missing on an attribute value. use strict; use warnings; use HTML::Tidy; my $html="<a href=\"mailto:test\@test.net>Email Us"; my $tidy = HTML::Tidy->new(); my $clean = $tidy->clean($html); print "Clean HTML:\n---------------------------\n$clean\n"; This produced incorrect output.

        Unreadable!

        Please use <code>...</code> tags ... and please read Markup in the Monastery

        Now, what you posted appears (XML view) to be intended to render this way:

        I recently tried cleaning up following HTML and got problem. The end quote was missing on an attribute value.
        use strict; use warnings; use HTML::Tidy; my $html="<a href=\"mailto:test\@test.net><font size=4>Email Us</a>"; my $tidy = HTML::Tidy->new(); my $clean = $tidy->clean($html); print "Clean HTML:\n---------------------------\n$clean\n";
        This produced incorrect output.

        So, what was the incorrect output? Please also read How do I post a question effectively?.

        ...and on the offchance you don't know what's wrong with your $html, check the quoting, the missing </font> tag, and the H::T docs.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://664794]
Approved by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-25 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (161 votes), past polls