lib/App/Dapper/Publish.pm

filter: Statementstmt 100% 274 / 274 Branchbran 98.3% 175 / 178 Conditioncond 96.6% 114 / 118 MC/DCmcdc 94.7% 72 / 76 Subroutinesub 100% 33 / 33 Podpod 100% 20 / 20 totaltotal 98.4% 688 / 699 CC 139 SCAR 26.8
CC
139
Cov
99%
seo
40.0
feed
11.0
has_twin
11.0
CRAP 14.5
« lib/App/Dapper/Components.pm ↑ Summary lib/App/Dapper/Collections.pm »
1 package App::Dapper::Publish;
2  
3 =head1 NAME
4  
5 App::Dapper::Publish - Sitemaps, feeds, Markdown copies, llms.txt and SEO tags.
6  
7 =head1 DESCRIPTION
8  
9 After the pages of a site are rendered, Dapper writes files that help search
10 engines, feed readers and AI tools find and read them. Each is controlled by a
11 setting in C<_config.yml>:
12  
13 url: https://example.com # needed for absolute links in all of these
14 markdown_twins: true # page.md next to each Markdown page's HTML
15 llms_txt: true # /llms.txt and /llms-full.txt
16 sitemap: true # /sitemap.xml
17 feed: # /feed.xml (off unless set)
18 category: blog
19 limit: 20
20  
21 Redirects (C<redirects> in C<_config.yml>, or C<_data/redirects.yml>) are written as
22 small pages at the old URLs.
23  
24 It also builds C<page.seo>, the tags a layout can put in its C<< <head> >>:
25 a description, a canonical link, Open Graph and Twitter tags, a feed link,
26 and JSON-LD structured data.
27  
28 =cut
29  
30 191 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 191 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 191 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 191 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 191 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 191 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 191 ▶use App::Dapper::Filters;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 191 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 191 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 =head2 absolute_url
43  
44 Takes the site and a URL relative to the site root (e.g. C</about/>) and
45 returns it with C<site.url> in front, or unchanged if C<site.url> is not set
46 or the URL is already absolute.
47  
48 =cut
49  
50 101 ▶sub absolute_url {
Subroutine absolute_url: called CC=7 SCAR=19.5
Pod documented
51 101 my ($site, $url) = @_;
52  
53 101 ▶ return $url unless defined $url and length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 2 99 101
Condition100%3/3
defined $url and length $url
!ll&&!rl&&r
1199
MC/DC100%2/2
defined $url and length $url
defined $url length $url
Truth table100%3/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
54 99 ▶ return $url if $url =~ m{^[a-z][a-z0-9+.-]*:}i;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^[a-z][a-z0-9+.-]*:/i 6 93 99
55  
56 93 my $base = $site->{url};
57 93 ▶ return $url unless defined $base and length $base;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base and length $base 45 48 93
Condition100%3/3
defined $base and length $base
!ll&&!rl&&r
44148
MC/DC100%2/2
defined $base and length $base
defined $base length $base
Truth table100%3/3
A and B
A= defined $baseB= length $base
ABresult
0X0
100
111
58  
59 48 $base =~ s{/+$}{};
60 48 ▶ $url = "/$url" unless $url =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^/] 2 46 48
61 48 return "$base$url";
62 }
63  
64 =head2 plain_text
65  
66 Takes HTML and returns its text: tags removed, common entities decoded and
67 white space collapsed.
68  
69 =cut
70  
71 59 ▶sub plain_text {
Subroutine plain_text: called CC=2 SCAR=6.9
Pod documented
72 59 my ($html) = @_;
73  
74 59 ▶ return '' unless defined $html;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 39 20 59
75  
76 20 my $text = $html;
77 20 $text =~ s/<[^>]*>//g;
78 20 $text =~ s/&nbsp;/ /g;
79 20 $text =~ s/&lt;/</g;
80 20 $text =~ s/&gt;/>/g;
81 20 $text =~ s/&quot;/"/g;
82 20 $text =~ s/&#39;/'/g;
83 20 $text =~ s/&amp;/&/g;
84 20 $text =~ s/\s+/ /g;
85 20 $text =~ s/^ | $//g;
86  
87 20 return $text;
88 }
89  
90 =head2 description
91  
92 Takes a page and returns its description: its C<description> setting, or the
93 text of its excerpt, shortened to about 200 characters.
94  
95 =cut
96  
97 63 ▶sub description {
Subroutine description: called CC=4 SCAR=13.9
Pod documented
98 63 my ($page) = @_;
99  
100 my $text = defined $page->{description} && !ref $page->{description}
101 ? $page->{description}
102 63 ▶ : plain_text($page->{excerpt});
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'description'} && !ref($page->{'description'}) ? : 14 49 63
Condition100%3/3
defined $page->{"description"} and not ref $page->{"description"}
!ll&&!rl&&r
47214
MC/DC100%2/2
defined $page->{"description"} and not ref $page->{"description"}
defined $page->{"description"} not ref $page->{"description"}
Truth table100%3/3
A and not(B)
A= defined $page->{"description"}B= not ref $page->{"description"}
ABresult
0X0
100
111
103  
104 63 ▶ if (length $text > 200) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text > 200) 4 59 63
105 4 $text = substr($text, 0, 200);
106 4 $text =~ s/\s+\S*$//;
107 4 $text .= '...';
108 }
109  
110 63 return $text;
111 }
112  
113 =head2 is_html
114  
115 Returns true if a page's output is an HTML file.
116  
117 =cut
118  
119 68 ▶sub is_html {
Subroutine is_html: called CC=2 SCAR=6.9
Pod documented
120 68 my ($page) = @_;
121 68 ▶ return defined $page->{filename} && $page->{filename} =~ /\.html?$/i;
Condition100%3/3
defined $page->{'filename'} && $page->{'filename'} =~ /\.html?$/i
!ll&&!rl&&r
11156
MC/DC100%2/2
defined $page->{'filename'} && $page->{'filename'} =~ /\.html?$/i
defined $page->{'filename'} $page->{'filename'} =~ /\.html?$/i
Truth table100%3/3
A && B
A= defined $page->{'filename'}B= $page->{'filename'} =~ /\.html?$/i
ABresult
0X0
100
111
122 }
123  
124 =head2 enabled
125  
126 Takes the site and the name of a setting, and returns true unless the setting
127 is off (false, 0, "false" or "no").
128  
129 =cut
130  
131 69 ▶sub enabled {
Subroutine enabled: called CC=6 SCAR=17.9
Pod documented
132 69 my ($site, $name) = @_;
133  
134 69 my $value = $site->{$name};
135 69 ▶ return 1 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 17 52 69
136 52 ▶ return 0 if !ref $value and $value =~ /^(?:0|false|no|off|)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $value and $value =~ /^(?:0|false|no|off|)$/i 23 29 52
Condition100%3/3
not ref $value and $value =~ /^(?:0|false|no|off|)$/i
!ll&&!rl&&r
42523
MC/DC100%2/2
not ref $value and $value =~ /^(?:0|false|no|off|)$/i
not ref $value $value =~ /^(?:0|false|no|off|)$/i
Truth table100%3/3
not(A) and B
A= not ref $valueB= $value =~ /^(?:0|false|no|off|)$/i
ABresult
0X0
100
111
137 # A boolean object, such as JSON::false, is off when it is false
138 29 ▶ return 0 if Scalar::Util::blessed($value) and !$value;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and not $value 1 28 29
Condition100%3/3
builtin::blessed($value) and not $value
!ll&&!rl&&r
2711
MC/DC100%2/2
builtin::blessed($value) and not $value
builtin::blessed($value) not $value
Truth table100%3/3
A and not(B)
A= builtin::blessed($value)B= not $value
ABresult
0X0
100
111
139 28 return 1;
140 }
141  
142 =head2 is_draft
143  
144 Returns true if a page's front matter says it is a draft (C<draft: true>).
145  
146 =cut
147  
148 10 ▶sub is_draft {
Subroutine is_draft: called CC=2 SCAR=6.9
Pod documented
149 10 my ($page) = @_;
150 10 ▶ return defined $page->{draft} && enabled($page, 'draft');
Condition100%3/3
defined $page->{'draft'} && enabled($page, 'draft')
!ll&&!rl&&r
532
MC/DC100%2/2
defined $page->{'draft'} && enabled($page, 'draft')
defined $page->{'draft'} enabled($page, 'draft')
Truth table100%3/3
A && B
A= defined $page->{'draft'}B= enabled($page, 'draft')
ABresult
0X0
100
111
151 }
152  
153 =head2 twin_path
154  
155 Takes a page and returns the output path of its Markdown copy:
156 C<blog/post/index.html> becomes C<blog/post/index.md>.
157  
158 =cut
159  
160 6 ▶sub twin_path {
Subroutine twin_path: called CC=1 SCAR=0.0
Pod documented
161 6 my ($page) = @_;
162  
163 6 (my $path = $page->{filename}) =~ s/\.[^.\/]+$/.md/;
164 6 return $path;
165 }
166  
167 =head2 twin_url
168  
169 Takes a page and returns the URL of its Markdown copy: C</blog/post/>
170 becomes C</blog/post/index.md>.
171  
172 =cut
173  
174 17 ▶sub twin_url {
Subroutine twin_url: called CC=2 SCAR=6.9
Pod documented
175 17 my ($page) = @_;
176  
177 17 my $url = $page->{url};
178 17 ▶ return $url . 'index.md' if $url =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[/$] 5 12 17
179 12 $url =~ s/\.[^.\/]+$/.md/;
180 12 return $url;
181 }
182  
183 =head2 has_twin
184  
185 Returns true if a page gets a Markdown copy: it is an HTML page made from a
186 Markdown file, it has not turned it off (C<markdown_twin: false>), and its
187 Markdown is not a template (which would show template code, not text).
188  
189 =cut
190  
191 16 ▶sub has_twin {
Subroutine has_twin: called CC=11 SCAR=24.0
Pod documented
192 16 my ($site, $page) = @_;
193  
194 16 ▶ return 0 unless enabled($site, 'markdown_twins');
Branch: 1 branch
BranchTrueFalseTotal
unless enabled($site, "markdown_twins") 2 14 16
195 14 ▶ return 0 unless ($page->{source_file_extension} || '') eq '.md';
Branch: 1 branch
BranchTrueFalseTotal
unless ($page->{"source_file_extension"} || "") eq ".md" 2 12 14
Condition100%2/2
$page->{'source_file_extension'} || ''
l!l
131
MC/DC100%1/1
$page->{'source_file_extension'} || ''
$page->{'source_file_extension'}
Truth table100%2/2
A
A= $page->{'source_file_extension'}
Aresult
11
00
196 12 ▶ return 0 unless is_html($page);
Branch: 1 branch
BranchTrueFalseTotal
unless is_html($page) 1 11 12
197 11 ▶ return 0 if defined $page->{markdown_twin} and !enabled($page, 'markdown_twin');
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"markdown_twin"} and not enabled($page, "markdown_twin") 1 10 11
Condition100%3/3
defined $page->{"markdown_twin"} and not enabled($page, "markdown_twin")
!ll&&!rl&&r
911
MC/DC100%2/2
defined $page->{"markdown_twin"} and not enabled($page, "markdown_twin")
defined $page->{"markdown_twin"} not enabled($page, "markdown_twin")
Truth table100%3/3
A and not(B)
A= defined $page->{"markdown_twin"}B= not enabled($page, "markdown_twin")
ABresult
0X0
100
111
198 10 ▶ return 0 unless defined $page->{markdown};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page->{"markdown"} 1 9 10
199  
200 9 ▶ my $templated = $page->{template} || $site->{templates_in_content};
Condition100%3/3
$page->{'template'} || $site->{'templates_in_content'}
l!l&&r!l&&!r
216
MC/DC100%2/2
$page->{'template'} || $site->{'templates_in_content'}
$page->{'template'} $site->{'templates_in_content'}
Truth table100%3/3
A || B
A= $page->{'template'}B= $site->{'templates_in_content'}
ABresult
1X1
011
000
201 9 ▶ return 0 if $templated and $page->{markdown} =~ /\[%/;
Branch: 1 branch
BranchTrueFalseTotal
if $templated and $page->{"markdown"} =~ /\[%/ 2 7 9
Condition100%3/3
$templated and $page->{"markdown"} =~ /\[%/
!ll&&!rl&&r
612
MC/DC100%2/2
$templated and $page->{"markdown"} =~ /\[%/
$templated $page->{"markdown"} =~ /\[%/
Truth table100%3/3
A and B
A= $templatedB= $page->{"markdown"} =~ /\[%/
ABresult
0X0
100
111
202  
203 7 return 1;
204 }
205  
206 =head2 twin
207  
208 Takes the site and a page, and returns the text of its Markdown copy: the
209 title as a heading, the description as a quote, and the page's Markdown.
210  
211 =cut
212  
213 13 ▶sub twin {
Subroutine twin: called CC=6 SCAR=17.9
Pod documented
214 13 my ($site, $page) = @_;
215  
216 13 my $text = '';
217 13 ▶ $text .= "# $page->{title}\n\n" if defined $page->{title} and length $page->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"title"} and length $page->{"title"} 11 2 13
Condition100%3/3
defined $page->{"title"} and length $page->{"title"}
!ll&&!rl&&r
1111
MC/DC100%2/2
defined $page->{"title"} and length $page->{"title"}
defined $page->{"title"} length $page->{"title"}
Truth table100%3/3
A and B
A= defined $page->{"title"}B= length $page->{"title"}
ABresult
0X0
100
111
218  
219 13 ▶ my $description = defined $page->{description} && !ref $page->{description} ? $page->{description} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'description'} && !ref($page->{'description'}) ? : 3 10 13
Condition100%3/3
defined $page->{"description"} and not ref $page->{"description"}
!ll&&!rl&&r
913
MC/DC100%2/2
defined $page->{"description"} and not ref $page->{"description"}
defined $page->{"description"} not ref $page->{"description"}
Truth table100%3/3
A and not(B)
A= defined $page->{"description"}B= not ref $page->{"description"}
ABresult
0X0
100
111
220 13 ▶ $text .= "> $description\n\n" if length $description;
Branch: 1 branch
BranchTrueFalseTotal
if length $description 2 11 13
221  
222 13 my $body = $page->{markdown};
223 13 $body =~ s/^\s+//;
224 13 $body =~ s/\s+\z//;
225  
226 13 return "$text$body\n";
227 }
228  
229 =head2 llms_txt
230  
231 Takes the site and the pages that have Markdown copies, and returns the text
232 of C</llms.txt>: the site's name and description, then a list of links to the
233 Markdown copies, grouped by category (newest first), with other pages last.
234  
235 =cut
236  
237 6 ▶sub llms_txt {
Subroutine llms_txt: called CC=10 SCAR=23.0
Pod documented
238 6 my ($site, $pages) = @_;
239  
240 6 $pages = [ grep { _in_llms($_) } @$pages ];
241  
242 6 ▶ my $text = "# " . (defined $site->{name} ? $site->{name} : 'Site') . "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $site->{'name'} ? : 5 1 6
243 6 ▶ $text .= "> $site->{description}\n\n" if defined $site->{description} and length $site->{description};
Branch: 1 branch
BranchTrueFalseTotal
if defined $site->{"description"} and length $site->{"description"} 1 5 6
Condition100%3/3
defined $site->{"description"} and length $site->{"description"}
!ll&&!rl&&r
411
MC/DC100%2/2
defined $site->{"description"} and length $site->{"description"}
defined $site->{"description"} length $site->{"description"}
Truth table100%3/3
A and B
A= defined $site->{"description"}B= length $site->{"description"}
ABresult
0X0
100
111
244  
245 6 my (%by_category, @other, %seen);
246 6 for my $page (@$pages) {
247 10 my $category = _first_category($page);
248 10 ▶ if (defined $category) { push @{$by_category{$category}}, $page }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $category) { } 3 7 10
249 7 else { push @other, $page }
250 }
251  
252 6 for my $category (sort keys %by_category) {
253 2 $text .= "## " . ucfirst($category) . "\n\n";
254 2 for my $page (sort { $b->{date} <=> $a->{date} } @{$by_category{$category}}) {
255 3 $text .= _llms_line($site, $page);
256 }
257 2 $text .= "\n";
258 }
259  
260 6 ▶ if (@other) {
Branch: 1 branch
BranchTrueFalseTotal
if (@other) 5 1 6
261 5 $text .= "## Pages\n\n";
262 5 $text .= _llms_line($site, $_) for @other;
263 5 $text .= "\n";
264 }
265  
266 6 return $text;
267 }
268  
269 17 ▶sub _in_llms {
Subroutine _in_llms: called CC=2 SCAR=6.9
270 17 my ($page) = @_;
271 17 ▶ return !(defined $page->{llms} and !enabled($page, 'llms'));
Condition100%3/3
defined $page->{'llms'} && !enabled($page, 'llms')
!ll&&!rl&&r
1412
MC/DC100%2/2
defined $page->{'llms'} && !enabled($page, 'llms')
defined $page->{'llms'} !enabled($page, 'llms')
Truth table100%3/3
A && not(B)
A= defined $page->{'llms'}B= !enabled($page, 'llms')
ABresult
0X0
100
111
272 }
273  
274 10 ▶sub _llms_line {
Subroutine _llms_line: called CC=4 SCAR=13.9
275 10 my ($site, $page) = @_;
276  
277 10 ▶ my $title = defined $page->{title} && length $page->{title} ? $page->{title} : $page->{url};
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'title'} && length $page->{'title'} ? : 9 1 10
Condition66%2/3
defined $page->{"title"} and length $page->{"title"}
!ll&&!rl&&r
019
MC/DC50%1/2
defined $page->{"title"} and length $page->{"title"}
defined $page->{"title"} length $page->{"title"}
Truth table66%2/3
A and B
A= defined $page->{"title"}B= length $page->{"title"}
ABresult
0X0
100
111
278 10 my $line = "- [$title](" . absolute_url($site, twin_url($page)) . ")";
279 10 my $description = description($page);
280 10 ▶ $line .= ": $description" if length $description;
Branch: 1 branch
BranchTrueFalseTotal
if length $description 7 3 10
281 10 return "$line\n";
282 }
283  
284 46 ▶sub _first_category {
Subroutine _first_category: called CC=6 SCAR=17.9
285 46 my ($page) = @_;
286  
287 46 my $categories = $page->{categories};
288 46 ▶ return $categories->[0] if ref $categories eq 'ARRAY' and @$categories;
Branch: 1 branch
BranchTrueFalseTotal
if ref $categories eq "ARRAY" and @$categories 6 40 46
Condition100%3/3
ref $categories eq "ARRAY" and @$categories
!ll&&!rl&&r
3736
MC/DC100%2/2
ref $categories eq "ARRAY" and @$categories
ref $categories eq "ARRAY" @$categories
Truth table100%3/3
A and B
A= ref $categories eq "ARRAY"B= @$categories
ABresult
0X0
100
111
289 40 ▶ return $categories if defined $categories and !ref $categories and length $categories;
Branch: 1 branch
BranchTrueFalseTotal
if defined $categories and not ref $categories and length $categories 3 37 40
Condition100%6/6
defined $categories and not ref $categories and length $categories
!ll&&!rl&&r
3613
defined $categories and not ref $categories
!ll&&!rl&&r
3244
MC/DC100%3/3
defined $categories and not ref $categories and length $categories
defined $categories not ref $categories length $categories
Truth table100%4/4
A and not(B) and C
A= defined $categoriesB= not ref $categoriesC= length $categories
ABCresult
0XX0
10X0
1100
1111
290 37 return;
291 }
292  
293 =head2 llms_full_txt
294  
295 Takes the site and the pages that have Markdown copies, and returns the text
296 of C</llms-full.txt>: every Markdown copy, one after another, each with its
297 URL.
298  
299 =cut
300  
301 5 ▶sub llms_full_txt {
Subroutine llms_full_txt: called CC=5 SCAR=16.1
Pod documented
302 5 my ($site, $pages) = @_;
303  
304 5 $pages = [ grep { _in_llms($_) } @$pages ];
305  
306 5 ▶ my $text = "# " . (defined $site->{name} ? $site->{name} : 'Site') . "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $site->{'name'} ? : 4 1 5
307 5 ▶ $text .= "> $site->{description}\n\n" if defined $site->{description} and length $site->{description};
Branch: 1 branch
BranchTrueFalseTotal
if defined $site->{"description"} and length $site->{"description"} 1 4 5
Condition66%2/3
defined $site->{"description"} and length $site->{"description"}
!ll&&!rl&&r
401
MC/DC50%1/2
defined $site->{"description"} and length $site->{"description"}
defined $site->{"description"} length $site->{"description"}
Truth table66%2/3
A and B
A= defined $site->{"description"}B= length $site->{"description"}
ABresult
0X0
100
111
308  
309 5 for my $page (@$pages) {
310 5 $text .= "---\n\nURL: " . absolute_url($site, $page->{url}) . "\n\n" . twin($site, $page) . "\n";
311 }
312  
313 5 return $text;
314 }
315  
316 =head2 sitemap
317  
318 Takes the site and its pages, and returns the text of C</sitemap.xml>,
319 listing every HTML page that has not turned it off with C<sitemap: false>.
320 Needs C<site.url>.
321  
322 =cut
323  
324 3 ▶sub sitemap {
Subroutine sitemap: called CC=7 SCAR=19.5
Pod documented
325 3 my ($site, $pages) = @_;
326  
327 3 my $xml = qq{<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n};
328  
329 3 for my $page (@$pages) {
330 11 ▶ next unless is_html($page);
Branch: 1 branch
BranchTrueFalseTotal
unless is_html($page) 1 10 11
331 10 ▶ next if defined $page->{sitemap} and !enabled($page, 'sitemap');
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"sitemap"} and not enabled($page, "sitemap") 1 9 10
Condition100%3/3
defined $page->{"sitemap"} and not enabled($page, "sitemap")
!ll&&!rl&&r
811
MC/DC100%2/2
defined $page->{"sitemap"} and not enabled($page, "sitemap")
defined $page->{"sitemap"} not enabled($page, "sitemap")
Truth table100%3/3
A and not(B)
A= defined $page->{"sitemap"}B= not enabled($page, "sitemap")
ABresult
0X0
100
111
332 9 ▶ next if $page->{url} =~ m{(?:^|/)404\.html$};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"url"} =~ m[(?:^|/)404\.html$] 2 7 9
333  
334 7 $xml .= " <url>\n";
335 7 $xml .= " <loc>" . App::Dapper::Filters::xml_escape(absolute_url($site, $page->{url})) . "</loc>\n";
336 7 my $modified = _modified($page);
337 7 ▶ $xml .= " <lastmod>" . $modified->xmlschema . "</lastmod>\n" if $modified;
Branch: 1 branch
BranchTrueFalseTotal
if $modified 5 2 7
338 7 $xml .= " </url>\n";
339 }
340  
341 3 $xml .= "</urlset>\n";
342 3 return $xml;
343 }
344  
345 17 ▶sub _modified {
Subroutine _modified: called CC=5 SCAR=16.1
346 17 my ($page) = @_;
347  
348 17 ▶ my $date = defined $page->{updated} ? eval { App::Dapper::Utils::parse_date($page->{updated}) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'updated'} ? : 7 10 17
349 17 ▶ $date = $page->{date} unless $date;
Branch: 1 branch
BranchTrueFalseTotal
unless $date 12 5 17
350 17 ▶ return ref $date && eval { $date->can('xmlschema') } ? $date : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $date && eval { do { $date->can('xmlschema') } } ? : 15 2 17
Condition66%2/3
ref $date and eval { do { $date->can("xmlschema") } }
!ll&&!rl&&r
2015
MC/DC50%1/2
ref $date and eval { do { $date->can("xmlschema") } }
ref $date eval { do { $date->can("xmlschema") } }
Truth table66%2/3
A and B
A= ref $dateB= eval { do { $date->can("xmlschema") } }
ABresult
0X0
100
111
351 }
352  
353 =head2 feed_settings
354  
355 Takes the site and returns the feed settings (C<path>, C<category>, C<limit>,
356 C<title>) if a feed is turned on, or nothing if it is not. C<feed: true>
357 turns on a feed of every page with a category.
358  
359 =cut
360  
361 52 ▶sub feed_settings {
Subroutine feed_settings: called CC=9 SCAR=22.0
Pod documented
362 52 my ($site) = @_;
363  
364 52 my $feed = $site->{feed};
365 52 ▶ return unless defined $feed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $feed 37 15 52
366 15 ▶ return unless ref $feed or enabled($site, 'feed');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $feed or enabled($site, "feed") 4 11 15
Condition100%3/3
ref $feed or enabled($site, "feed")
l!l&&r!l&&!r
744
MC/DC100%2/2
ref $feed or enabled($site, "feed")
ref $feed enabled($site, "feed")
Truth table100%3/3
A or B
A= ref $feedB= enabled($site, "feed")
ABresult
1X1
011
000
367  
368 11 ▶ my %settings = ref $feed eq 'HASH' ? %$feed : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $feed eq 'HASH' ? : 6 5 11
369 11 ▶ $settings{path} = '/feed.xml' unless defined $settings{path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $settings{"path"} 8 3 11
370 11 ▶ $settings{limit} = 20 unless defined $settings{limit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $settings{"limit"} 8 3 11
371 11 ▶ $settings{title} = $site->{name} unless defined $settings{title};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $settings{"title"} 9 2 11
372 11 ▶ $settings{path} = "/$settings{path}" unless $settings{path} =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $settings{"path"} =~ m[^/] 2 9 11
373  
374 11 return \%settings;
375 }
376  
377 =head2 feed_pages
378  
379 Takes the site and the feed settings, and returns the pages in the feed:
380 those in the feed's category (or with any category), newest first, up to the
381 limit.
382  
383 =cut
384  
385 8 ▶sub feed_pages {
Subroutine feed_pages: called CC=5 SCAR=16.1
Pod documented
386 8 my ($site, $settings) = @_;
387  
388 8 my @pages;
389 8 ▶ if (defined $settings->{category}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $settings->{'category'}) { } 5 3 8
390 5 ▶ @pages = @{ $site->{categories}->{$settings->{category}} || [] };
Condition100%2/2
$site->{'categories'}{$settings->{'category'}} || []
l!l
41
MC/DC100%1/1
$site->{'categories'}{$settings->{'category'}} || []
$site->{'categories'}{$settings->{'category'}}
Truth table100%2/2
A
A= $site->{'categories'}{$settings->{'category'}}
Aresult
11
00
391 }
392 else {
393 3 my %seen;
394 3 @pages = grep { !$seen{$_}++ } map { @{ $site->{categories}->{$_} } } keys %{$site->{categories}};
395 }
396  
397 8 @pages = sort { $b->{date} <=> $a->{date} } grep { is_html($_) } @pages;
398 8 ▶ splice(@pages, $settings->{limit}) if $settings->{limit} and @pages > $settings->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $settings->{"limit"} and @pages > $settings->{"limit"} 2 6 8
Condition100%3/3
$settings->{"limit"} and @pages > $settings->{"limit"}
!ll&&!rl&&r
152
MC/DC100%2/2
$settings->{"limit"} and @pages > $settings->{"limit"}
$settings->{"limit"} @pages > $settings->{"limit"}
Truth table100%3/3
A and B
A= $settings->{"limit"}B= @pages > $settings->{"limit"}
ABresult
0X0
100
111
399  
400 8 return \@pages;
401 }
402  
403 =head2 feed
404  
405 Takes the site, the feed settings and the pages in the feed, and returns an
406 Atom feed.
407  
408 =cut
409  
410 3 ▶sub feed {
Subroutine feed: called CC=11 SCAR=24.0
Pod documented
411 3 my ($site, $settings, $pages) = @_;
412  
413 3 my $esc = \&App::Dapper::Filters::xml_escape;
414 3 my $home = absolute_url($site, '/');
415 3 my $self_url = absolute_url($site, $settings->{path});
416 3 ▶ my $updated = @$pages ? _modified($pages->[0]) : $site->{time};
Branch: 1 branch
BranchTrueFalseTotal
@$pages ? : 2 1 3
417 3 ▶ $updated = $site->{time} unless $updated;
Branch: 1 branch
BranchTrueFalseTotal
unless $updated 0 3 3
418  
419 3 my $xml = qq{<?xml version="1.0" encoding="utf-8"?>\n<feed xmlns="http://www.w3.org/2005/Atom">\n};
420 3 $xml .= " <title>" . $esc->($settings->{title}) . "</title>\n";
421 3 ▶ $xml .= " <subtitle>" . $esc->($site->{description}) . "</subtitle>\n" if defined $site->{description};
Branch: 1 branch
BranchTrueFalseTotal
if defined $site->{"description"} 1 2 3
422 3 $xml .= qq{ <link href="} . $esc->($home) . qq{"/>\n};
423 3 $xml .= qq{ <link rel="self" href="} . $esc->($self_url) . qq{"/>\n};
424 3 $xml .= " <id>" . $esc->($home) . "</id>\n";
425 3 $xml .= " <updated>" . $updated->xmlschema . "</updated>\n";
426 3 ▶ $xml .= " <author><name>" . $esc->($site->{author}) . "</name></author>\n" if defined $site->{author};
Branch: 1 branch
BranchTrueFalseTotal
if defined $site->{"author"} 1 2 3
427  
428 3 for my $page (@$pages) {
429 5 my $url = absolute_url($site, $page->{url});
430 5 my $modified = _modified($page);
431 5 $xml .= " <entry>\n";
432 5 ▶ $xml .= " <title>" . $esc->(defined $page->{title} ? $page->{title} : $page->{url}) . "</title>\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'title'} ? : 4 1 5
433 5 $xml .= qq{ <link href="} . $esc->($url) . qq{"/>\n};
434 5 $xml .= " <id>" . $esc->($url) . "</id>\n";
435 5 $xml .= " <published>" . $page->{date}->xmlschema . "</published>\n";
436 5 ▶ $xml .= " <updated>" . $modified->xmlschema . "</updated>\n" if $modified;
Branch: 1 branch
BranchTrueFalseTotal
if $modified 5 0 5
437 $xml .= " <author><name>" . $esc->($page->{author}) . "</name></author>\n"
438 5 ▶ if defined $page->{author} and !ref $page->{author};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"author"} and not ref $page->{"author"} 1 4 5
Condition100%3/3
defined $page->{"author"} and not ref $page->{"author"}
!ll&&!rl&&r
311
MC/DC100%2/2
defined $page->{"author"} and not ref $page->{"author"}
defined $page->{"author"} not ref $page->{"author"}
Truth table100%3/3
A and not(B)
A= defined $page->{"author"}B= not ref $page->{"author"}
ABresult
0X0
100
111
439 5 my $summary = description($page);
440 5 ▶ $xml .= " <summary>" . $esc->($summary) . "</summary>\n" if length $summary;
Branch: 1 branch
BranchTrueFalseTotal
if length $summary 2 3 5
441 5 $xml .= qq{ <content type="html">} . $esc->($page->{content}) . "</content>\n";
442 5 $xml .= " </entry>\n";
443 }
444  
445 3 $xml .= "</feed>\n";
446 3 return $xml;
447 }
448  
449 =head2 redirects
450  
451 Takes the site and returns a hash of old URLs to new ones, from C<redirects>
452 in C<_config.yml> and the C<_data/redirects.yml> data file (which wins). Dies
453 if an entry isn't a pair of URLs.
454  
455 =cut
456  
457 19 ▶sub redirects {
Subroutine redirects: called CC=10 SCAR=23.0
Pod documented
458 19 my ($site) = @_;
459  
460 19 my %redirects;
461 # Copied first: looping over the hash elements directly would create them
462 19 ▶ my @sources = ($site->{redirects}, ref $site->{data} eq 'HASH' ? $site->{data}->{redirects} : undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $site->{'data'} eq 'HASH' ? : 6 13 19
463 19 for my $source (@sources) {
464 31 ▶ next unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 15 16 31
465 16 ▶ die "error: redirects must be pairs of an old URL and a new one, e.g. /old/: /new/\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $source eq "HASH" 3 13 16
466 unless ref $source eq 'HASH';
467 13 for my $from (keys %$source) {
468 13 my $to = $source->{$from};
469 13 ▶ die "error: the redirect from $from must go to a URL\n" unless defined $to and !ref $to and length $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to and not ref $to and length $to 3 10 13
Condition100%6/6
defined $to and not ref $to and length $to
!ll&&!rl&&r
2110
defined $to and not ref $to
!ll&&!rl&&r
1111
MC/DC100%3/3
defined $to and not ref $to and length $to
defined $to not ref $to length $to
Truth table100%4/4
A and not(B) and C
A= defined $toB= not ref $toC= length $to
ABCresult
0XX0
10X0
1100
1111
470 10 ▶ die "error: the redirect from $from must start with /\n" unless $from =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $from =~ m[^/] 2 8 10
471 8 $redirects{$from} = $to;
472 }
473 }
474  
475 11 return \%redirects;
476 }
477  
478 =head2 redirect_path
479  
480 Takes an old URL and returns the path of the file to write for it: C</old/>
481 becomes C<old/index.html>, C</old.html> stays C<old.html>, and C</old>
482 becomes C<old/index.html>.
483  
484 =cut
485  
486 9 ▶sub redirect_path {
Subroutine redirect_path: called CC=4 SCAR=13.9
Pod documented
487 9 my ($url) = @_;
488  
489 9 (my $path = $url) =~ s{^/+}{};
490 9 ▶ return "${path}index.html" if $path eq '' or $path =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" or $path =~ m[/$] 4 5 9
Condition100%3/3
$path eq "" or $path =~ m[/$]
l!l&&r!l&&!r
135
MC/DC100%2/2
$path eq "" or $path =~ m[/$]
$path eq "" $path =~ m[/$]
Truth table100%3/3
A or B
A= $path eq ""B= $path =~ m[/$]
ABresult
1X1
011
000
491 5 ▶ return $path if $path =~ m{\.[^./]+$};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[\.[^./]+$] 3 2 5
492 2 return "$path/index.html";
493 }
494  
495 =head2 redirect_page
496  
497 Takes the site and the URL to send people to, and returns a small HTML page
498 that sends browsers (and search engines) there.
499  
500 =cut
501  
502 3 ▶sub redirect_page {
Subroutine redirect_page: called CC=1 SCAR=0.0
Pod documented
503 3 my ($site, $to) = @_;
504  
505 3 my $esc = \&App::Dapper::Filters::xml_escape;
506 3 my $url = $esc->($to);
507 3 my $canonical = $esc->(absolute_url($site, $to));
508  
509 3 return <<"HTML";
510 <!DOCTYPE html>
511 <html>
512 <head>
513 <meta charset="utf-8">
514 <title>This page has moved</title>
515 <link rel="canonical" href="$canonical">
516 <meta name="robots" content="noindex">
517 <meta http-equiv="refresh" content="0; url=$url">
518 </head>
519 <body>
520 <p>This page has moved to <a href="$url">$url</a>.</p>
521 </body>
522 </html>
523 HTML
524 }
525  
526 =head2 seo
527  
528 Takes the site and a page, and returns the HTML for C<page.seo>: tags for the
529 page's C<< <head> >>.
530  
531 =cut
532  
533 36 ▶sub seo {
Subroutine seo: called CC=40 SCAR=36.9
Pod documented
534 36 my ($site, $page) = @_;
535  
536 36 my $esc = \&App::Dapper::Filters::xml_escape;
537 36 my @tags;
538  
539 36 ▶ my $title = defined $page->{title} && !ref $page->{title} ? $page->{title} : $site->{name};
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'title'} && !ref($page->{'title'}) ? : 8 28 36
Condition100%3/3
defined $page->{"title"} and not ref $page->{"title"}
!ll&&!rl&&r
2718
MC/DC100%2/2
defined $page->{"title"} and not ref $page->{"title"}
defined $page->{"title"} not ref $page->{"title"}
Truth table100%3/3
A and not(B)
A= defined $page->{"title"}B= not ref $page->{"title"}
ABresult
0X0
100
111
540 36 my $description = description($page);
541 36 ▶ my $has_url = defined $site->{url} && length $site->{url};
Condition66%2/3
defined $site->{'url'} && length $site->{'url'}
!ll&&!rl&&r
2808
MC/DC50%1/2
defined $site->{'url'} && length $site->{'url'}
defined $site->{'url'} length $site->{'url'}
Truth table66%2/3
A && B
A= defined $site->{'url'}B= length $site->{'url'}
ABresult
0X0
100
111
542 36 my $url = absolute_url($site, $page->{url});
543 36 my $is_article = defined _first_category($page);
544 my $image = defined $page->{image} && !ref $page->{image} ? $page->{image}
545 : defined $site->{image} && !ref $site->{image} ? $site->{image}
546 36 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $page->{'image'} && !ref($page->{'image'}) ? : 4 32 36
defined $site->{'image'} && !ref($site->{'image'}) ? : 3 29 32
Condition100%6/6
defined $page->{"image"} and not ref $page->{"image"}
!ll&&!rl&&r
3114
defined $site->{"image"} and not ref $site->{"image"}
!ll&&!rl&&r
2723
MC/DC100%2/2
defined $page->{"image"} and not ref $page->{"image"}
defined $page->{"image"} not ref $page->{"image"}
MC/DC100%2/2
defined $site->{"image"} and not ref $site->{"image"}
defined $site->{"image"} not ref $site->{"image"}
Truth table100%3/3
A and not(B)
A= defined $page->{"image"}B= not ref $page->{"image"}
ABresult
0X0
100
111
Truth table100%3/3
A and not(B)
A= defined $site->{"image"}B= not ref $site->{"image"}
ABresult
0X0
100
111
547 36 ▶ $image = absolute_url($site, $image) if defined $image;
Branch: 1 branch
BranchTrueFalseTotal
if defined $image 7 29 36
548  
549 36 ▶ push @tags, qq{<meta name="description" content="} . $esc->($description) . qq{">} if length $description;
Branch: 1 branch
BranchTrueFalseTotal
if length $description 6 30 36
550 36 ▶ push @tags, qq{<link rel="canonical" href="} . $esc->($url) . qq{">} if $has_url;
Branch: 1 branch
BranchTrueFalseTotal
if $has_url 8 28 36
551 push @tags, qq{<link rel="alternate" type="text/markdown" href="} . $esc->(absolute_url($site, $page->{markdown_url})) . qq{">}
552 36 ▶ if defined $page->{markdown_url};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"markdown_url"} 4 32 36
553  
554 36 ▶ if (my $feed = feed_settings($site)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $feed = feed_settings($site)) 3 33 36
555 push @tags, qq{<link rel="alternate" type="application/atom+xml" title="} . $esc->($feed->{title})
556 3 . qq{" href="} . $esc->(absolute_url($site, $feed->{path})) . qq{">};
557 }
558  
559 36 ▶ push @tags, qq{<meta property="og:type" content="} . ($is_article ? 'article' : 'website') . qq{">};
Branch: 1 branch
BranchTrueFalseTotal
$is_article ? : 6 30 36
560 36 ▶ push @tags, qq{<meta property="og:title" content="} . $esc->($title) . qq{">} if defined $title;
Branch: 1 branch
BranchTrueFalseTotal
if defined $title 13 23 36
561 36 ▶ push @tags, qq{<meta property="og:description" content="} . $esc->($description) . qq{">} if length $description;
Branch: 1 branch
BranchTrueFalseTotal
if length $description 6 30 36
562 36 ▶ push @tags, qq{<meta property="og:url" content="} . $esc->($url) . qq{">} if $has_url;
Branch: 1 branch
BranchTrueFalseTotal
if $has_url 8 28 36
563 36 ▶ push @tags, qq{<meta property="og:site_name" content="} . $esc->($site->{name}) . qq{">} if defined $site->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $site->{"name"} 13 23 36
564 36 ▶ push @tags, qq{<meta property="og:image" content="} . $esc->($image) . qq{">} if defined $image;
Branch: 1 branch
BranchTrueFalseTotal
if defined $image 7 29 36
565 push @tags, qq{<meta property="article:published_time" content="} . $page->{date}->xmlschema . qq{">}
566 36 ▶ if $is_article and ref $page->{date};
Branch: 1 branch
BranchTrueFalseTotal
if $is_article and ref $page->{"date"} 3 33 36
Condition100%3/3
$is_article and ref $page->{"date"}
!ll&&!rl&&r
3033
MC/DC100%2/2
$is_article and ref $page->{"date"}
$is_article ref $page->{"date"}
Truth table100%3/3
A and B
A= $is_articleB= ref $page->{"date"}
ABresult
0X0
100
111
567 36 ▶ push @tags, qq{<meta name="twitter:card" content="} . (defined $image ? 'summary_large_image' : 'summary') . qq{">};
Branch: 1 branch
BranchTrueFalseTotal
defined $image ? : 7 29 36
568  
569 36 ▶ my %data = (
Branch: 1 branch
BranchTrueFalseTotal
$is_article ? : 6 30 36
570 '@context' => 'https://schema.org',
571 '@type' => $is_article ? 'BlogPosting' : 'WebPage',
572 );
573 36 ▶ $data{headline} = $title if defined $title;
Branch: 1 branch
BranchTrueFalseTotal
if defined $title 13 23 36
574 36 ▶ $data{name} = $title if defined $title and !$is_article;
Branch: 1 branch
BranchTrueFalseTotal
if defined $title and not $is_article 11 25 36
Condition100%3/3
defined $title and not $is_article
!ll&&!rl&&r
23211
MC/DC100%2/2
defined $title and not $is_article
defined $title not $is_article
Truth table100%3/3
A and not(B)
A= defined $titleB= not $is_article
ABresult
0X0
100
111
575 36 ▶ $data{description} = $description if length $description;
Branch: 1 branch
BranchTrueFalseTotal
if length $description 6 30 36
576 36 ▶ $data{url} = $url if $has_url;
Branch: 1 branch
BranchTrueFalseTotal
if $has_url 8 28 36
577 36 ▶ $data{image} = $image if defined $image;
Branch: 1 branch
BranchTrueFalseTotal
if defined $image 7 29 36
578 36 ▶ if ($is_article and ref $page->{date}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_article and ref $page->{"date"}) 3 33 36
Condition100%3/3
$is_article and ref $page->{"date"}
!ll&&!rl&&r
3033
MC/DC100%2/2
$is_article and ref $page->{"date"}
$is_article ref $page->{"date"}
Truth table100%3/3
A and B
A= $is_articleB= ref $page->{"date"}
ABresult
0X0
100
111
579 3 $data{datePublished} = $page->{date}->xmlschema;
580 3 my $modified = _modified($page);
581 3 ▶ $data{dateModified} = $modified->xmlschema if $modified;
Branch: 1 branch
BranchTrueFalseTotal
if $modified 3 0 3
582 }
583 36 ▶ my $author = defined $page->{author} && !ref $page->{author} ? $page->{author} : $site->{author};
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'author'} && !ref($page->{'author'}) ? : 1 35 36
Condition100%3/3
defined $page->{"author"} and not ref $page->{"author"}
!ll&&!rl&&r
3411
MC/DC100%2/2
defined $page->{"author"} and not ref $page->{"author"}
defined $page->{"author"} not ref $page->{"author"}
Truth table100%3/3
A and not(B)
A= defined $page->{"author"}B= not ref $page->{"author"}
ABresult
0X0
100
111
584 36 ▶ $data{author} = { '@type' => 'Person', name => $author } if defined $author and !ref $author;
Branch: 1 branch
BranchTrueFalseTotal
if defined $author and not ref $author 4 32 36
Condition100%3/3
defined $author and not ref $author
!ll&&!rl&&r
3114
MC/DC100%2/2
defined $author and not ref $author
defined $author not ref $author
Truth table100%3/3
A and not(B)
A= defined $authorB= not ref $author
ABresult
0X0
100
111
585 36 ▶ $data{publisher} = { '@type' => 'Organization', name => $site->{name} } if $is_article and defined $site->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $is_article and defined $site->{"name"} 2 34 36
Condition100%3/3
$is_article and defined $site->{"name"}
!ll&&!rl&&r
3042
MC/DC100%2/2
$is_article and defined $site->{"name"}
$is_article defined $site->{"name"}
Truth table100%3/3
A and B
A= $is_articleB= defined $site->{"name"}
ABresult
0X0
100
111
586 36 ▶ $data{mainEntityOfPage} = $url if $is_article and $has_url;
Branch: 1 branch
BranchTrueFalseTotal
if $is_article and $has_url 1 35 36
Condition100%3/3
$is_article and $has_url
!ll&&!rl&&r
3051
MC/DC100%2/2
$is_article and $has_url
$is_article $has_url
Truth table100%3/3
A and B
A= $is_articleB= $has_url
ABresult
0X0
100
111
587  
588 36 my $json = JSON->new->canonical->encode(\%data);
589 36 $json =~ s{</}{<\\/}g;
590 36 push @tags, qq{<script type="application/ld+json">$json</script>};
591  
592 36 return join("\n", @tags);
593 }
594  
595 191 1;
« lib/App/Dapper/Components.pm ↑ Summary lib/App/Dapper/Collections.pm »