home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Perl_Libs / site_perl / HTML / Filter.pm < prev    next >
Text File  |  1997-12-12  |  3KB  |  110 lines

  1. package HTML::Filter;
  2.  
  3. require HTML::Parser;
  4. @ISA=qw(HTML::Parser);
  5.  
  6. $VERSION = sprintf("%d.%02d", q$Revision: 2.4 $ =~ /(\d+)\.(\d+)/);
  7.  
  8. sub declaration { $_[0]->output("<!$_[1]>")     }
  9. sub comment     { $_[0]->output("<!--$_[1]-->") }
  10. sub start       { $_[0]->output($_[4])          }
  11. sub end         { $_[0]->output($_[2])          }
  12. sub text        { $_[0]->output($_[1])          }
  13.  
  14. sub output      { print $_[1] }
  15.  
  16. 1;
  17.  
  18. __END__
  19.  
  20. =head1 NAME
  21.  
  22. HTML::Filter - Filter HTML text through the parser
  23.  
  24. =head1 SYNOPSIS
  25.  
  26.  require HTML::Filter;
  27.  $p = HTML::Filter->new->parse_file("index.html");
  28.  
  29. =head1 DESCRIPTION
  30.  
  31. The I<HTML::Filter> is an HTML parser that by default prints the
  32. original text parsed (a slow version of cat(1) basically).  You can
  33. override the callback methods to modify the filtering for some of the
  34. HTML elements and you can override output() method which is called to
  35. print the HTML text.
  36.  
  37. The I<HTML::Filter> is a subclass of I<HTML::Parser>. This means that
  38. the document should be given to the parser by calling the $p->parse()
  39. or $p->parse_file() methods.
  40.  
  41. =head1 EXAMPLES
  42.  
  43. The first example is a filter that will remove all comments from an
  44. HTML file.  This is achieved by simply overriding the comment method
  45. to do nothing.
  46.  
  47.   package CommentStripper;
  48.   require HTML::Filter;
  49.   @ISA=qw(HTML::Filter);
  50.   sub comment { }  # ignore comments
  51.  
  52. The second example shows a filter that will remove any E<lt>TABLE>s
  53. found in the HTML file.  We specialize the start() and end() methods
  54. to count table tags and then make output not happen when inside a
  55. table.
  56.  
  57.   package TableStripper;
  58.   require HTML::Filter;
  59.   @ISA=qw(HTML::Filter);
  60.   sub start
  61.   {
  62.      my $self = shift;
  63.      $self->{table_seen}++ if $_[0] eq "table";
  64.      $self->SUPER::start(@_);
  65.   }  
  66.  
  67.   sub end
  68.   {
  69.      my $self = shift;
  70.      $self->SUPER::end(@_);
  71.      $self->{table_seen}-- if $_[0] eq "table";
  72.   }
  73.  
  74.   sub output
  75.   {
  76.       my $self = shift;
  77.       unless ($self->{table_seen}) {
  78.       $self->SUPER::output(@_);
  79.       }
  80.   }
  81.  
  82. If you want to collect the parsed text internally you might want to do
  83. something like this:
  84.  
  85.   package FilterIntoString;
  86.   require HTML::Filter;
  87.   @ISA=qw(HTML::Filter);
  88.   sub output { push(@{$_[0]->{fhtml}}, $_[1]) }
  89.   sub filtered_html { join("", @{$_[0]->{fhtml}}) }
  90.  
  91. =head1 BUGS
  92.  
  93. Comments in declarations are removed from the declarations and then
  94. inserted as separate comments after the declaration.  If you turn on
  95. strict_comment(), then comments with embedded "--" are split into
  96. multiple comments.
  97.  
  98. =head1 SEE ALSO
  99.  
  100. L<HTML::Parser>
  101.  
  102. =head1 COPYRIGHT
  103.  
  104. Copyright 1997 Gisle Aas.
  105.  
  106. This library is free software; you can redistribute it and/or
  107. modify it under the same terms as Perl itself.
  108.  
  109. =cut
  110.