HTML-TokeParser-Simple

 view release on metacpan or  search on metacpan

lib/HTML/TokeParser/Simple.pm  view on Meta::CPAN

 my @html_docs  = glob( "*.html" );

 foreach my $doc ( @html_docs ) {
     print "Processing $doc\n";
     my $new_file = "$new_folder$doc";

     open PHB, "> $new_file" or die "Cannot open $new_file for writing: $!";

     my $p = HTML::TokeParser::Simple->new( $file => doc );
     while ( my $token = $p->get_token ) {
         next if $token->is_comment;
         print PHB $token->as_is;
     }
     close PHB;
 }

=head2 Changing form tags

Your company was foo.com and now is bar.com.  Unfortunately, whoever wrote your
HTML decided to hardcode "http://www.foo.com/" into the C<action> attribute of
the form tags.  You need to change it to "http://www.bar.com/".

 use strict;
 use HTML::TokeParser::Simple;

 my $new_folder = 'new_html/';
 my @html_docs  = glob( "*.html" );

 foreach my $doc ( @html_docs ) {
     print "Processing $doc\n";
     my $new_file = "$new_folder$doc";

     open FILE, "> $new_file" or die "Cannot open $new_file for writing: $!";

     my $p = HTML::TokeParser::Simple->new( file => $doc );
     while ( my $token = $p->get_token ) {
         if ( $token->is_start_tag('form') ) {
             my $action = $token->get_attr(action);
             $action =~ s/www\.foo\.com/www.bar.com/;
             $token->set_attr('action', $action);
         }
         print FILE $token->as_is;
     }
     close FILE;
 }

=head1 CAVEATS

For compatibility reasons with C<HTML::TokeParser>, methods that return
references are violating encapsulation and altering the references directly
B<will> alter the state of the object.  Subsequent calls to C<rewrite_tag()>
can thus have unexpected results.  Do not alter these references directly
unless you are following behavior described in these docs.  In the future,
certain methods such as C<get_attr>, C<get_attrseq> and others may return a
copy of the reference rather than the original reference.  This behavior has
not yet been changed in order to maintain compatibility with previous versions
of this module.  At the present time, your author is not aware of anyone taking
advantage of this "feature," but it's better to be safe than sorry.

Use of C<$HTML::Parser::VERSION> which is less than 3.25 may result in
incorrect behavior as older versions do not always handle XHTML correctly.  It
is the programmer's responsibility to verify that the behavior of this code
matches the programmer's needs.

Note that C<HTML::Parser> processes text in 512 byte chunks.  This sometimes
will cause strange behavior and cause text to be broken into more than one
token.  You can suppress this behavior with the following command:

 $p->unbroken_text( [$bool] );

See the C<HTML::Parser> documentation and
http://www.perlmonks.org/index.pl?node_id=230667 for more information.

=head1 BUGS

There are no known bugs, but that's no guarantee.

Address bug reports and comments to: E<lt>eop_divo_sitruc@yahoo.comE<gt>.  When
sending bug reports, please provide the version of C<HTML::Parser>,
C<HTML::TokeParser>, C<HTML::TokeParser::Simple>, the version of Perl, and the
version of the operating system you are using.

Reverse the name to email the author.

=head1 SUBCLASSING

You may wish to change the behavior of this module.  You probably do not want
to subclass C<HTML::TokeParser::Simple>.  Instead, you'll want to subclass one
of the token classes.  C<HTML::TokeParser::Simple::Token> is the base class for
all tokens.  Global behavioral changes should go there.  Otherwise, see the
appropriate token class for the behavior you wish to alter.

=head1 SEE ALSO

L<HTML::TokeParser::Simple::Token>

L<HTML::TokeParser::Simple::Token::Tag>

L<HTML::TokeParser::Simple::Token::Text>

L<HTML::TokeParser::Simple::Token::Comment>

L<HTML::TokeParser::Simple::Token::Declaration>

L<HTML::TokeParser::Simple::Token::ProcessInstruction>

=head1 COPYRIGHT

Copyright (c) 2004 by Curtis "Ovid" Poe.  All rights reserved.  This program is
free software; you may redistribute it and/or modify it under the same terms as
Perl itself

=head1 AUTHOR

Curtis "Ovid" Poe E<lt>eop_divo_sitruc@yahoo.comE<gt>

Reverse the name to email the author.

=cut



( run in 1.977 second using v1.01-cache-2.11-cpan-119454b85a5 )