Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
7.4 KiB

  1. package HTML::TokeParser;
  2. # $Id: TokeParser.pm,v 2.24 2001/03/26 07:32:17 gisle Exp $
  3. require HTML::PullParser;
  4. @ISA=qw(HTML::PullParser);
  5. $VERSION = sprintf("%d.%02d", q$Revision: 2.24 $ =~ /(\d+)\.(\d+)/);
  6. use strict;
  7. use Carp ();
  8. use HTML::Entities qw(decode_entities);
  9. my %ARGS =
  10. (
  11. start => "'S',tagname,attr,attrseq,text",
  12. end => "'E',tagname,text",
  13. text => "'T',text,is_cdata",
  14. process => "'PI',token0,text",
  15. comment => "'C',text",
  16. declaration => "'D',text",
  17. );
  18. sub new
  19. {
  20. my $class = shift;
  21. my %cnf;
  22. if (@_ == 1) {
  23. my $type = (ref($_[0]) eq "SCALAR") ? "doc" : "file";
  24. %cnf = ($type => $_[0]);
  25. }
  26. else {
  27. %cnf = @_;
  28. }
  29. my $textify = delete $cnf{textify} || {img => "alt", applet => "alt"};
  30. my $self = $class->SUPER::new(%cnf, %ARGS) || return undef;
  31. $self->{textify} = $textify;
  32. $self;
  33. }
  34. sub get_tag
  35. {
  36. my $self = shift;
  37. my $token;
  38. while (1) {
  39. $token = $self->get_token || return undef;
  40. my $type = shift @$token;
  41. next unless $type eq "S" || $type eq "E";
  42. substr($token->[0], 0, 0) = "/" if $type eq "E";
  43. return $token unless @_;
  44. for (@_) {
  45. return $token if $token->[0] eq $_;
  46. }
  47. }
  48. }
  49. sub get_text
  50. {
  51. my $self = shift;
  52. my $endat = shift;
  53. my @text;
  54. while (my $token = $self->get_token) {
  55. my $type = $token->[0];
  56. if ($type eq "T") {
  57. my $text = $token->[1];
  58. decode_entities($text) unless $token->[2];
  59. push(@text, $text);
  60. } elsif ($type =~ /^[SE]$/) {
  61. my $tag = $token->[1];
  62. if ($type eq "S") {
  63. if (exists $self->{textify}{$tag}) {
  64. my $alt = $self->{textify}{$tag};
  65. my $text;
  66. if (ref($alt)) {
  67. $text = &$alt(@$token);
  68. } else {
  69. $text = $token->[2]{$alt || "alt"};
  70. $text = "[\U$tag]" unless defined $text;
  71. }
  72. push(@text, $text);
  73. next;
  74. }
  75. } else {
  76. $tag = "/$tag";
  77. }
  78. if (!defined($endat) || $endat eq $tag) {
  79. $self->unget_token($token);
  80. last;
  81. }
  82. }
  83. }
  84. join("", @text);
  85. }
  86. sub get_trimmed_text
  87. {
  88. my $self = shift;
  89. my $text = $self->get_text(@_);
  90. $text =~ s/^\s+//; $text =~ s/\s+$//; $text =~ s/\s+/ /g;
  91. $text;
  92. }
  93. 1;
  94. __END__
  95. =head1 NAME
  96. HTML::TokeParser - Alternative HTML::Parser interface
  97. =head1 SYNOPSIS
  98. require HTML::TokeParser;
  99. $p = HTML::TokeParser->new("index.html") || die "Can't open: $!";
  100. while (my $token = $p->get_token) {
  101. #...
  102. }
  103. =head1 DESCRIPTION
  104. The C<HTML::TokeParser> is an alternative interface to the
  105. C<HTML::Parser> class. It is an C<HTML::PullParser> subclass.
  106. The following methods are available:
  107. =over 4
  108. =item $p = HTML::TokeParser->new( $file_or_doc );
  109. The object constructor argument is either a file name, a file handle
  110. object, or the complete document to be parsed.
  111. If the argument is a plain scalar, then it is taken as the name of a
  112. file to be opened and parsed. If the file can't be opened for
  113. reading, then the constructor will return an undefined value and $!
  114. will tell you why it failed.
  115. If the argument is a reference to a plain scalar, then this scalar is
  116. taken to be the literal document to parse. The value of this
  117. scalar should not be changed before all tokens have been extracted.
  118. Otherwise the argument is taken to be some object that the
  119. C<HTML::TokeParser> can read() from when it needs more data. Typically
  120. it will be a filehandle of some kind. The stream will be read() until
  121. EOF, but not closed.
  122. =item $p->get_token
  123. This method will return the next I<token> found in the HTML document,
  124. or C<undef> at the end of the document. The token is returned as an
  125. array reference. The first element of the array will be a (mostly)
  126. single character string denoting the type of this token: "S" for start
  127. tag, "E" for end tag, "T" for text, "C" for comment, "D" for
  128. declaration, and "PI" for process instructions. The rest of the array
  129. is the same as the arguments passed to the corresponding HTML::Parser
  130. v2 compatible callbacks (see L<HTML::Parser>). In summary, returned
  131. tokens look like this:
  132. ["S", $tag, $attr, $attrseq, $text]
  133. ["E", $tag, $text]
  134. ["T", $text, $is_data]
  135. ["C", $text]
  136. ["D", $text]
  137. ["PI", $token0, $text]
  138. where $attr is a hash reference, $attrseq is an array reference and
  139. the rest is plain scalars.
  140. =item $p->unget_token($token,...)
  141. If you find out you have read too many tokens you can push them back,
  142. so that they are returned the next time $p->get_token is called.
  143. =item $p->get_tag( [$tag, ...] )
  144. This method returns the next start or end tag (skipping any other
  145. tokens), or C<undef> if there are no more tags in the document. If
  146. one or more arguments are given, then we skip tokens until one of the
  147. specified tag types is found. For example:
  148. $p->get_tag("font", "/font");
  149. will find the next start or end tag for a font-element.
  150. The tag information is returned as an array reference in the same form
  151. as for $p->get_token above, but the type code (first element) is
  152. missing. A start tag will be returned like this:
  153. [$tag, $attr, $attrseq, $text]
  154. The tagname of end tags are prefixed with "/", i.e. end tag is
  155. returned like this:
  156. ["/$tag", $text]
  157. =item $p->get_text( [$endtag] )
  158. This method returns all text found at the current position. It will
  159. return a zero length string if the next token is not text. The
  160. optional $endtag argument specifies that any text occurring before the
  161. given tag is to be returned. Any entities will be converted to their
  162. corresponding character.
  163. The $p->{textify} attribute is a hash that defines how certain tags can
  164. be treated as text. If the name of a start tag matches a key in this
  165. hash then this tag is converted to text. The hash value is used to
  166. specify which tag attribute to obtain the text from. If this tag
  167. attribute is missing, then the upper case name of the tag enclosed in
  168. brackets is returned, e.g. "[IMG]". The hash value can also be a
  169. subroutine reference. In this case the routine is called with the
  170. start tag token content as its argument and the return value is treated
  171. as the text.
  172. The default $p->{textify} value is:
  173. {img => "alt", applet => "alt"}
  174. This means that <IMG> and <APPLET> tags are treated as text, and that
  175. the text to substitute can be found in the ALT attribute.
  176. =item $p->get_trimmed_text( [$endtag] )
  177. Same as $p->get_text above, but will collapse any sequences of white
  178. space to a single space character. Leading and trailing white space is
  179. removed.
  180. =back
  181. =head1 EXAMPLES
  182. This example extracts all links from a document. It will print one
  183. line for each link, containing the URL and the textual description
  184. between the <A>...</A> tags:
  185. use HTML::TokeParser;
  186. $p = HTML::TokeParser->new(shift||"index.html");
  187. while (my $token = $p->get_tag("a")) {
  188. my $url = $token->[1]{href} || "-";
  189. my $text = $p->get_trimmed_text("/a");
  190. print "$url\t$text\n";
  191. }
  192. This example extract the <TITLE> from the document:
  193. use HTML::TokeParser;
  194. $p = HTML::TokeParser->new(shift||"index.html");
  195. if ($p->get_tag("title")) {
  196. my $title = $p->get_trimmed_text;
  197. print "Title: $title\n";
  198. }
  199. =head1 SEE ALSO
  200. L<HTML::PullParser>, L<HTML::Parser>
  201. =head1 COPYRIGHT
  202. Copyright 1998-2001 Gisle Aas.
  203. This library is free software; you can redistribute it and/or
  204. modify it under the same terms as Perl itself.
  205. =cut