lib/App/Dapper/Check.pm

filter: Statementstmt 21.9% 58 / 264 Branchbran 15.0% 25 / 166 Conditioncond 28.2% 26 / 92 MC/DCmcdc 25.4% 14 / 55 Subroutinesub 57.8% 11 / 19 Podpod 100% 8 / 8 totaltotal 23.5% 142 / 604 CC 128 SCAR 69.0
CC
128
Cov
21%
quality
2652.0
run
210.0
links
210.0
CRAP 991.3
↑ Summary lib/App/Dapper/Import.pm »
1 package App::Dapper::Check;
2  
3 =head1 NAME
4  
5 App::Dapper::Check - Find the problems in a site without publishing it.
6  
7 =head1 DESCRIPTION
8  
9 C<dapper check> builds the site into a temporary directory, collecting every
10 problem instead of stopping at the first, then looks for links between pages
11 that lead nowhere. The real output directory is not touched.
12  
13 $ dapper check
14 $ dapper check --json
15  
16 It reports:
17  
18 =over 4
19  
20 =item * front matter that can't be read, or that doesn't match a C<_schema.yml>
21  
22 =item * layouts that don't exist, and errors in templates
23  
24 =item * two pages that would be written to the same file
25  
26 =item * links and images (C<href> and C<src>) inside the site that lead to no file
27  
28 =item * settings that need C<url> when it isn't set, and pages without a title
29  
30 =item * images without alt text, skipped heading levels, pages with no language,
31 and links with no text or only "click here" (see L</quality>)
32  
33 =item * missing, duplicate or overlong titles and descriptions
34  
35 =back
36  
37 With C<--json>, the report is printed as JSON, for scripts and AI assistants:
38  
39 {
40 "ok": false,
41 "pages": 12,
42 "errors": [ { "file": "...", "type": "...", "message": "..." } ],
43 "warnings": [ ... ]
44 }
45  
46 The command exits with status 1 if there are errors, and 0 otherwise.
47  
48 =cut
49  
50 190 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 190 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 190 ▶use File::Temp qw/ tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 190 ▶use File::Spec::Functions qw/ catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 190 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 190 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 190 ▶use App::Dapper::Publish;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 =head2 run
63  
64 Takes a Dapper object and returns the report as a hash reference.
65  
66 =cut
67  
68 0 ▶sub run {
Subroutine run: not called CC=14 SCAR=53.5
Pod documented
69 0 my ($dapper) = @_;
70  
71 0 my $tmp = tempdir(CLEANUP => 1);
72  
73 # Build into the temporary directory, collecting problems
74 0 my %saved = map { $_ => $dapper->{$_} } qw/ collect quiet output /;
75 0 my $explicit_output = $dapper->{explicit}->{output};
76  
77 0 $dapper->{collect} = 1;
78 0 $dapper->{quiet} = 1;
79  
80 0 my ($pages, @problems) = (0);
81 0 my @held;
82 0 my $ok = eval {
83 # Resolve the real directories first, then build into the temporary one
84 0 $dapper->read_project();
85 0 my $real_output = $dapper->{output};
86 0 $dapper->{output} = catfile($tmp, 'site');
87 0 $dapper->{explicit}->{output} = 1;
88 0 $dapper->build();
89 0 $dapper->{site}->{output} = $real_output;
90 0 1;
91 };
92  
93 0 ▶ @problems = @{ $dapper->{problems} || [] };
Condition0%0/2
$dapper->{'problems'} || []
l!l
00
MC/DC0%0/1
$dapper->{'problems'} || []
$dapper->{'problems'}
Truth table0%0/2
A
A= $dapper->{'problems'}
Aresult
11
00
94 0 ▶ if (not $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
95 0 (my $message = $@) =~ s/^(?:error): //;
96 0 $message =~ s/\s+\z//;
97 0 push @problems, { severity => 'error', file => $dapper->{config}, message => $message };
98 }
99  
100 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
101 0 $pages = scalar @{ $dapper->{site}->{pages} };
102 0 ▶ @held = map { { file => $_->{file}, reason => $_->{reason} } } @{ $dapper->{held} || [] };
Condition0%0/2
$dapper->{'held'} || []
l!l
00
MC/DC0%0/1
$dapper->{'held'} || []
$dapper->{'held'}
Truth table0%0/2
A
A= $dapper->{'held'}
Aresult
11
00
103 0 push @problems, links($dapper);
104 0 push @problems, warnings($dapper);
105 0 push @problems, quality($dapper);
106 }
107  
108 # Name the real output directory, not the temporary one, in messages
109 0 my $temporary = catfile($tmp, 'site');
110 my $real = defined $dapper->{site}->{output} && $dapper->{site}->{output} ne $temporary
111 0 ▶ ? $dapper->{site}->{output} : $saved{output};
Branch: 1 branch
BranchTrueFalseTotal
defined $dapper->{'site'}{'output'} && $dapper->{'site'}{'output'} ne $temporary ? : 0 0 0
Condition0%0/3
defined $dapper->{"site"}{"output"} and $dapper->{"site"}{"output"} ne $temporary
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dapper->{"site"}{"output"} and $dapper->{"site"}{"output"} ne $temporary
defined $dapper->{"site"}{"output"} $dapper->{"site"}{"output"} ne $temporary
Truth table0%0/3
A and B
A= defined $dapper->{"site"}{"output"}B= $dapper->{"site"}{"output"} ne $temporary
ABresult
0X0
100
111
112 0 for my $problem (@problems) {
113 0 for my $key (qw/ file message /) {
114 0 ▶ $problem->{$key} =~ s/\Q$temporary\E/$real/g if defined $problem->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem->{$key} 0 0 0
115 }
116 }
117  
118 0 $dapper->{$_} = $saved{$_} for keys %saved;
119 0 $dapper->{explicit}->{output} = $explicit_output;
120  
121 0 for my $problem (@problems) {
122 0 ▶ $problem->{type} = classify($problem) unless defined $problem->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $problem->{"type"} 0 0 0
123 }
124  
125 0 my @errors = grep { $_->{severity} eq 'error' } @problems;
126 0 my @warnings = grep { $_->{severity} ne 'error' } @problems;
127  
128 return {
129 ok => @errors ? JSON::false : JSON::true,
130 pages => $pages,
131 0 errors => [ map { _clean($_) } @errors ],
132 0 ▶ warnings => [ map { _clean($_) } @warnings ],
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 0 0 0
133 held => \@held,
134 };
135 }
136  
137 0 ▶sub _clean {
Subroutine _clean: not called CC=3 SCAR=24.8
138 0 my ($problem) = @_;
139  
140 # Messages often start with the file they are about; it is in "file" already
141 0 my $message = $problem->{message};
142 0 ▶ my $file = defined $problem->{file} ? $problem->{file} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $problem->{'file'} ? : 0 0 0
143 0 ▶ $message =~ s/^\Q$file\E:?\s+// if length $file;
Branch: 1 branch
BranchTrueFalseTotal
if length $file 0 0 0
144  
145 0 return { file => $problem->{file}, type => $problem->{type}, message => $message };
146 }
147  
148 =head2 classify
149  
150 Takes a problem and returns a short name for its kind: C<schema>,
151 C<front_matter>, C<layout>, C<template>, C<duplicate>, C<link>, C<setting>,
152 C<component>, C<accessibility>, C<seo> or C<other>.
153  
154 =cut
155  
156 0 ▶sub classify {
Subroutine classify: not called CC=8 SCAR=42.8
Pod documented
157 0 my ($problem) = @_;
158  
159 0 my $message = $problem->{message};
160 0 ▶ return 'schema' if $message =~ /schema/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /schema/ 0 0 0
161 0 ▶ return 'front_matter' if $message =~ /front matter|could not understand the date|unknown time zone/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /front matter|could not understand the date|unknown time zone/ 0 0 0
162 0 ▶ return 'layout' if $message =~ /uses the layout|loop of layouts|names the layout/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /uses the layout|loop of layouts|names the layout/ 0 0 0
163 0 ▶ return 'template' if $message =~ /could not process/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /could not process/ 0 0 0
164 0 ▶ return 'duplicate' if $message =~ /both write|already writes/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /both write|already writes/ 0 0 0
165 0 ▶ return 'setting' if $message =~ /needs the site|needs a layout|needs Pagefind|must be settings/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /needs the site|needs a layout|needs Pagefind|must be settings/ 0 0 0
166 0 ▶ return 'component' if $message =~ /uses the component|component template/;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /uses the component|component template/ 0 0 0
167 0 return 'other';
168 }
169  
170 =head2 links
171  
172 Takes a Dapper object that has just been built, and returns a problem for each
173 link or image in an HTML page that points to a file the site does not have.
174  
175 =cut
176  
177 0 ▶sub links {
Subroutine links: not called CC=14 SCAR=53.5
Pod documented
178 0 my ($dapper) = @_;
179  
180 0 my $site = $dapper->{site};
181 0 my $output = $dapper->{output};
182 0 ▶ my %source_of = %{ $dapper->{page_owner} || {} };
Condition0%0/2
$dapper->{'page_owner'} || {}
l!l
00
MC/DC0%0/1
$dapper->{'page_owner'} || {}
$dapper->{'page_owner'}
Truth table0%0/2
A
A= $dapper->{'page_owner'}
Aresult
11
00
183 0 ▶ $source_of{$_->{output}} = $_->{source} for @{ $dapper->{static_files} || [] };
Condition0%0/2
$dapper->{'static_files'} || []
l!l
00
MC/DC0%0/1
$dapper->{'static_files'} || []
$dapper->{'static_files'}
Truth table0%0/2
A
A= $dapper->{'static_files'}
Aresult
11
00
184 0 ▶ my $base = defined $site->{url} ? $site->{url} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $site->{'url'} ? : 0 0 0
185 0 $base =~ s{/+$}{};
186  
187 0 my @problems;
188  
189 0 for my $relative (sort keys %{$dapper->{written}}) {
190 0 ▶ next unless $relative =~ /\.html?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $relative =~ /\.html?$/i 0 0 0
191 0 my $file = catfile($output, $relative);
192 0 my $html = App::Dapper::Utils::read_file($file);
193  
194 # Links inside comments (including old IE conditional comments) are never
195 # followed by a browser
196 0 $html =~ s/<!--.*?-->//gs;
197 0 my $page_dir = '/' . $relative;
198 0 $page_dir =~ s{[^/]*$}{};
199  
200 0 my %seen;
201 0 ▶ while ($html =~ /\b(?:href|src)\s*=\s*(["'])(.*?)\1/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\b(?:href|src)\s*=\s*(["'])(.*?)\1/sig) 0 0 0
202 0 my $link = $2;
203 0 ▶ next if $seen{$link}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$link}++ 0 0 0
204  
205 0 my $path = target_path($link, $page_dir, $base);
206 0 ▶ next unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
207 0 ▶ next if $site->{search} and $path =~ m{^/pagefind/};
Branch: 1 branch
BranchTrueFalseTotal
if $site->{"search"} and $path =~ m[^/pagefind/] 0 0 0
Condition0%0/3
$site->{"search"} and $path =~ m[^/pagefind/]
!ll&&!rl&&r
000
MC/DC0%0/2
$site->{"search"} and $path =~ m[^/pagefind/]
$site->{"search"} $path =~ m[^/pagefind/]
Truth table0%0/3
A and B
A= $site->{"search"}B= $path =~ m[^/pagefind/]
ABresult
0X0
100
111
208 0 ▶ next if exists_in($output, $path);
Branch: 1 branch
BranchTrueFalseTotal
if exists_in($output, $path) 0 0 0
209  
210 push @problems, {
211 severity => 'error',
212 type => 'link',
213 # A file copied from the project directory is reported by its own name
214 0 ▶ file => $source_of{$file} || $relative,
Condition0%0/3
$source_of{$file} || $relative
l!l&&r!l&&!r
000
MC/DC0%0/2
$source_of{$file} || $relative
$source_of{$file} $relative
Truth table0%0/3
A || B
A= $source_of{$file}B= $relative
ABresult
1X1
011
000
215 message => "links to $link, which is not in the site",
216 };
217 }
218 }
219  
220 0 return @problems;
221 }
222  
223 =head2 target_path
224  
225 Takes a link, the directory of the page it is on (e.g. C</blog/post/>) and the
226 site's URL, and returns the path in the site it points to, or nothing for links
227 that lead outside the site (or to the same page).
228  
229 =cut
230  
231 48 ▶sub target_path {
Subroutine target_path: called CC=20 SCAR=30.0
Pod documented
232 48 my ($link, $page_dir, $base) = @_;
233  
234 48 $link =~ s/^\s+|\s+$//g;
235 48 ▶ return if $link eq '' or $link =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $link eq "" or $link =~ /^#/ 4 44 48
Condition100%3/3
$link eq "" or $link =~ /^#/
l!l&&r!l&&!r
2244
MC/DC100%2/2
$link eq "" or $link =~ /^#/
$link eq "" $link =~ /^#/
Truth table100%3/3
A or B
A= $link eq ""B= $link =~ /^#/
ABresult
1X1
011
000
236  
237 # A link to the site's own address: the address, then the end or /, ? or #
238 44 ▶ if (length $base and index($link, $base) == 0 and substr($link, length $base) =~ m{^(?:$|[/?#])}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $base and index($link, $base) == 0 and substr($link, length $base) =~ m[^(?:$|[/?#])]) 6 38 44
Condition100%6/6
length $base and index($link, $base) == 0 and substr($link, length $base) =~ m[^(?:$|[/?#])]
!ll&&!rl&&r
3626
length $base and index($link, $base) == 0
!ll&&!rl&&r
3338
MC/DC100%3/3
length $base and index($link, $base) == 0 and substr($link, length $base) =~ m[^(?:$|[/?#])]
length $base index($link, $base) == 0 substr($link, length $base) =~ m[^(?:$|[/?#])]
Truth table100%4/4
A and B and C
A= length $baseB= index($link, $base) == 0C= substr($link, length $base) =~ m[^(?:$|[/?#])]
ABCresult
0XX0
10X0
1100
1111
239 6 $link = substr($link, length $base);
240 6 ▶ $link = '/' unless length $link;
Branch: 1 branch
BranchTrueFalseTotal
unless length $link 1 5 6
241 }
242  
243 44 ▶ return if $link =~ m{^[a-z][a-z0-9+.-]*:}i or $link =~ m{^//};
Branch: 1 branch
BranchTrueFalseTotal
if $link =~ /^[a-z][a-z0-9+.-]*:/i or $link =~ m[^//] 13 31 44
Condition100%3/3
$link =~ /^[a-z][a-z0-9+.-]*:/i or $link =~ m[^//]
l!l&&r!l&&!r
12131
MC/DC100%2/2
$link =~ /^[a-z][a-z0-9+.-]*:/i or $link =~ m[^//]
$link =~ /^[a-z][a-z0-9+.-]*:/i $link =~ m[^//]
Truth table100%3/3
A or B
A= $link =~ /^[a-z][a-z0-9+.-]*:/iB= $link =~ m[^//]
ABresult
1X1
011
000
244  
245 31 $link =~ s/[?#].*$//;
246 31 ▶ return if $link eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $link eq "" 2 29 31
247  
248 29 $link =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
249 29 $link =~ s/&amp;/&/g;
250  
251 29 ▶ my $path = $link =~ m{^/} ? $link : "$page_dir$link";
Branch: 1 branch
BranchTrueFalseTotal
$link =~ m[^/] ? : 15 14 29
252  
253 29 my @parts;
254 29 for my $part (split m{/}, $path, -1) {
255 92 ▶ next if $part eq '.' or ($part eq '' and @parts);
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "." or $part eq "" and @parts 13 79 92
Condition100%6/6
$part eq "." or $part eq "" and @parts
l!l&&r!l&&!r
31079
$part eq "" and @parts
!ll&&!rl&&r
502910
MC/DC100%3/3
$part eq "." or $part eq "" and @parts
$part eq "." $part eq "" @parts
Truth table100%4/4
A or B and C
A= $part eq "."B= $part eq ""C= @parts
ABCresult
1XX1
0111
00X0
0100
256 79 ▶ if ($part eq '..') { pop @parts if @parts > 1; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($part eq "..") 9 70 79
if @parts > 1 6 3 9
257 70 push @parts, $part;
258 }
259 29 my $resolved = join('/', @parts);
260 29 ▶ $resolved .= '/' if $path =~ m{/$} and $resolved !~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[/$] and not $resolved =~ m[/$] 9 20 29
Condition66%2/3
$path =~ m[/$] and not $resolved =~ m[/$]
!ll&&!rl&&r
2009
MC/DC50%1/2
$path =~ m[/$] and not $resolved =~ m[/$]
$path =~ m[/$] not $resolved =~ m[/$]
Truth table66%2/3
A and not(B)
A= $path =~ m[/$]B= not $resolved =~ m[/$]
ABresult
0X0
100
111
261 29 ▶ $resolved = "/$resolved" unless $resolved =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $resolved =~ m[^/] 0 29 29
262  
263 29 return $resolved;
264 }
265  
266 =head2 exists_in
267  
268 Takes the output directory and a path in the site, and returns true if a file
269 is there (or, for a directory, an C<index.html> in it).
270  
271 =cut
272  
273 9 ▶sub exists_in {
Subroutine exists_in: called CC=5 SCAR=16.1
Pod documented
274 9 my ($output, $path) = @_;
275  
276 9 my $file = catfile($output, $path);
277 9 ▶ return 1 if -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 2 7 9
278 7 ▶ return 1 if -d $file and (-f catfile($file, 'index.html') or -f catfile($file, 'index.htm'));
Branch: 1 branch
BranchTrueFalseTotal
if -d $file and -f catfile($file, "index.html") || -f catfile($file, "index.htm") 4 3 7
Condition100%6/6
-d $file and -f catfile($file, "index.html") || -f catfile($file, "index.htm")
!ll&&!rl&&r
124
-f catfile($file, "index.html") or -f catfile($file, "index.htm")
l!l&&r!l&&!r
312
MC/DC100%3/3
-d $file and -f catfile($file, "index.html") || -f catfile($file, "index.htm")
-d $file -f catfile($file, "index.html") -f catfile($file, "index.htm")
Truth table100%4/4
A and B or C
A= -d $fileB= -f catfile($file, "index.html")C= -f catfile($file, "index.htm")
ABCresult
0XX0
1000
11X1
1011
279 3 return 0;
280 }
281  
282 =head2 warnings
283  
284 Takes a Dapper object that has just been built, and returns warnings about the
285 site's settings and pages.
286  
287 =cut
288  
289 0 ▶sub warnings {
Subroutine warnings: not called CC=11 SCAR=48.8
Pod documented
290 0 my ($dapper) = @_;
291  
292 0 my $site = $dapper->{site};
293 0 my @problems;
294  
295 0 ▶ my $has_url = defined $site->{url} && length $site->{url};
Condition0%0/3
defined $site->{'url'} && length $site->{'url'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $site->{'url'} && length $site->{'url'}
defined $site->{'url'} length $site->{'url'}
Truth table0%0/3
A && B
A= defined $site->{'url'}B= length $site->{'url'}
ABresult
0X0
100
111
296 0 ▶ if (not $has_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_url) 0 0 0
297 push @problems, {
298 severity => 'warning',
299 type => 'setting',
300 file => $dapper->{config},
301 0 message => 'url is not set, so there is no sitemap, and links in llms.txt and page.seo are not absolute',
302 };
303 }
304  
305 0 for my $page (@{$site->{pages}}) {
306 0 ▶ next if $page->{generated} or $page->{paginator} && $page->{paginator}->{page} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"generated"} or $page->{"paginator"} and $page->{"paginator"}{"page"} > 1 0 0 0
Condition0%0/6
$page->{"generated"} or $page->{"paginator"} and $page->{"paginator"}{"page"} > 1
l!l&&r!l&&!r
000
$page->{"paginator"} and $page->{"paginator"}{"page"} > 1
!ll&&!rl&&r
000
MC/DC0%0/3
$page->{"generated"} or $page->{"paginator"} and $page->{"paginator"}{"page"} > 1
$page->{"generated"} $page->{"paginator"} $page->{"paginator"}{"page"} > 1
Truth table0%0/4
A or B and C
A= $page->{"generated"}B= $page->{"paginator"}C= $page->{"paginator"}{"page"} > 1
ABCresult
1XX1
0111
00X0
0100
307 0 ▶ next unless defined $page->{filename} and $page->{filename} =~ /\.html?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page->{"filename"} and $page->{"filename"} =~ /\.html?$/i 0 0 0
Condition0%0/3
defined $page->{"filename"} and $page->{"filename"} =~ /\.html?$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page->{"filename"} and $page->{"filename"} =~ /\.html?$/i
defined $page->{"filename"} $page->{"filename"} =~ /\.html?$/i
Truth table0%0/3
A and B
A= defined $page->{"filename"}B= $page->{"filename"} =~ /\.html?$/i
ABresult
0X0
100
111
308 0 ▶ next if defined $page->{title} and length $page->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"title"} and length $page->{"title"} 0 0 0
Condition0%0/3
defined $page->{"title"} and length $page->{"title"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page->{"title"} and length $page->{"title"}
defined $page->{"title"} length $page->{"title"}
Truth table0%0/3
A and B
A= defined $page->{"title"}B= length $page->{"title"}
ABresult
0X0
100
111
309 push @problems, {
310 severity => 'warning',
311 type => 'front_matter',
312 file => $page->{source},
313 0 message => 'has no title',
314 };
315 }
316  
317 0 return @problems;
318 }
319  
320 =head2 quality
321  
322 Takes a Dapper object that has just been built, and returns warnings about
323 accessibility (images without alt text, skipped heading levels, a page with no
324 language, links with no text or only vague text such as "click here") and
325 search engines (missing, duplicate or overlong titles and descriptions).
326 C<check: { accessibility: false }> or C<check: { seo: false }> in
327 C<_config.yml> turns a group off.
328  
329 =cut
330  
331 190 my %VAGUE = map { $_ => 1 } ('click here', 'click', 'here', 'this', 'link', 'this link', 'this page');
332  
333 0 ▶sub quality {
Subroutine quality: not called CC=51 SCAR=78.8
Pod documented
334 0 my ($dapper) = @_;
335  
336 0 my $site = $dapper->{site};
337 0 ▶ my $settings = ref $site->{check} eq 'HASH' ? $site->{check} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $site->{'check'} eq 'HASH' ? : 0 0 0
338 0 my $a11y = App::Dapper::Publish::enabled($settings, 'accessibility');
339 0 my $seo = App::Dapper::Publish::enabled($settings, 'seo');
340 0 ▶ return unless $a11y or $seo;
Branch: 1 branch
BranchTrueFalseTotal
unless $a11y or $seo 0 0 0
Condition0%0/3
$a11y or $seo
l!l&&r!l&&!r
000
MC/DC0%0/2
$a11y or $seo
$a11y $seo
Truth table0%0/3
A or B
A= $a11yB= $seo
ABresult
1X1
011
000
341  
342 0 my (@problems, %title_of, %description_of, %no_lang);
343 0 ▶ my $warn = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
344 0 my ($type, $file, $message) = @_;
345 0 push @problems, { severity => 'warning', type => $type, file => $file, message => $message };
346 0 };
347  
348 0 for my $page (@{$site->{pages}}) {
349 0 ▶ next unless App::Dapper::Publish::is_html($page);
Branch: 1 branch
BranchTrueFalseTotal
unless App::Dapper::Publish::is_html($page) 0 0 0
350 0 ▶ next if $page->{paginator} && $page->{paginator}->{page} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"paginator"} and $page->{"paginator"}{"page"} > 1 0 0 0
Condition0%0/3
$page->{"paginator"} and $page->{"paginator"}{"page"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$page->{"paginator"} and $page->{"paginator"}{"page"} > 1
$page->{"paginator"} $page->{"paginator"}{"page"} > 1
Truth table0%0/3
A and B
A= $page->{"paginator"}B= $page->{"paginator"}{"page"} > 1
ABresult
0X0
100
111
351 # Pages that failed to render, or lost their file to another page, are
352 # reported already
353 0 ▶ next unless -f $page->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $page->{"filename"} 0 0 0
354 0 my $owner = $dapper->{page_owner}->{$page->{filename}};
355 0 ▶ next if defined $owner and $owner ne $page->{source};
Branch: 1 branch
BranchTrueFalseTotal
if defined $owner and $owner ne $page->{"source"} 0 0 0
Condition0%0/3
defined $owner and $owner ne $page->{"source"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $owner and $owner ne $page->{"source"}
defined $owner $owner ne $page->{"source"}
Truth table0%0/3
A and B
A= defined $ownerB= $owner ne $page->{"source"}
ABresult
0X0
100
111
356 0 my $html = App::Dapper::Utils::read_file($page->{filename});
357 0 $html =~ s/<!--.*?-->//gs;
358 0 $html =~ s{<(script|style|pre|code)\b.*?</\1>}{}gis;
359 0 my $file = $page->{source};
360  
361 0 ▶ if ($a11y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a11y) 0 0 0
362 0 my %alt_missing;
363  
364 # Markdown writes ![](photo.png) as alt="", which looks deliberate in
365 # the HTML, so look for it in the Markdown
366 0 ▶ my $markdown = defined $page->{markdown} ? $page->{markdown} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'markdown'} ? : 0 0 0
367 0 $markdown =~ s/^([ ]{0,3})(`{3,}|~{3,}).*?^[ ]{0,3}\2[`~]*[ \t]*$//gms;
368 0 $markdown =~ s/`[^`\n]*`//g;
369 0 ▶ while ($markdown =~ /!\[\s*\]\(\s*<?([^)\s>]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($markdown =~ /!\[\s*\]\(\s*<?([^)\s>]+)/g) 0 0 0
370 $warn->('accessibility', $file, "the image $1 has no alt text; describe it, or use alt=\"\" if it is decoration")
371 0 ▶ unless $alt_missing{$1}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $alt_missing{$1}++ 0 0 0
372 }
373  
374 0 ▶ while ($html =~ /<img\b([^>]*)>/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<img\b([^>]*)>/gi) 0 0 0
375 0 my $attributes = $1;
376 0 ▶ next if $attributes =~ /(?:^|\s)alt(?:\s*=|\s|\/|$)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes =~ m[(?:^|\s)alt(?:\s*=|\s|/|$)]i 0 0 0
377 0 my ($src) = $attributes =~ /\bsrc\s*=\s*["']([^"']*)/i;
378 0 ▶ $src = '(no src)' unless defined $src;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $src 0 0 0
379 $warn->('accessibility', $file, "the image $src has no alt text; describe it, or use alt=\"\" if it is decoration")
380 0 ▶ unless $alt_missing{$src}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $alt_missing{$src}++ 0 0 0
381 }
382  
383 0 my $previous;
384 0 ▶ while ($html =~ /<h([1-6])\b[^>]*>(.*?)<\/h\1>/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[<h([1-6])\b[^>]*>(.*?)</h\1>]sig) 0 0 0
385 0 my ($level, $text) = ($1, App::Dapper::Publish::plain_text($2));
386 0 ▶ if (defined $previous and $level > $previous + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $previous and $level > $previous + 1) 0 0 0
Condition0%0/3
defined $previous and $level > $previous + 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $previous and $level > $previous + 1
defined $previous $level > $previous + 1
Truth table0%0/3
A and B
A= defined $previousB= $level > $previous + 1
ABresult
0X0
100
111
387 0 $warn->('accessibility', $file, "the heading \"$text\" skips from h$previous to h$level; use h" . ($previous + 1));
388 0 last;
389 }
390 0 $previous = $level;
391 }
392 0 pos($html) = undef;
393  
394 0 ▶ if ($html =~ /<html\b([^>]*)>/i and $1 !~ /\blang\s*=/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /<html\b([^>]*)>/i and not $1 =~ /\blang\s*=/i) 0 0 0
Condition0%0/3
$html =~ /<html\b([^>]*)>/i and not $1 =~ /\blang\s*=/i
!ll&&!rl&&r
000
MC/DC0%0/2
$html =~ /<html\b([^>]*)>/i and not $1 =~ /\blang\s*=/i
$html =~ /<html\b([^>]*)>/i not $1 =~ /\blang\s*=/i
Truth table0%0/3
A and not(B)
A= $html =~ /<html\b([^>]*)>/iB= not $1 =~ /\blang\s*=/i
ABresult
0X0
100
111
395 0 ▶ my $name = $page->{layout} || 'index';
Condition0%0/2
$page->{'layout'} || 'index'
l!l
00
MC/DC0%0/1
$page->{'layout'} || 'index'
$page->{'layout'}
Truth table0%0/2
A
A= $page->{'layout'}
Aresult
11
00
396 0 my ($layout) = grep { App::Dapper::Utils::filter_stem($_) eq $name } App::Dapper::Utils::read_dir($dapper->{layout});
397 0 ▶ $layout = "$dapper->{layout}/" . (defined $layout ? $layout : $name);
Branch: 1 branch
BranchTrueFalseTotal
defined $layout ? : 0 0 0
398 $warn->('accessibility', $layout, 'the <html> tag has no lang attribute (for example <html lang="en">), so screen readers may read the page in the wrong language')
399 0 ▶ unless $no_lang{$layout}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_lang{$layout}++ 0 0 0
400 }
401  
402 0 my %vague;
403 0 ▶ while ($html =~ /<a\b([^>]*)>(.*?)<\/a>/gis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ m[<a\b([^>]*)>(.*?)</a>]sig) 0 0 0
404 0 my ($attributes, $inner) = ($1, $2);
405 0 ▶ next unless $attributes =~ /\bhref\s*=/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $attributes =~ /\bhref\s*=/i 0 0 0
406 0 ▶ next if $attributes =~ /\baria-label(?:ledby)?\s*=/i;
Branch: 1 branch
BranchTrueFalseTotal
if $attributes =~ /\baria-label(?:ledby)?\s*=/i 0 0 0
407 0 my $text = lc App::Dapper::Publish::plain_text($inner);
408 0 $text =~ s/[.!:\s]+$//;
409 0 my ($href) = $attributes =~ /\bhref\s*=\s*["']([^"']*)/i;
410 0 ▶ $href = '' unless defined $href;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $href 0 0 0
411 0 ▶ if (not length $text) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $text) { } 0 0 0
elsif ($VAGUE{$text}) { } 0 0 0
412 0 ▶ next if $inner =~ /<img\b[^>]*\balt\s*=\s*["'][^"']+/i or $attributes =~ /\btitle\s*=\s*["'][^"']+/i;
Branch: 1 branch
BranchTrueFalseTotal
if $inner =~ /<img\b[^>]*\balt\s*=\s*["'][^"']+/i or $attributes =~ /\btitle\s*=\s*["'][^"']+/i 0 0 0
Condition0%0/3
$inner =~ /<img\b[^>]*\balt\s*=\s*["'][^"']+/i or $attributes =~ /\btitle\s*=\s*["'][^"']+/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$inner =~ /<img\b[^>]*\balt\s*=\s*["'][^"']+/i or $attributes =~ /\btitle\s*=\s*["'][^"']+/i
$inner =~ /<img\b[^>]*\balt\s*=\s*["'][^"']+/i $attributes =~ /\btitle\s*=\s*["'][^"']+/i
Truth table0%0/3
A or B
A= $inner =~ /<img\b[^>]*\balt\s*=\s*["'][^"']+/iB= $attributes =~ /\btitle\s*=\s*["'][^"']+/i
ABresult
1X1
011
000
413 $warn->('accessibility', $file, "the link to $href has no text; add text, alt text to its image, or an aria-label")
414 0 ▶ unless $vague{"empty $href"}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $vague{"empty $href"}++ 0 0 0
415 }
416 elsif ($VAGUE{$text}) {
417 $warn->('accessibility', $file, "the link to $href says only \"$text\"; say where it goes instead")
418 0 ▶ unless $vague{"vague $text $href"}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $vague{"vague $text $href"}++ 0 0 0
419 }
420 }
421 }
422  
423 0 ▶ next unless $seo;
Branch: 1 branch
BranchTrueFalseTotal
unless $seo 0 0 0
424 0 ▶ next if $page->{generated} or $page->{url} =~ m{(?:^|/)404\.html$};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"generated"} or $page->{"url"} =~ m[(?:^|/)404\.html$] 0 0 0
Condition0%0/3
$page->{"generated"} or $page->{"url"} =~ m[(?:^|/)404\.html$]
l!l&&r!l&&!r
000
MC/DC0%0/2
$page->{"generated"} or $page->{"url"} =~ m[(?:^|/)404\.html$]
$page->{"generated"} $page->{"url"} =~ m[(?:^|/)404\.html$]
Truth table0%0/3
A or B
A= $page->{"generated"}B= $page->{"url"} =~ m[(?:^|/)404\.html$]
ABresult
1X1
011
000
425 0 ▶ next if defined $page->{sitemap} and !App::Dapper::Publish::enabled($page, 'sitemap');
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"sitemap"} and not App::Dapper::Publish::enabled($page, "sitemap") 0 0 0
Condition0%0/3
defined $page->{"sitemap"} and not App::Dapper::Publish::enabled($page, "sitemap")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page->{"sitemap"} and not App::Dapper::Publish::enabled($page, "sitemap")
defined $page->{"sitemap"} not App::Dapper::Publish::enabled($page, "sitemap")
Truth table0%0/3
A and not(B)
A= defined $page->{"sitemap"}B= not App::Dapper::Publish::enabled($page, "sitemap")
ABresult
0X0
100
111
426  
427 0 my ($title) = $html =~ /<title\b[^>]*>(.*?)<\/title>/is;
428 0 ▶ if (defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) 0 0 0
429 0 $title = App::Dapper::Publish::plain_text($title);
430 0 ▶ if (length $title > 70) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $title > 70) 0 0 0
431 0 $warn->('seo', $file, 'the title is ' . length($title) . ' characters long; search engines show about 60');
432 }
433 0 ▶ if (length $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $title) 0 0 0
434 0 ▶ if (defined(my $other = $title_of{$title})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $other = $title_of{$title})) { } 0 0 0
435 0 $warn->('seo', $file, "has the same title as $other (\"$title\"); give each page its own");
436 }
437 else {
438 0 $title_of{$title} = $file;
439 }
440 }
441 }
442  
443 0 my $description = App::Dapper::Publish::description($page);
444 0 ▶ if (not length $description) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $description) { } 0 0 0
445 0 $warn->('seo', $file, 'has no description; add description to its front matter, or start it with a paragraph');
446 }
447 else {
448 0 ▶ my $own = defined $page->{description} && !ref $page->{description} ? $page->{description} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'description'} && !ref($page->{'description'}) ? : 0 0 0
Condition0%0/3
defined $page->{"description"} and not ref $page->{"description"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page->{"description"} and not ref $page->{"description"}
defined $page->{"description"} not ref $page->{"description"}
Truth table0%0/3
A and not(B)
A= defined $page->{"description"}B= not ref $page->{"description"}
ABresult
0X0
100
111
449 0 ▶ $warn->('seo', $file, 'the description is ' . length($own) . ' characters long; search engines show about 160')
Branch: 1 branch
BranchTrueFalseTotal
if length $own > 160 0 0 0
450 if length $own > 160;
451 0 ▶ if (defined(my $other = $description_of{$description})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $other = $description_of{$description})) { } 0 0 0
452 0 $warn->('seo', $file, "has the same description as $other; give each page its own");
453 }
454 else {
455 0 $description_of{$description} = $file;
456 }
457 }
458 }
459  
460 0 return @problems;
461 }
462  
463 =head2 text
464  
465 Takes a report from L</run> and returns it as text for people.
466  
467 =cut
468  
469 0 ▶sub text {
Subroutine text: not called CC=10 SCAR=47.0
Pod documented
470 0 my ($report) = @_;
471  
472 0 my $out = '';
473 0 for my $problem (@{$report->{errors}}) {
474 0 $out .= "error: $problem->{file}: $problem->{message}\n";
475 }
476 0 for my $problem (@{$report->{warnings}}) {
477 0 $out .= "warning: $problem->{file}: $problem->{message}\n";
478 }
479  
480 0 my $errors = @{$report->{errors}};
481 0 my $warnings = @{$report->{warnings}};
482 0 ▶ my $held = @{ $report->{held} || [] };
Condition0%0/2
$report->{'held'} || []
l!l
00
MC/DC0%0/1
$report->{'held'} || []
$report->{'held'}
Truth table0%0/2
A
A= $report->{'held'}
Aresult
11
00
483 0 ▶ my $drafts = grep { $_->{reason} eq 'draft' } @{ $report->{held} || [] };
Condition0%0/2
$report->{'held'} || []
l!l
00
MC/DC0%0/1
$report->{'held'} || []
$report->{'held'}
Truth table0%0/2
A
A= $report->{'held'}
Aresult
11
00
484 0 ▶ $out .= sprintf("Not published: %d draft%s, %d scheduled (use --drafts to include them).\n",
Branch: 2 branches
BranchTrueFalseTotal
if $held 0 0 0
$drafts == 1 ? : 0 0 0
485 $drafts, $drafts == 1 ? '' : 's', $held - $drafts) if $held;
486 $out .= sprintf("Checked %d page%s: %d error%s, %d warning%s.\n",
487 0 ▶ $report->{pages}, $report->{pages} == 1 ? '' : 's',
Branch: 3 branches
BranchTrueFalseTotal
$report->{'pages'} == 1 ? : 0 0 0
$errors == 1 ? : 0 0 0
$warnings == 1 ? : 0 0 0
488 $errors, $errors == 1 ? '' : 's',
489 $warnings, $warnings == 1 ? '' : 's');
490  
491 0 return $out;
492 }
493  
494 190 1;
↑ Summary lib/App/Dapper/Import.pm »