FollowLinks/Parser/HTML.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 69 CC 4 SCAR 20.2
CC
4
Cov
0%
_start
20.0
new
2.0
parse
2.0
CRAP 7.5
1 # $Id: /mirror/perl/GunghoX-FollowLinks/trunk/lib/GunghoX/FollowLinks/Parser/HTML.pm 40584 2008-01-29T14:54:08.742000Z daisuke $
2 #
3 # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4 # All rights reserved.
5  
6 package GunghoX::FollowLinks::Parser::HTML;
7 use strict;
8 use warnings;
9 use base qw(GunghoX::FollowLinks::Parser);
10 use HTML::Parser;
11 use HTML::Tagset;
12 use URI;
13 use List::Util qw(shuffle);
14  
15 __PACKAGE__->mk_accessors($_) for qw(parser);
16  
17 sub new
18 {
19 my $class = shift;
20 my $parser = HTML::Parser->new(
21 start_h => [ \&_start, "self,tagname,attr" ],
22 report_tags => [ keys %HTML::Tagset::linkElements ],
23 );
24 return $class->next::method(
25 content_type => 'text/html',
26 @_,
27 parser => $parser
28 );
29 }
30  
31 sub _start
32 {
33 my ($self, $tag, $attr) = @_;
34  
35 my $links = $HTML::Tagset::linkElements{ $tag };
36 $links = [ $links ] unless ref $links;
37  
38 my $container = $self->{ 'container' };
39 my $c = $self->{ 'context' };
40 my $response = $self->{ 'response' };
41 my $base = $response->request->uri;
42 foreach my $link_attr (shuffle @$links) {
43 next unless exists $attr->{ $link_attr };
44  
45 my $url = URI->new_abs( $attr->{ $link_attr }, $base );
46 if ($container->follow_if_allowed( $c, $response, $url, { tag => $tag, attr => $attr } )) {
47 $self->{ 'count' }++;
48 }
49 }
50 }
51  
52 sub parse
53 {
54 my ($self, $c, $response) = @_;
55  
56 my $parser = $self->parser;
57 local $parser->{ 'response' } = $response;
58 local $parser->{ 'container' } = $self;
59 local $parser->{ 'context' } = $c;
60 local $parser->{ 'count' } = 0;
61 $parser->parse( $response->content );
62 $parser->eof;
63 return $parser->{ 'count' };
64 }
65  
66 1;
67  
68 __END__