home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _8ff4fd5445557b2c12205692db7c31b0 < prev    next >
Encoding:
Text File  |  2004-06-01  |  9.1 KB  |  342 lines

  1. package HTML::TokeParser;
  2.  
  3. # $Id: TokeParser.pm,v 2.28 2003/10/14 10:11:05 gisle Exp $
  4.  
  5. require HTML::PullParser;
  6. @ISA=qw(HTML::PullParser);
  7. $VERSION = sprintf("%d.%02d", q$Revision: 2.28 $ =~ /(\d+)\.(\d+)/);
  8.  
  9. use strict;
  10. use Carp ();
  11. use HTML::Entities qw(decode_entities);
  12. use HTML::Tagset ();
  13.  
  14. my %ARGS =
  15. (
  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",
  22. );
  23.  
  24.  
  25. sub new
  26. {
  27.     my $class = shift;
  28.     my %cnf;
  29.     if (@_ == 1) {
  30.     my $type = (ref($_[0]) eq "SCALAR") ? "doc" : "file";
  31.     %cnf = ($type => $_[0]);
  32.     }
  33.     else {
  34.     %cnf = @_;
  35.     }
  36.  
  37.     my $textify = delete $cnf{textify} || {img => "alt", applet => "alt"};
  38.  
  39.     my $self = $class->SUPER::new(%cnf, %ARGS) || return undef;
  40.  
  41.     $self->{textify} = $textify;
  42.     $self;
  43. }
  44.  
  45.  
  46. sub get_tag
  47. {
  48.     my $self = shift;
  49.     my $token;
  50.     while (1) {
  51.     $token = $self->get_token || return undef;
  52.     my $type = shift @$token;
  53.     next unless $type eq "S" || $type eq "E";
  54.     substr($token->[0], 0, 0) = "/" if $type eq "E";
  55.     return $token unless @_;
  56.     for (@_) {
  57.         return $token if $token->[0] eq $_;
  58.     }
  59.     }
  60. }
  61.  
  62.  
  63. sub _textify {
  64.     my($self, $token) = @_;
  65.     my $tag = $token->[1];
  66.     return undef unless exists $self->{textify}{$tag};
  67.  
  68.     my $alt = $self->{textify}{$tag};
  69.     my $text;
  70.     if (ref($alt)) {
  71.     $text = &$alt(@$token);
  72.     } else {
  73.     $text = $token->[2]{$alt || "alt"};
  74.     $text = "[\U$tag]" unless defined $text;
  75.     }
  76.     return $text;
  77. }
  78.  
  79.  
  80. sub get_text
  81. {
  82.     my $self = shift;
  83.     my @text;
  84.     while (my $token = $self->get_token) {
  85.     my $type = $token->[0];
  86.     if ($type eq "T") {
  87.         my $text = $token->[1];
  88.         decode_entities($text) unless $token->[2];
  89.         push(@text, $text);
  90.     } elsif ($type =~ /^[SE]$/) {
  91.         my $tag = $token->[1];
  92.         if ($type eq "S") {
  93.         if (defined(my $text = _textify($self, $token))) {
  94.             push(@text, $text);
  95.             next;
  96.         }
  97.         } else {
  98.         $tag = "/$tag";
  99.         }
  100.         if (!@_ || grep $_ eq $tag, @_) {
  101.          $self->unget_token($token);
  102.          last;
  103.         }
  104.         push(@text, " ")
  105.         if $tag eq "br" || !$HTML::Tagset::isPhraseMarkup{$token->[1]};
  106.     }
  107.     }
  108.     join("", @text);
  109. }
  110.  
  111.  
  112. sub get_trimmed_text
  113. {
  114.     my $self = shift;
  115.     my $text = $self->get_text(@_);
  116.     $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g;
  117.     $text;
  118. }
  119.  
  120. sub get_phrase {
  121.     my $self = shift;
  122.     my @text;
  123.     while (my $token = $self->get_token) {
  124.     my $type = $token->[0];
  125.     if ($type eq "T") {
  126.         my $text = $token->[1];
  127.         decode_entities($text) unless $token->[2];
  128.         push(@text, $text);
  129.     } elsif ($type =~ /^[SE]$/) {
  130.         my $tag = $token->[1];
  131.         if ($type eq "S") {
  132.         if (defined(my $text = _textify($self, $token))) {
  133.             push(@text, $text);
  134.             next;
  135.         }
  136.         }
  137.         if (!$HTML::Tagset::isPhraseMarkup{$tag}) {
  138.         $self->unget_token($token);
  139.         last;
  140.         }
  141.         push(@text, " ") if $tag eq "br";
  142.     }
  143.     }
  144.     my $text = join("", @text);
  145.     $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g;
  146.     $text;
  147. }
  148.  
  149. 1;
  150.  
  151.  
  152. __END__
  153.  
  154. =head1 NAME
  155.  
  156. HTML::TokeParser - Alternative HTML::Parser interface
  157.  
  158. =head1 SYNOPSIS
  159.  
  160.  require HTML::TokeParser;
  161.  $p = HTML::TokeParser->new("index.html") ||
  162.       die "Can't open: $!";
  163.  
  164.  while (my $token = $p->get_token) {
  165.      #...
  166.  }
  167.  
  168. =head1 DESCRIPTION
  169.  
  170. The C<HTML::TokeParser> is an alternative interface to the
  171. C<HTML::Parser> class.  It is an C<HTML::PullParser> subclass with a
  172. predeclared set of token types.  If you wish the tokens to be reported
  173. differently you probably want to use the C<HTML::PullParser> directly.
  174.  
  175. The following methods are available:
  176.  
  177. =over 4
  178.  
  179. =item $p = HTML::TokeParser->new( $filename );
  180.  
  181. =item $p = HTML::TokeParser->new( $filehandle );
  182.  
  183. =item $p = HTML::TokeParser->new( \$document );
  184.  
  185. The object constructor argument is either a file name, a file handle
  186. object, or the complete document to be parsed.
  187.  
  188. If the argument is a plain scalar, then it is taken as the name of a
  189. file to be opened and parsed.  If the file can't be opened for
  190. reading, then the constructor will return an undefined value and $!
  191. will tell you why it failed.
  192.  
  193. If the argument is a reference to a plain scalar, then this scalar is
  194. taken to be the literal document to parse.  The value of this
  195. scalar should not be changed before all tokens have been extracted.
  196.  
  197. Otherwise the argument is taken to be some object that the
  198. C<HTML::TokeParser> can read() from when it needs more data.  Typically
  199. it will be a filehandle of some kind.  The stream will be read() until
  200. EOF, but not closed.
  201.  
  202. =item $p->get_token
  203.  
  204. This method will return the next I<token> found in the HTML document,
  205. or C<undef> at the end of the document.  The token is returned as an
  206. array reference.  The first element of the array will be a string
  207. denoting the type of this token: "S" for start tag, "E" for end tag,
  208. "T" for text, "C" for comment, "D" for declaration, and "PI" for
  209. process instructions.  The rest of the token array depend on the type
  210. like this:
  211.  
  212.   ["S",  $tag, $attr, $attrseq, $text]
  213.   ["E",  $tag, $text]
  214.   ["T",  $text, $is_data]
  215.   ["C",  $text]
  216.   ["D",  $text]
  217.   ["PI", $token0, $text]
  218.  
  219. where $attr is a hash reference, $attrseq is an array reference and
  220. the rest are plain scalars.  The L<HTML::Parser/Attrspec> explains the
  221. details.
  222.  
  223. =item $p->unget_token( @tokens )
  224.  
  225. If you find you have read too many tokens you can push them back,
  226. so that they are returned the next time $p->get_token is called.
  227.  
  228. =item $p->get_tag
  229.  
  230. =item $p->get_tag( @tags )
  231.  
  232. This method returns the next start or end tag (skipping any other
  233. tokens), or C<undef> if there are no more tags in the document.  If
  234. one or more arguments are given, then we skip tokens until one of the
  235. specified tag types is found.  For example:
  236.  
  237.    $p->get_tag("font", "/font");
  238.  
  239. will find the next start or end tag for a font-element.
  240.  
  241. The tag information is returned as an array reference in the same form
  242. as for $p->get_token above, but the type code (first element) is
  243. missing. A start tag will be returned like this:
  244.  
  245.   [$tag, $attr, $attrseq, $text]
  246.  
  247. The tagname of end tags are prefixed with "/", i.e. end tag is
  248. returned like this:
  249.  
  250.   ["/$tag", $text]
  251.  
  252. =item $p->get_text
  253.  
  254. =item $p->get_text( @endtags )
  255.  
  256. This method returns all text found at the current position. It will
  257. return a zero length string if the next token is not text. Any
  258. entities will be converted to their corresponding character.
  259.  
  260. If one or more arguments are given, then we return all text occurring
  261. before the first of the specified tags found. For example:
  262.  
  263.    $p->get_text("p", "br");
  264.  
  265. will return the text up to either a paragraph of linebreak element.
  266.  
  267. The text might span tags that should be I<textified>.  This is
  268. controlled by the $p->{textify} attribute, which is a hash that
  269. defines how certain tags can be treated as text.  If the name of a
  270. start tag matches a key in this hash then this tag is converted to
  271. text.  The hash value is used to specify which tag attribute to obtain
  272. the text from.  If this tag attribute is missing, then the upper case
  273. name of the tag enclosed in brackets is returned, e.g. "[IMG]".  The
  274. hash value can also be a subroutine reference.  In this case the
  275. routine is called with the start tag token content as its argument and
  276. the return value is treated as the text.
  277.  
  278. The default $p->{textify} value is:
  279.  
  280.   {img => "alt", applet => "alt"}
  281.  
  282. This means that <IMG> and <APPLET> tags are treated as text, and that
  283. the text to substitute can be found in the ALT attribute.
  284.  
  285. =item $p->get_trimmed_text
  286.  
  287. =item $p->get_trimmed_text( @endtags )
  288.  
  289. Same as $p->get_text above, but will collapse any sequences of white
  290. space to a single space character.  Leading and trailing white space is
  291. removed.
  292.  
  293. =item $p->get_phrase
  294.  
  295. This will return all text found at the current position ignoring any
  296. phrasal-level tags.  Text is extracted until the first non
  297. phrasal-level tag.  Textification of tags is the same as for
  298. get_text().  This method will collapse white space in the same way as
  299. get_trimmed_text() does.
  300.  
  301. The definition of <i>phrasal-level tags</i> is obtained from the
  302. HTML::Tagset module.
  303.  
  304. =back
  305.  
  306. =head1 EXAMPLES
  307.  
  308. This example extracts all links from a document.  It will print one
  309. line for each link, containing the URL and the textual description
  310. between the <A>...</A> tags:
  311.  
  312.   use HTML::TokeParser;
  313.   $p = HTML::TokeParser->new(shift||"index.html");
  314.  
  315.   while (my $token = $p->get_tag("a")) {
  316.       my $url = $token->[1]{href} || "-";
  317.       my $text = $p->get_trimmed_text("/a");
  318.       print "$url\t$text\n";
  319.   }
  320.  
  321. This example extract the <TITLE> from the document:
  322.  
  323.   use HTML::TokeParser;
  324.   $p = HTML::TokeParser->new(shift||"index.html");
  325.   if ($p->get_tag("title")) {
  326.       my $title = $p->get_trimmed_text;
  327.       print "Title: $title\n";
  328.   }
  329.  
  330. =head1 SEE ALSO
  331.  
  332. L<HTML::PullParser>, L<HTML::Parser>
  333.  
  334. =head1 COPYRIGHT
  335.  
  336. Copyright 1998-2001 Gisle Aas.
  337.  
  338. This library is free software; you can redistribute it and/or
  339. modify it under the same terms as Perl itself.
  340.  
  341. =cut
  342.