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

  1. package HTML::Parse;
  2.  
  3. # $Id: Parse.pm,v 2.4 1997/12/05 16:42:13 aas Exp $
  4.  
  5. =head1 NAME
  6.  
  7. HTML::Parse - Depreciated
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.  use HTML::Parse;
  12.  $h = parse_htmlfile("test.html");
  13.  print $h->dump;
  14.  $h = parse_html("<p>Some more <i>italic</i> text", $h);
  15.  $h->delete;
  16.  
  17.  print parse_htmlfile("index.html")->as_HTML;  # tidy up markup in a file
  18.  
  19. =head1 DESCRIPTION
  20.  
  21. I<Disclaimer: This module is only provided for backwards compatibility
  22. with earlier versions of this library.  New code shold use the
  23. HTML::Parser and HTML::TreeBuilder modules directly.>
  24.  
  25. The C<HTML::Parse> module provides functions to parse HTML documents.
  26. There are two functions exported by this module:
  27.  
  28. =over 4
  29.  
  30. =item parse_html($html, [$obj])
  31.  
  32. This function is really just a synonym for $obj->parse($html) and $obj
  33. is assumed to be a subclass of C<HTML::Parser>.  Refer to
  34. L<HTML::Parser> for more documentation.
  35.  
  36. The $obj will default to an internally created C<HTML::TreeBuilder>
  37. object configured with strict_comment() turned on.  This class
  38. implements a parser that builds (and is) a HTML syntax tree with
  39. HTML::Element objects as nodes.
  40.  
  41. The return value from parse_html() is $obj.
  42.  
  43. =item parse_htmlfile($file, [$obj])
  44.  
  45. Same as parse_html(), but obtains HTML text from the named file.
  46.  
  47. Returns C<undef> if the file could not be opened, or $obj otherwise.
  48.  
  49. =back
  50.  
  51. When a C<HTML::TreeBuilder> object is created, the following variables
  52. control how parsing takes place:
  53.  
  54. =over 4
  55.  
  56. =item $HTML::Parse::IMPLICIT_TAGS
  57.  
  58. Setting this variable to true will instruct the parser to try to
  59. deduce implicit elements and implicit end tags.  If this variable is
  60. false you get a parse tree that just reflects the text as it stands.
  61. Might be useful for quick & dirty parsing.  Default is true.
  62.  
  63. Implicit elements have the implicit() attribute set.
  64.  
  65. =item $HTML::Parse::IGNORE_UNKNOWN
  66.  
  67. This variable contols whether unknow tags should be represented as
  68. elements in the parse tree.  Default is true.
  69.  
  70. =item $HTML::Parse::IGNORE_TEXT
  71.  
  72. Do not represent the text content of elements.  This saves space if
  73. all you want is to examine the structure of the document.  Default is
  74. false.
  75.  
  76. =item $HTML::Parse::WARN
  77.  
  78. Call warn() with an apropriate message for syntax errors.  Default is
  79. false.
  80.  
  81. =back
  82.  
  83. =head1 SEE ALSO
  84.  
  85. L<HTML::Parser>, L<HTML::TreeBuilder>, L<HTML::Element>
  86.  
  87. =head1 COPYRIGHT
  88.  
  89. Copyright 1995-1996 Gisle Aas. All rights reserved.
  90.  
  91. This library is free software; you can redistribute it and/or
  92. modify it under the same terms as Perl itself.
  93.  
  94. =head1 AUTHOR
  95.  
  96. Gisle Aas <aas@sn.no>
  97.  
  98. =cut
  99.  
  100.  
  101. require Exporter;
  102. @ISA = qw(Exporter);
  103. @EXPORT = qw(parse_html parse_htmlfile);
  104.  
  105. use strict;
  106. use vars qw($VERSION
  107.             $IMPLICIT_TAGS $IGNORE_UNKNOWN $IGNORE_TEXT $WARN);
  108.  
  109. # Backwards compatability
  110. $IMPLICIT_TAGS  = 1;
  111. $IGNORE_UNKNOWN = 1;
  112. $IGNORE_TEXT    = 0;
  113. $WARN           = 0;
  114.  
  115. require HTML::TreeBuilder;
  116.  
  117. $VERSION = sprintf("%d.%02d", q$Revision: 2.4 $ =~ /(\d+)\.(\d+)/);
  118.  
  119.  
  120. sub parse_html ($;$)
  121. {
  122.     my $p = $_[1];
  123.     $p = _new_tree_maker() unless $p;
  124.     $p->parse($_[0]);
  125. }
  126.  
  127.  
  128. sub parse_htmlfile ($;$)
  129. {
  130.     my($file, $p) = @_;
  131.     local(*HTML);
  132.     open(HTML, $file) or return undef;
  133.     $p = _new_tree_maker() unless $p;
  134.     $p->parse_file(\*HTML);
  135. }
  136.  
  137. sub _new_tree_maker
  138. {
  139.     my $p = HTML::TreeBuilder->new(implicit_tags  => $IMPLICIT_TAGS,
  140.                         ignore_unknown => $IGNORE_UNKNOWN,
  141.                        ignore_text    => $IGNORE_TEXT,
  142.                    'warn'         => $WARN,
  143.                   );
  144.     $p->strict_comment(1);
  145.     $p;
  146. }
  147.  
  148. 1;
  149.