home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / HTML / LinkExtor.pm < prev    next >
Text File  |  1997-09-05  |  4KB  |  178 lines

  1. package HTML::LinkExtor;
  2.  
  3. =head1 NAME
  4.  
  5. HTML::LinkExtor - Extract links from an HTML document
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.  require HTML::LinkExtor;
  10.  $p = HTML::LinkExtor->new(\&cb, "http://www.sn.no/");
  11.  sub cb {
  12.      my($tag, %links) = @_;
  13.      print "$tag @{[%links]}\n";
  14.  }
  15.  $p->parse_file("index.html");
  16.  
  17. =head1 DESCRIPTION
  18.  
  19. The I<HTML::LinkExtor> (link extractor) is an HTML parser that takes a
  20. callback routine as parameter.  This routine is then called as the
  21. various link attributes are recognized.
  22.  
  23. The I<HTML::LinkExtor> is a subclass of I<HTML::Parser>. This means
  24. that the document should be given to the parser by calling the
  25. $p->parse() or $p->parse_file() methods.
  26.  
  27. =cut
  28.  
  29. require HTML::Parser;
  30. @ISA = qw(HTML::Parser);
  31.  
  32. use URI::URL qw(url);
  33.  
  34. use strict;
  35. use vars qw(%LINK_ELEMENT);
  36.  
  37. # Elements that might contain links and the name of the link attribute
  38. %LINK_ELEMENT =
  39. (
  40.  body   => 'background',
  41.  base   => 'href',
  42.  a      => 'href',
  43.  img    => [qw(src lowsrc usemap)],   # 'lowsrc' is a Netscape invention
  44.  form   => 'action',
  45.  input  => 'src',
  46. 'link'  => 'href',          # need quoting since link is a perl builtin
  47.  frame  => 'src',
  48.  applet => [qw(codebase code)],
  49.  area   => 'href',
  50.  frame  => 'src',   # Netscape 2.0 extention
  51.  embed  => 'src',   # used in Netscape 2.0 for Shockwave and things like that
  52. );
  53.  
  54. =head2 $p = HTML::LinkExtor->new([$callback[, $base]])
  55.  
  56. The constructor takes two optional argument. The first is a reference
  57. to a callback routine. It will be called as links are found. If a
  58. callback is not provided, then links are just accumulated internally
  59. and can be retrieved by calling the $p->links() method. The $base is
  60. an optional base URL used to absolutize all URLs found.
  61.  
  62. The callback is called with the lowercase tag name as first argument,
  63. and then all link attributes as separate key/value pairs.  All
  64. non-link attributes are removed.
  65.  
  66. =cut
  67.  
  68. sub new
  69. {
  70.     my($class, $cb, $base) = @_;
  71.     my $self = $class->SUPER::new;
  72.     $self->{extractlink_cb} = $cb;
  73.     $self->{extractlink_base} = $base;
  74.     $self;
  75. }
  76.  
  77. sub start
  78. {
  79.     my($self, $tag, $attr) = @_;  # $attr is reference to a HASH
  80.     return unless exists $LINK_ELEMENT{$tag};
  81.  
  82.     my $base = $self->{extractlink_base};
  83.     my $links = $LINK_ELEMENT{$tag};
  84.     $links = [$links] unless ref $links;
  85.  
  86.     my @links;
  87.     my $a;
  88.     for $a (@$links) {
  89.     next unless exists $attr->{$a};
  90.     push(@links, $a, $base ? url($attr->{$a}, $base)->abs : $attr->{$a});
  91.     }
  92.     return unless @links;
  93.  
  94.     my $cb = $self->{extractlink_cb};
  95.     if ($cb) {
  96.     &$cb($tag, @links);
  97.     } else {
  98.     push(@{$self->{'links'}}, [$tag, @links]);
  99.     }
  100. }
  101.  
  102. =head2 $p->links
  103.  
  104. Returns a list of all links found in the document.  The returned
  105. values will be anonymous arrays with the follwing elements:
  106.  
  107.   [$tag, $attr => $url1, $attr2 => $url2,...]
  108.  
  109. The $p->links method will also truncate the internal link list.  This
  110. means that if the method is called twice without any parsing in
  111. between then the second call will return an empty list.
  112.  
  113. Also note that $p->links will always be empty if a callback routine
  114. was provided when the L<HTML::LinkExtor> was created.
  115.  
  116. =cut
  117.  
  118. sub links
  119. {
  120.     my $self = shift;
  121.     exists($self->{'links'}) ? @{delete $self->{'links'}} : ();
  122. }
  123.  
  124. # We override the parse_file() method so that we can clear the links
  125. # before we start with a new file.
  126. sub parse_file
  127. {
  128.     my $self = shift;
  129.     delete $self->{'links'};
  130.     $self->SUPER::parse_file(@_);
  131. }
  132.  
  133. =head1 EXAMPLE
  134.  
  135. This is an example showing how you can extract links as a document
  136. is received using LWP:
  137.  
  138.   use LWP::UserAgent;
  139.   use HTML::LinkExtor;
  140.   use URI::URL;
  141.  
  142.   $url = "http://www.sn.no/";  # for instance
  143.   $ua = new LWP::UserAgent;
  144.  
  145.   # Set up a callback that collect image links
  146.   my @imgs = ();
  147.   sub callback {
  148.      my($tag, %attr) = @_;
  149.      return if $tag ne 'img';  # we only look closer at <img ...>
  150.      push(@imgs, values %attr);
  151.   }
  152.  
  153.   # Make the parser.  Unfortunately, we don't know the base yet (it might
  154.   # be diffent from $url)
  155.   $p = HTML::LinkExtor->new(\&callback);
  156.  
  157.   # Request document and parse it as it arrives
  158.   $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->parse($_[0])});
  159.  
  160.   # Expand all image URLs to absolute ones
  161.   my $base = $res->base;
  162.   @imgs = map { $_ = url($_, $base)->abs; } @imgs;
  163.  
  164.   # Print them out
  165.   print join("\n", @imgs), "\n";
  166.  
  167. =head1 SEE ALSO
  168.  
  169. L<HTML::Parser>
  170.  
  171. =head1 AUTHOR
  172.  
  173. Gisle Aas E<lt>aas@sn.no>
  174.  
  175. =cut
  176.  
  177. 1;
  178.