1 package HTML::TokeParser;
3 # $Id: TokeParser.pm,v 2.37 2006/04/26 08:00:28 gisle Exp $
5 require HTML::PullParser;
6 @ISA=qw(HTML::PullParser);
7 $VERSION = sprintf("%d.%02d", q$Revision: 2.37 $ =~ /(\d+)\.(\d+)/);
11 use HTML::Entities qw(decode_entities);
16 start => "'S',tagname,attr,attrseq,text",
17 end => "'E',tagname,text",
18 text => "'T',text,is_cdata",
19 process => "'PI',token0,text",
20 comment => "'C',text",
21 declaration => "'D',text",
23 # options that default on
33 my $type = (ref($_[0]) eq "SCALAR") ? "doc" : "file";
34 %cnf = ($type => $_[0]);
40 my $textify = delete $cnf{textify} || {img => "alt", applet => "alt"};
42 my $self = $class->SUPER::new(%cnf, %ARGS) || return undef;
44 $self->{textify} = $textify;
54 $token = $self->get_token || return undef;
55 my $type = shift @$token;
56 next unless $type eq "S" || $type eq "E";
57 substr($token->[0], 0, 0) = "/" if $type eq "E";
58 return $token unless @_;
60 return $token if $token->[0] eq $_;
67 my($self, $token) = @_;
68 my $tag = $token->[1];
69 return undef unless exists $self->{textify}{$tag};
71 my $alt = $self->{textify}{$tag};
74 $text = &$alt(@$token);
76 $text = $token->[2]{$alt || "alt"};
77 $text = "[\U$tag]" unless defined $text;
87 while (my $token = $self->get_token) {
88 my $type = $token->[0];
90 my $text = $token->[1];
91 decode_entities($text) unless $token->[2];
93 } elsif ($type =~ /^[SE]$/) {
94 my $tag = $token->[1];
96 if (defined(my $text = _textify($self, $token))) {
103 if (!@_ || grep $_ eq $tag, @_) {
104 $self->unget_token($token);
108 if $tag eq "br" || !$HTML::Tagset::isPhraseMarkup{$token->[1]};
118 my $text = $self->get_text(@_);
119 $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g;
126 while (my $token = $self->get_token) {
127 my $type = $token->[0];
129 my $text = $token->[1];
130 decode_entities($text) unless $token->[2];
132 } elsif ($type =~ /^[SE]$/) {
133 my $tag = $token->[1];
135 if (defined(my $text = _textify($self, $token))) {
140 if (!$HTML::Tagset::isPhraseMarkup{$tag}) {
141 $self->unget_token($token);
144 push(@text, " ") if $tag eq "br";
147 my $text = join("", @text);
148 $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g;
159 HTML::TokeParser - Alternative HTML::Parser interface
163 require HTML::TokeParser;
164 $p = HTML::TokeParser->new("index.html") ||
165 die "Can't open: $!";
166 $p->empty_element_tags(1); # configure its behaviour
168 while (my $token = $p->get_token) {
174 The C<HTML::TokeParser> is an alternative interface to the
175 C<HTML::Parser> class. It is an C<HTML::PullParser> subclass with a
176 predeclared set of token types. If you wish the tokens to be reported
177 differently you probably want to use the C<HTML::PullParser> directly.
179 The following methods are available:
183 =item $p = HTML::TokeParser->new( $filename, %opt );
185 =item $p = HTML::TokeParser->new( $filehandle, %opt );
187 =item $p = HTML::TokeParser->new( \$document, %opt );
189 The object constructor argument is either a file name, a file handle
190 object, or the complete document to be parsed. Extra options can be
191 provided as key/value pairs and are processed as documented by the base
194 If the argument is a plain scalar, then it is taken as the name of a
195 file to be opened and parsed. If the file can't be opened for
196 reading, then the constructor will return C<undef> and $! will tell
199 If the argument is a reference to a plain scalar, then this scalar is
200 taken to be the literal document to parse. The value of this
201 scalar should not be changed before all tokens have been extracted.
203 Otherwise the argument is taken to be some object that the
204 C<HTML::TokeParser> can read() from when it needs more data. Typically
205 it will be a filehandle of some kind. The stream will be read() until
208 A newly constructed C<HTML::TokeParser> differ from its base classes
209 by having the C<unbroken_text> attribute enabled by default. See
210 L<HTML::Parser> for a description of this and other attributes that
211 influence how the document is parsed. It is often a good idea to enable
212 C<empty_element_tags> behaviour.
214 Note that the parsing result will likely not be valid if raw undecoded
215 UTF-8 is used as a source. When parsing UTF-8 encoded files turn
218 open(my $fh, "<:utf8", "index.html") || die "Can't open 'index.html': $!";
219 my $p = HTML::TokeParser->new( $fh );
222 If a $filename is passed to the constructor the file will be opened in
223 raw mode and the parsing result will only be valid if its content is
224 Latin-1 or pure ASCII.
226 If parsing from an UTF-8 encoded string buffer decode it first:
228 utf8::decode($document);
229 my $p = HTML::TokeParser->new( \$document );
234 This method will return the next I<token> found in the HTML document,
235 or C<undef> at the end of the document. The token is returned as an
236 array reference. The first element of the array will be a string
237 denoting the type of this token: "S" for start tag, "E" for end tag,
238 "T" for text, "C" for comment, "D" for declaration, and "PI" for
239 process instructions. The rest of the token array depend on the type
242 ["S", $tag, $attr, $attrseq, $text]
244 ["T", $text, $is_data]
247 ["PI", $token0, $text]
249 where $attr is a hash reference, $attrseq is an array reference and
250 the rest are plain scalars. The L<HTML::Parser/Argspec> explains the
253 =item $p->unget_token( @tokens )
255 If you find you have read too many tokens you can push them back,
256 so that they are returned the next time $p->get_token is called.
260 =item $p->get_tag( @tags )
262 This method returns the next start or end tag (skipping any other
263 tokens), or C<undef> if there are no more tags in the document. If
264 one or more arguments are given, then we skip tokens until one of the
265 specified tag types is found. For example:
267 $p->get_tag("font", "/font");
269 will find the next start or end tag for a font-element.
271 The tag information is returned as an array reference in the same form
272 as for $p->get_token above, but the type code (first element) is
273 missing. A start tag will be returned like this:
275 [$tag, $attr, $attrseq, $text]
277 The tagname of end tags are prefixed with "/", i.e. end tag is
284 =item $p->get_text( @endtags )
286 This method returns all text found at the current position. It will
287 return a zero length string if the next token is not text. Any
288 entities will be converted to their corresponding character.
290 If one or more arguments are given, then we return all text occurring
291 before the first of the specified tags found. For example:
293 $p->get_text("p", "br");
295 will return the text up to either a paragraph of linebreak element.
297 The text might span tags that should be I<textified>. This is
298 controlled by the $p->{textify} attribute, which is a hash that
299 defines how certain tags can be treated as text. If the name of a
300 start tag matches a key in this hash then this tag is converted to
301 text. The hash value is used to specify which tag attribute to obtain
302 the text from. If this tag attribute is missing, then the upper case
303 name of the tag enclosed in brackets is returned, e.g. "[IMG]". The
304 hash value can also be a subroutine reference. In this case the
305 routine is called with the start tag token content as its argument and
306 the return value is treated as the text.
308 The default $p->{textify} value is:
310 {img => "alt", applet => "alt"}
312 This means that <IMG> and <APPLET> tags are treated as text, and that
313 the text to substitute can be found in the ALT attribute.
315 =item $p->get_trimmed_text
317 =item $p->get_trimmed_text( @endtags )
319 Same as $p->get_text above, but will collapse any sequences of white
320 space to a single space character. Leading and trailing white space is
325 This will return all text found at the current position ignoring any
326 phrasal-level tags. Text is extracted until the first non
327 phrasal-level tag. Textification of tags is the same as for
328 get_text(). This method will collapse white space in the same way as
329 get_trimmed_text() does.
331 The definition of <i>phrasal-level tags</i> is obtained from the
338 This example extracts all links from a document. It will print one
339 line for each link, containing the URL and the textual description
340 between the <A>...</A> tags:
342 use HTML::TokeParser;
343 $p = HTML::TokeParser->new(shift||"index.html");
345 while (my $token = $p->get_tag("a")) {
346 my $url = $token->[1]{href} || "-";
347 my $text = $p->get_trimmed_text("/a");
348 print "$url\t$text\n";
351 This example extract the <TITLE> from the document:
353 use HTML::TokeParser;
354 $p = HTML::TokeParser->new(shift||"index.html");
355 if ($p->get_tag("title")) {
356 my $title = $p->get_trimmed_text;
357 print "Title: $title\n";
362 L<HTML::PullParser>, L<HTML::Parser>
366 Copyright 1998-2005 Gisle Aas.
368 This library is free software; you can redistribute it and/or
369 modify it under the same terms as Perl itself.