Debian lenny version packages
[pkg-perl] / deb-src / libhtml-tree-perl / libhtml-tree-perl-3.23 / lib / HTML / Parse.pm
1 package HTML::Parse;
2
3 =head1 NAME
4
5 HTML::Parse - Deprecated, a wrapper around HTML::TreeBuilder
6
7 =head1 SYNOPSIS
8
9   See the documentation for HTML::TreeBuilder
10
11 =head1 DESCRIPTION
12
13 Disclaimer: This module is provided only for backwards compatibility
14 with earlier versions of this library.  New code should I<not> use
15 this module, and should really use the HTML::Parser and
16 HTML::TreeBuilder modules directly, instead.
17
18 The C<HTML::Parse> module provides functions to parse HTML documents.
19 There are two functions exported by this module:
20
21 =over 4
22
23 =item parse_html($html) or parse_html($html, $obj)
24
25 This function is really just a synonym for $obj->parse($html) and $obj
26 is assumed to be a subclass of C<HTML::Parser>.  Refer to
27 L<HTML::Parser> for more documentation.
28
29 If $obj is not specified, the $obj will default to an internally
30 created new C<HTML::TreeBuilder> object configured with strict_comment()
31 turned on.  That class implements a parser that builds (and is) a HTML
32 syntax tree with HTML::Element objects as nodes.
33
34 The return value from parse_html() is $obj.
35
36 =item parse_htmlfile($file, [$obj])
37
38 Same as parse_html(), but pulls the HTML to parse, from the named file.
39
40 Returns C<undef> if the file could not be opened, or $obj otherwise.
41
42 =back
43
44 When a C<HTML::TreeBuilder> object is created, the following variables
45 control how parsing takes place:
46
47 =over 4
48
49 =item $HTML::Parse::IMPLICIT_TAGS
50
51 Setting this variable to true will instruct the parser to try to
52 deduce implicit elements and implicit end tags.  If this variable is
53 false you get a parse tree that just reflects the text as it stands.
54 Might be useful for quick & dirty parsing.  Default is true.
55
56 Implicit elements have the implicit() attribute set.
57
58 =item $HTML::Parse::IGNORE_UNKNOWN
59
60 This variable contols whether unknow tags should be represented as
61 elements in the parse tree.  Default is true.
62
63 =item $HTML::Parse::IGNORE_TEXT
64
65 Do not represent the text content of elements.  This saves space if
66 all you want is to examine the structure of the document.  Default is
67 false.
68
69 =item $HTML::Parse::WARN
70
71 Call warn() with an apropriate message for syntax errors.  Default is
72 false.
73
74 =back
75
76 =head1 REMEMBER!
77
78 HTML::TreeBuilder objects should be explicitly destroyed when you're
79 finished with them.  See L<HTML::TreeBuilder>.
80
81 =head1 SEE ALSO
82
83 L<HTML::Parser>, L<HTML::TreeBuilder>, L<HTML::Element>
84
85 =head1 COPYRIGHT
86
87 Copyright 1995-1998 Gisle Aas, 1999-2004 Sean M. Burke, 2005 Andy Lester,
88 2006 Pete Krawczyk.
89
90 This library is free software; you can redistribute it and/or
91 modify it under the same terms as Perl itself.
92
93 This program is distributed in the hope that it will be useful, but
94 without any warranty; without even the implied warranty of
95 merchantability or fitness for a particular purpose.
96
97 =head1 AUTHOR
98
99 Currently maintained by Pete Krawczyk C<< <petek@cpan.org> >>
100
101 Original authors: Gisle Aas, Sean Burke and Andy Lester.
102
103 =cut
104
105
106 require Exporter;
107 @ISA = qw(Exporter);
108 @EXPORT = qw(parse_html parse_htmlfile);
109
110 use strict;
111 use vars qw($VERSION
112             $IMPLICIT_TAGS $IGNORE_UNKNOWN $IGNORE_TEXT $WARN
113            );
114
115 # Backwards compatability
116 $IMPLICIT_TAGS  = 1;
117 $IGNORE_UNKNOWN = 1;
118 $IGNORE_TEXT    = 0;
119 $WARN           = 0;
120
121 require HTML::TreeBuilder;
122
123 $VERSION = '2.71';
124
125
126 sub parse_html ($;$)
127 {
128     my $p = $_[1];
129     $p = _new_tree_maker() unless $p;
130     $p->parse($_[0]);
131 }
132
133
134 sub parse_htmlfile ($;$)
135 {
136     my($file, $p) = @_;
137     local(*HTML);
138     open(HTML, $file) or return undef;
139     $p = _new_tree_maker() unless $p;
140     $p->parse_file(\*HTML);
141 }
142
143 sub _new_tree_maker
144 {
145     my $p = HTML::TreeBuilder->new(
146       implicit_tags  => $IMPLICIT_TAGS,
147       ignore_unknown => $IGNORE_UNKNOWN,
148       ignore_text    => $IGNORE_TEXT,
149       'warn'         => $WARN,
150     );
151     $p->strict_comment(1);
152     $p;
153 }
154
155 1;