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   177795 use strict;
  2         9  
  2         52  
4 2     2   9 use warnings;
  2         4  
  2         43  
5 2     2   42 use 5.008001;
  2         6  
6 2     2   761 use Alien::Build::Plugin;
  2         4304  
  2         11  
7 2     2   200 use Module::Load ();
  2         5  
  2         883  
8              
9             # ABSTRACT: Plugin to extract links from HTML using Mojo::DOM or Mojo::DOM58
10             our $VERSION = '0.04_01'; # TRIAL VERSION
11             $VERSION = eval $VERSION;
12              
13              
14             sub _load ($)
15             {
16 1     1   3 eval { Module::Load::load($_[0]) };
  1         6  
17 1 50       75 $@ 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 24003 my($self, $meta) = @_;
29              
30 3         12 $meta->add_requires('share' => 'URI' => 0);
31 3         48 $meta->add_requires('share' => 'URI::Escape' => 0);
32              
33 3 100       36 if($self->_class eq 'Mojo::DOM58')
    50          
34             {
35 2         18 $meta->add_requires('share' => 'Mojo::DOM58' => '1.00');
36             }
37             elsif($self->_class eq 'Mojo::DOM')
38             {
39 1         16 $meta->add_requires('share' => 'Mojolicious' => '7.00');
40 1         12 $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 3     3   29786 my(undef, $res) = @_;
49              
50 0         0 die "do not know how to decode @{[ $res->{type} ]}"
51 3 50       12 unless $res->{type} eq 'html';
52              
53 3         16 my $dom = $self->_class->new($res->{content});
54              
55 3         15057 my $base = URI->new($res->{base});
56              
57 3 100       6168 if(my $base_element = $dom->find('head base')->first)
58             {
59 1         832 my $href = $base_element->attr('href');
60 1 50       21 $base = URI->new($href) if defined $href;
61             }
62              
63             my @list = map {
64 24         501 my $url = URI->new_abs($_, $base);
65 24         4996 my $path = $url->path;
66 24         198 $path =~ s{/$}{}; # work around for Perl 5.8.7- gh#8
67             {
68 24         518 filename => URI::Escape::uri_unescape(File::Basename::basename($path)),
69             url => URI::Escape::uri_unescape($url->as_string),
70             }
71             }
72             grep !/^\.\.?\/?$/,
73 28 50       4698 map { $_->attr('href') || () }
74 3         4725 @{ $dom->find('a')->to_array };
  3         12  
75              
76             return {
77 3         159 type => 'list',
78             list => \@list,
79             };
80             })
81              
82              
83 3         49 }
84              
85             1;
86              
87             __END__