File Coverage

blib/lib/Alien/Build/Plugin/Decode/Mojo.pm
Criterion Covered Total %
statement 40 42 95.2
branch 9 14 64.2
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 58 65 89.2


line stmt bran cond sub pod time code
1             package Alien::Build::Plugin::Decode::Mojo;
2              
3 2     2   232162 use strict;
  2         10  
  2         61  
4 2     2   11 use warnings;
  2         5  
  2         51  
5 2     2   49 use 5.008001;
  2         7  
6 2     2   1000 use Alien::Build::Plugin;
  2         5466  
  2         14  
7 2     2   199 use Module::Load ();
  2         4  
  2         1166  
8              
9             # ABSTRACT: Plugin to extract links from HTML using Mojo::DOM or Mojo::DOM58
10             our $VERSION = '0.03_01'; # TRIAL VERSION
11             $VERSION = eval $VERSION;
12              
13              
14             sub _load ($)
15             {
16 1     1   6 eval { Module::Load::load($_[0]) };
  1         8  
17 1 50       101 $@ eq '' ? 1 : 0;
18             }
19              
20             has _class => sub {
21             return 'Mojo::DOM58' if _load 'Mojo::DOM58';
22             return 'Mojo::DOM' if _load 'Mojo::DOM' && _load 'Mojolicious' && eval { Mojolicious->VERSION('7.00') };
23             return 'Mojo::DOM58';
24             };
25              
26             sub init
27             {
28 3     3 1 43364 my($self, $meta) = @_;
29              
30 3         16 $meta->add_requires('share' => 'URI' => 0);
31 3         59 $meta->add_requires('share' => 'URI::Escape' => 0);
32              
33 3 100       45 if($self->_class eq 'Mojo::DOM58')
    50          
34             {
35 2         28 $meta->add_requires('share' => 'Mojo::DOM58' => '1.00');
36             }
37             elsif($self->_class eq 'Mojo::DOM')
38             {
39 1         18 $meta->add_requires('share' => 'Mojolicious' => '7.00');
40 1         15 $meta->add_requires('share' => 'Mojo::DOM' => '0');
41             }
42             else
43             {
44 0         0 die "bad class";
45             }
46              
47             $meta->register_hook( decode => sub {
48 6     6   78327 my(undef, $res) = @_;
49              
50 0         0 die "do not know how to decode @{[ $res->{type} ]}"
51 6 50       68 unless $res->{type} eq 'html';
52              
53 6         38 my $dom = $self->_class->new($res->{content});
54              
55 6         37275 my $base = URI->new($res->{base});
56              
57 6 100       8085 if(my $base_element = $dom->find('head base')->first)
58             {
59 2         1992 my $href = $base_element->attr('href');
60 2 50       50 $base = URI->new($href) if defined $href;
61             }
62              
63             my @list = map {
64 48         1206 my $url = URI->new_abs($_, $base);
65 48         12216 my $path = $url->path;
66 48         505 $path =~ s{/$}{}; # work around for Perl 5.8.7- gh#8
67             {
68 48         1653 filename => URI::Escape::uri_unescape(File::Basename::basename($path)),
69             url => URI::Escape::uri_unescape($url->as_string),
70             }
71             }
72             grep !/^\.\.?\/?$/,
73 56 50       11290 map { $_->attr('href') || () }
74 6         10061 @{ $dom->find('a')->to_array };
  6         24  
75              
76             return {
77 6         432 type => 'list',
78             list => \@list,
79             };
80             })
81              
82              
83 3         62 }
84              
85             1;
86              
87             __END__