lib/App/Dapper/Publish.pm

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