blib/lib/Statocles/Plugin/LinkCheck.pm

filter: Statementstmt 23.3% 14 / 60 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 19.2% 20 / 104 CC 23 SCAR 61.2
CC
23
Cov
15%
check_pages
552.0
__ANON__
1.1
register
1.0
CRAP 453.6
↑ Summary blib/lib/Statocles/App/Perldoc.pm »
1 package Statocles::Plugin::LinkCheck;
2 3 our $VERSION = '0.099';
3 # ABSTRACT: Check links and images for validity during build
4  
5 3 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 with 'Statocles::Plugin';
7 3 ▶use Mojo::Util qw( url_escape url_unescape );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #pod =attr fatal
10 #pod
11 #pod If set to true, and there are any broken links, the plugin will also call
12 #pod C<die()> after printing the problems. Defaults to false.
13 #pod
14 #pod =cut
15  
16 3 has fatal => (
17 is => 'ro',
18 isa => Bool,
19 default => 0,
20 );
21  
22 #pod =attr ignore
23 #pod
24 #pod An array of URL patterns to ignore. These are interpreted as regular expressions,
25 #pod and are anchored to the beginning of the URL.
26 #pod
27 #pod For example:
28 #pod
29 #pod /broken will match "/broken.html" "/broken/page.html" but not "/page/broken"
30 #pod .*/broken will match "/broken.html" "/broken/page.html" and "/page/broken"
31 #pod
32 #pod =cut
33  
34 has ignore => (
35 is => 'ro',
36 isa => ArrayRef[Str],
37 8 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 3 );
39  
40 #pod =method check_pages
41 #pod
42 #pod $plugin->check_pages( $event );
43 #pod
44 #pod Check the pages inside the given
45 #pod L<Statocles::Event::Pages|Statocles::Event::Pages> event.
46 #pod
47 #pod =cut
48  
49 0 ▶sub check_pages {
Subroutine check_pages: not called CC=23 SCAR=63.1
Pod documented
50 0 my ( $self, $event ) = @_;
51  
52 0 my %page_paths = ();
53 0 my %links = ();
54 0 my %empty = (); # Pages with empty links
55 0 for my $page ( @{ $event->pages } ) {
56 0 $page_paths{ url_unescape $page->path } = 1;
57 0 ▶ if ( $page->DOES( 'Statocles::Page::Document' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->DOES("Statocles::Page::Document")) 0 0 0
58 0 my $dom = $page->dom;
59  
60 0 for my $attr ( qw( src href ) ) {
61 0 for my $el ( $dom->find( "[$attr]" )->each ) {
62 0 my $url = $el->attr( $attr );
63  
64 0 ▶ if ( !$url ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
65 0 push @{ $empty{ $page->path } }, $el;
66 }
67  
68 0 $url =~ s{#.*$}{};
69 0 ▶ next unless $url; # Skip checking fragment-internal links for now
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
70 0 ▶ next if $url =~ m{^(?:[a-z][a-z0-9+.-]*):}i;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^(?:[a-z][a-z0-9+.-]*):/i 0 0 0
71 0 ▶ next if $url =~ m{^//};
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[^//] 0 0 0
72 0 ▶ if ( $url !~ m{^/} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ m[^/]) 0 0 0
73 0 my $clone = $page->path->clone;
74 0 pop @$clone;
75 0 $url = join '/', $clone, $url;
76 }
77  
78 # Fix ".." and ".". Path::Tiny->canonpath can't do
79 # this for us because these paths do not exist on
80 # the filesystem
81 # Fix path/foo/../ into path/
82 0 ▶ 1 while $url =~ s{(/(?![.][.])[^/]+/[.][.](/|\z))}{$2}g; # Fix ".." to refer to parent
Branch: 1 branch
BranchTrueFalseTotal
$url =~ s[(/(?![.][.])[^/]+/[.][.](/|\z))][$2]g and do { '???' } 0 0 0
83 0 $url =~ s{/[.]/}{/}g; # Fix "." to refer to self
84  
85 0 $links{ url_unescape $url }{ url_unescape $page->path }++;
86  
87 }
88 }
89 }
90 }
91  
92 0 my @missing; # Array of arrayrefs of [ link_url, page_path ]
93 0 for my $link_url ( keys %links ) {
94 0 ▶ $link_url .= 'index.html' if $link_url =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $link_url =~ m[/$] 0 0 0
95 0 ▶ next if $page_paths{ $link_url } || $page_paths{ "$link_url/index.html" };
Branch: 1 branch
BranchTrueFalseTotal
if $page_paths{$link_url} or $page_paths{"$link_url/index.html"} 0 0 0
Condition0%0/3
$page_paths{$link_url} or $page_paths{"$link_url/index.html"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$page_paths{$link_url} or $page_paths{"$link_url/index.html"}
$page_paths{$link_url} $page_paths{"$link_url/index.html"}
Truth table0%0/3
A or B
A= $page_paths{$link_url}B= $page_paths{"$link_url/index.html"}
ABresult
1X1
011
000
96 0 ▶ next if grep { $link_url =~ /^$_/ } @{ $self->ignore };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$link_url =~ /^$_/;} @{$self->ignore;} 0 0 0
97 0 push @missing, [ $link_url, $_ ] for keys %{ $links{ $link_url } };
98 }
99  
100 0 for my $page_url ( keys %empty ) {
101 0 push @missing, map { [ '', $page_url, $_ ] } @{ $empty{ $page_url } };
102 }
103  
104 0 ▶ if ( @missing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 0 0 0
105 # Sort by page url and then missing link url
106 0 ▶ for my $m ( sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } @missing ) {
Condition0%0/3
$Statocles::Plugin::LinkCheck::a->[1] cmp $Statocles::Plugin::LinkCheck::b->[1] || $Statocles::Plugin::LinkCheck::a->[0] cmp $Statocles::Plugin::LinkCheck::b->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Statocles::Plugin::LinkCheck::a->[1] cmp $Statocles::Plugin::LinkCheck::b->[1] || $Statocles::Plugin::LinkCheck::a->[0] cmp $Statocles::Plugin::LinkCheck::b->[0]
$Statocles::Plugin::LinkCheck::a->[1] cmp $Statocles::Plugin::LinkCheck::b->[1] $Statocles::Plugin::LinkCheck::a->[0] cmp $Statocles::Plugin::LinkCheck::b->[0]
Truth table0%0/3
A || B
A= $Statocles::Plugin::LinkCheck::a->[1] cmp $Statocles::Plugin::LinkCheck::b->[1]B= $Statocles::Plugin::LinkCheck::a->[0] cmp $Statocles::Plugin::LinkCheck::b->[0]
ABresult
1X1
011
000
107 0 ▶ my $msg = $m->[0] ? sprintf( q{'%s' not found}, $m->[0] )
Branch: 1 branch
BranchTrueFalseTotal
$m->[0] ? : 0 0 0
108 : sprintf( q{Link with text "%s" has no destination}, $m->[2]->text )
109 ;
110 0 $event->emitter->log->warn( "URL broken on $m->[1]: $msg" );
111 }
112  
113 0 ▶ die 'Link check failed!' if $self->fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fatal 0 0 0
114 }
115 }
116  
117 #pod =method register
118 #pod
119 #pod Register this plugin to install its event handlers. Called automatically.
120 #pod
121 #pod =cut
122  
123 8 ▶sub register {
Subroutine register: called CC=1 SCAR=0.4
Pod documented
124 8 my ( $self, $site ) = @_;
125 0 ▶ $site->on( build => sub { $self->check_pages( @_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
126 }
127  
128 3 1;
129  
130 __END__
↑ Summary blib/lib/Statocles/App/Perldoc.pm »