home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / HTML / Parse.pm < prev    next >
Text File  |  1996-08-01  |  3KB  |  148 lines

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