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.

181 lines
4.5 KiB

  1. package HTML::LinkExtor;
  2. =head1 NAME
  3. HTML::LinkExtor - Extract links from an HTML document
  4. =head1 SYNOPSIS
  5. require HTML::LinkExtor;
  6. $p = HTML::LinkExtor->new(\&cb, "http://www.perl.org/");
  7. sub cb {
  8. my($tag, %links) = @_;
  9. print "$tag @{[%links]}\n";
  10. }
  11. $p->parse_file("index.html");
  12. =head1 DESCRIPTION
  13. I<HTML::LinkExtor> is an HTML parser that extracts links from an
  14. HTML document. The I<HTML::LinkExtor> is a subclass of
  15. I<HTML::Parser>. This means that the document should be given to the
  16. parser by calling the $p->parse() or $p->parse_file() methods.
  17. =cut
  18. require HTML::Parser;
  19. @ISA = qw(HTML::Parser);
  20. $VERSION = sprintf("%d.%02d", q$Revision: 1.31 $ =~ /(\d+)\.(\d+)/);
  21. use strict;
  22. use HTML::Tagset ();
  23. # legacy (some applications grabs this hash directly)
  24. use vars qw(%LINK_ELEMENT);
  25. *LINK_ELEMENT = \%HTML::Tagset::linkElements;
  26. =over 4
  27. =item $p = HTML::LinkExtor->new([$callback[, $base]])
  28. The constructor takes two optional arguments. The first is a reference
  29. to a callback routine. It will be called as links are found. If a
  30. callback is not provided, then links are just accumulated internally
  31. and can be retrieved by calling the $p->links() method.
  32. The $base argument is an optional base URL used to absolutize all URLs found.
  33. You need to have the I<URI> module installed if you provide $base.
  34. The callback is called with the lowercase tag name as first argument,
  35. and then all link attributes as separate key/value pairs. All
  36. non-link attributes are removed.
  37. =cut
  38. sub new
  39. {
  40. my($class, $cb, $base) = @_;
  41. my $self = $class->SUPER::new(
  42. start_h => ["_start_tag", "self,tagname,attr"],
  43. report_tags => [keys %HTML::Tagset::linkElements],
  44. );
  45. $self->{extractlink_cb} = $cb;
  46. if ($base) {
  47. require URI;
  48. $self->{extractlink_base} = URI->new($base);
  49. }
  50. $self;
  51. }
  52. sub _start_tag
  53. {
  54. my($self, $tag, $attr) = @_;
  55. my $base = $self->{extractlink_base};
  56. my $links = $HTML::Tagset::linkElements{$tag};
  57. $links = [$links] unless ref $links;
  58. my @links;
  59. my $a;
  60. for $a (@$links) {
  61. next unless exists $attr->{$a};
  62. push(@links, $a, $base ? URI->new($attr->{$a}, $base)->abs($base)
  63. : $attr->{$a});
  64. }
  65. return unless @links;
  66. $self->_found_link($tag, @links);
  67. }
  68. sub _found_link
  69. {
  70. my $self = shift;
  71. my $cb = $self->{extractlink_cb};
  72. if ($cb) {
  73. &$cb(@_);
  74. } else {
  75. push(@{$self->{'links'}}, [@_]);
  76. }
  77. }
  78. =item $p->links
  79. Returns a list of all links found in the document. The returned
  80. values will be anonymous arrays with the follwing elements:
  81. [$tag, $attr => $url1, $attr2 => $url2,...]
  82. The $p->links method will also truncate the internal link list. This
  83. means that if the method is called twice without any parsing
  84. between them the second call will return an empty list.
  85. Also note that $p->links will always be empty if a callback routine
  86. was provided when the I<HTML::LinkExtor> was created.
  87. =cut
  88. sub links
  89. {
  90. my $self = shift;
  91. exists($self->{'links'}) ? @{delete $self->{'links'}} : ();
  92. }
  93. # We override the parse_file() method so that we can clear the links
  94. # before we start a new file.
  95. sub parse_file
  96. {
  97. my $self = shift;
  98. delete $self->{'links'};
  99. $self->SUPER::parse_file(@_);
  100. }
  101. =back
  102. =head1 EXAMPLE
  103. This is an example showing how you can extract links from a document
  104. received using LWP:
  105. use LWP::UserAgent;
  106. use HTML::LinkExtor;
  107. use URI::URL;
  108. $url = "http://www.perl.org/"; # for instance
  109. $ua = LWP::UserAgent->new;
  110. # Set up a callback that collect image links
  111. my @imgs = ();
  112. sub callback {
  113. my($tag, %attr) = @_;
  114. return if $tag ne 'img'; # we only look closer at <img ...>
  115. push(@imgs, values %attr);
  116. }
  117. # Make the parser. Unfortunately, we don't know the base yet
  118. # (it might be diffent from $url)
  119. $p = HTML::LinkExtor->new(\&callback);
  120. # Request document and parse it as it arrives
  121. $res = $ua->request(HTTP::Request->new(GET => $url),
  122. sub {$p->parse($_[0])});
  123. # Expand all image URLs to absolute ones
  124. my $base = $res->base;
  125. @imgs = map { $_ = url($_, $base)->abs; } @imgs;
  126. # Print them out
  127. print join("\n", @imgs), "\n";
  128. =head1 SEE ALSO
  129. L<HTML::Parser>, L<HTML::Tagset>, L<LWP>, L<URI::URL>
  130. =head1 COPYRIGHT
  131. Copyright 1996-2001 Gisle Aas.
  132. This library is free software; you can redistribute it and/or
  133. modify it under the same terms as Perl itself.
  134. =cut
  135. 1;