This is a little snippet to show off the new power of HTML::TokeParser:Simple. The docs are a bit messy, though, so I'll have to clean them up.
You have an HTML document full of stuff like this (note the inconsitent capitalization, quotes, etc.):
<body alink=#0000ff BGCOLOR=#ffffff CLASS='none'> <P>This is <EM>ugly</em> HTML</P>
You have to parse the HTML and while you're at it, you'd rather have it look like this:
<body class="foobar"> <p>This is <em>ugly</em> HTML</p>
use HTML::TokeParser::Simple 2.1; my $parser = HTML::TokeParser::Simple->new( $ugly_html ); while (my $token = $parser->get_token) { rewrite_body_tag($token) if $token->is_start_tag('body'); $token->rewrite_tag; print $token->as_is; } sub rewrite_body_tag { # what could be easier? my $token = shift; $token ->delete_attr('bgcolor') ->delete_attr('alink') ->set_attr('class','foobar'); }
|
|---|