lib/App/Dapper/Collections.pm

filter: Statementstmt 98.1% 107 / 109 Branchbran 94.2% 49 / 52 Conditioncond 92.8% 13 / 14 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 96.5% 194 / 201 CC 37 SCAR 22.8
CC
37
Cov
97%
paginate
18.0
list_pages
8.0
generate
6.4
CRAP 9.8
« lib/App/Dapper/Publish.pm ↑ Summary lib/App/Dapper/Init.pm »
1 package App::Dapper::Collections;
2  
3 =head1 NAME
4  
5 App::Dapper::Collections - Category pages, tag pages and pagination.
6  
7 =head1 DESCRIPTION
8  
9 After the source files are read, Dapper can generate a page for each
10 category and each tag, and split long lists of pages across several pages.
11  
12 Category and tag pages are turned on in C<_config.yml>, and are rendered with
13 the layout you name:
14  
15 category_pages:
16 layout: category # _layout/category.html
17 urlpattern: /:category/ # the default
18 per_page: 10 # optional: split into pages of 10
19 tag_pages:
20 layout: tag
21 urlpattern: /tags/:tag/ # the default
22  
23 Each generated page has C<page.title> (the category or tag), C<page.category>
24 or C<page.tag>, and C<page.posts> (its pages, newest first). With C<per_page>,
25 C<page.posts> holds just the pages for that page, and C<paginator> describes
26 the whole set. The URL of each one is in C<site.category_pages> and
27 C<site.tag_pages>, so layouts can link to them:
28  
29 <a href="[% site.tag_pages.$tag %]">[% tag %]</a>
30  
31 Any page can list pages across several pages with C<paginate> in its front
32 matter:
33  
34 paginate:
35 category: blog # or tag: perl; leave both out for every post
36 per_page: 10
37  
38 In its layout (or its content, with C<template: true>), C<paginator.items>
39 holds that page's share of the posts, newest first.
40  
41 =head2 The paginator
42  
43 paginator.items the pages to show on this page
44 paginator.page this page's number, starting at 1
45 paginator.per_page pages per page
46 paginator.total_pages how many pages there are
47 paginator.total_items how many items there are
48 paginator.previous_url the URL of the previous page (if there is one)
49 paginator.next_url the URL of the next page (if there is one)
50 paginator.first_url, paginator.last_url
51 paginator.pages a list of { number, url } for every page
52  
53 Page 1 keeps the page's own URL; page 2 is at C<page/2/> below it.
54  
55 =cut
56  
57 190 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 190 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 190 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 190 ▶use File::Spec::Functions qw/ canonpath catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 190 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 =head2 generate
67  
68 Takes the Dapper object, adds category and tag pages to the site if they are
69 turned on, and expands every paginated page into its set of pages.
70  
71 =cut
72  
73 6 ▶sub generate {
Subroutine generate: called CC=6 SCAR=18.5
Pod documented
74 6 my ($dapper) = @_;
75  
76 6 my $site = $dapper->{site};
77  
78 # The settings from _config.yml; the same names then hold the URL of each
79 # generated page
80 6 my %settings = (category => $site->{category_pages}, tag => $site->{tag_pages});
81 6 $site->{category_pages} = {};
82 6 $site->{tag_pages} = {};
83  
84 6 my @generated;
85 6 for my $kind (qw/ category tag /) {
86 11 my $value = $settings{$kind};
87 11 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 9 2 11
88 2 ▶ die "error: ${kind}_pages in $dapper->{config} must be settings such as layout: $kind\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 2 0 2
89 unless ref $value eq 'HASH';
90 0 ▶ my $lists = $kind eq 'tag' ? $site->{tags} : $site->{categories};
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'tag' ? : 0 0 0
91 0 push @generated, list_pages($dapper, $kind, $lists, $site->{"${kind}_pages"}, $value);
92 }
93  
94 4 my @pages;
95 4 for my $page (@{$site->{pages}}, @generated) {
96 4 push @pages, paginate($dapper, $page);
97 }
98 4 $site->{pages} = \@pages;
99 }
100  
101 =head2 list_pages
102  
103 Takes the Dapper object, the kind of list (C<category> or C<tag>), the hash of
104 names to pages, the hash to record each list page's URL in, and the settings
105 from C<_config.yml>. Returns the generated pages.
106  
107 =cut
108  
109 6 ▶sub list_pages {
Subroutine list_pages: called CC=8 SCAR=20.8
Pod documented
110 6 my ($dapper, $kind, $lists, $urls, $settings) = @_;
111  
112 6 my $site = $dapper->{site};
113 6 my $label = "${kind}_pages";
114  
115 die "error: $label in $dapper->{config} needs a layout, e.g. layout: $kind\n"
116 6 ▶ unless defined $settings->{layout} and length $settings->{layout};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $settings->{"layout"} and length $settings->{"layout"} 2 4 6
Condition100%3/3
defined $settings->{"layout"} and length $settings->{"layout"}
!ll&&!rl&&r
114
MC/DC100%2/2
defined $settings->{"layout"} and length $settings->{"layout"}
defined $settings->{"layout"} length $settings->{"layout"}
Truth table100%3/3
A and B
A= defined $settings->{"layout"}B= length $settings->{"layout"}
ABresult
0X0
100
111
117  
118 my $pattern = defined $settings->{urlpattern} ? $settings->{urlpattern}
119 4 ▶ : $kind eq 'tag' ? '/tags/:tag/'
Branch: 2 branches
BranchTrueFalseTotal
defined $settings->{'urlpattern'} ? : 1 3 4
$kind eq 'tag' ? : 2 1 3
120 : '/:category/';
121  
122 4 my @pages;
123 4 for my $name (sort keys %$lists) {
124 4 my @posts = reverse @{$lists->{$name}};
125 4 ▶ my $value = $kind eq 'tag' ? App::Dapper::Utils::slugify($name) : $name;
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'tag' ? : 2 2 4
126  
127 my %page = (
128 title => $name,
129 $kind => $name,
130 posts => \@posts,
131 layout => $settings->{layout},
132 extension => $site->{extension},
133 date => $posts[0]->{date},
134 content => '',
135 source => "$label ($name)",
136 generated => 1,
137 sitemap => $settings->{sitemap},
138 4 );
139 4 $page{url} = App::Dapper::expand_urlpattern($pattern, { $kind => $value });
140 4 set_filename($dapper, \%page);
141  
142 4 ▶ if ($settings->{per_page}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings->{"per_page"}) 1 3 4
143 1 $page{paginate} = { items => \@posts, per_page => $settings->{per_page} };
144 }
145  
146 4 $urls->{$name} = $page{url};
147 4 push @pages, \%page;
148 }
149  
150 4 return @pages;
151 }
152  
153 =head2 paginate
154  
155 Takes the Dapper object and a page. If the page has C<paginate> settings,
156 returns the page followed by its further pages, each with a C<paginator>;
157 otherwise returns the page unchanged.
158  
159 =cut
160  
161 23 ▶sub paginate {
Subroutine paginate: called CC=18 SCAR=28.9
Pod documented
162 23 my ($dapper, $page) = @_;
163  
164 23 my $settings = $page->{paginate};
165 23 ▶ return $page unless $settings;
Branch: 1 branch
BranchTrueFalseTotal
unless $settings 7 16 23
166  
167 16 ▶ $settings = { per_page => $settings } unless ref $settings;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings 2 14 16
168 16 ▶ die "error: paginate in $page->{source} must be a number or settings such as per_page: 10\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" 1 15 16
169 unless ref $settings eq 'HASH';
170  
171 15 ▶ my $per_page = $settings->{per_page} || 10;
Condition100%2/2
$settings->{'per_page'} || 10
l!l
132
MC/DC100%1/1
$settings->{'per_page'} || 10
$settings->{'per_page'}
Truth table100%2/2
A
A= $settings->{'per_page'}
Aresult
11
00
172 15 ▶ die "error: per_page in $page->{source} must be a whole number above 0\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $per_page =~ /^\d+$/ and $per_page > 0 6 9 15
Condition66%2/3
$per_page =~ /^\d+$/ and $per_page > 0
!ll&&!rl&&r
609
MC/DC50%1/2
$per_page =~ /^\d+$/ and $per_page > 0
$per_page =~ /^\d+$/ $per_page > 0
Truth table66%2/3
A and B
A= $per_page =~ /^\d+$/B= $per_page > 0
ABresult
0X0
100
111
173 unless $per_page =~ /^\d+$/ and $per_page > 0;
174  
175 9 ▶ my @items = $settings->{items} ? @{$settings->{items}} : items_for($dapper->{site}, $settings);
Branch: 1 branch
BranchTrueFalseTotal
$settings->{'items'} ? : 1 8 9
176  
177 9 ▶ my $total_pages = int((@items + $per_page - 1) / $per_page) || 1;
Condition100%2/2
int((@items + $per_page - 1) / $per_page) || 1
l!l
81
MC/DC100%1/1
int((@items + $per_page - 1) / $per_page) || 1
int((@items + $per_page - 1) / $per_page)
Truth table100%2/2
A
A= int((@items + $per_page - 1) / $per_page)
Aresult
11
00
178 9 my $base = base_url($page->{url});
179  
180 9 ▶ my @urls = map { $_ == 1 ? $page->{url} : "${base}page/$_/" } 1 .. $total_pages;
Branch: 1 branch
BranchTrueFalseTotal
$_ == 1 ? : 9 12 21
181  
182 9 my @pages;
183 9 for my $number (1 .. $total_pages) {
184 21 ▶ my $copy = $number == 1 ? $page : { %$page };
Branch: 1 branch
BranchTrueFalseTotal
$number == 1 ? : 9 12 21
185  
186 21 ▶ if ($number > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 1) 12 9 21
187 12 $copy->{url} = $urls[$number - 1];
188 12 delete $copy->{markdown};
189 12 delete $copy->{markdown_url};
190 12 set_filename($dapper, $copy, 1);
191 }
192  
193 21 my $first = ($number - 1) * $per_page;
194 21 my $last = $first + $per_page - 1;
195 21 ▶ $last = $#items if $last > $#items;
Branch: 1 branch
BranchTrueFalseTotal
if $last > $#items 7 14 21
196  
197 $copy->{paginator} = {
198 items => [ @items > 0 ? @items[$first .. $last] : () ],
199 page => $number,
200 per_page => $per_page,
201 total_pages => $total_pages,
202 total_items => scalar @items,
203 previous_url => $number > 1 ? $urls[$number - 2] : undef,
204 next_url => $number < $total_pages ? $urls[$number] : undef,
205 first_url => $urls[0],
206 last_url => $urls[-1],
207 21 ▶ pages => [ map { { number => $_, url => $urls[$_ - 1] } } 1 .. $total_pages ],
Branch: 3 branches
BranchTrueFalseTotal
@items > 0 ? : 20 1 21
$number > 1 ? : 12 9 21
$number < $total_pages ? : 12 9 21
208 };
209 21 ▶ $copy->{posts} = $copy->{paginator}->{items} if $page->{generated};
Branch: 1 branch
BranchTrueFalseTotal
if $page->{"generated"} 2 19 21
210  
211 21 push @pages, $copy;
212 }
213  
214 9 return @pages;
215 }
216  
217 =head2 items_for
218  
219 Takes the site and pagination settings, and returns the pages to paginate,
220 newest first: those in the C<category> or with the C<tag>, or every page with
221 a category if neither is given.
222  
223 =cut
224  
225 14 ▶sub items_for {
Subroutine items_for: called CC=5 SCAR=16.1
Pod documented
226 14 my ($site, $settings) = @_;
227  
228 14 my @items;
229 14 ▶ if (defined $settings->{category}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $settings->{'category'}) { } 5 9 14
elsif (defined $settings->{'tag'}) { } 3 6 9
230 5 ▶ @items = @{ $site->{categories}->{$settings->{category}} || [] };
Condition100%2/2
$site->{'categories'}{$settings->{'category'}} || []
l!l
32
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
231 }
232 elsif (defined $settings->{tag}) {
233 3 ▶ @items = @{ $site->{tags}->{$settings->{tag}} || [] };
Condition100%2/2
$site->{'tags'}{$settings->{'tag'}} || []
l!l
21
MC/DC100%1/1
$site->{'tags'}{$settings->{'tag'}} || []
$site->{'tags'}{$settings->{'tag'}}
Truth table100%2/2
A
A= $site->{'tags'}{$settings->{'tag'}}
Aresult
11
00
234 }
235 else {
236 6 my %seen;
237 6 @items = grep { !$seen{$_}++ } map { @{ $site->{categories}->{$_} } } sort keys %{$site->{categories}};
238 }
239  
240 14 return sort { $b->{date} <=> $a->{date} } @items;
241 }
242  
243 =head2 base_url
244  
245 Takes a URL and returns the directory the further pages go in:
246 C</blog/> stays C</blog/>, and C</archive.html> becomes C</archive/>.
247  
248 =cut
249  
250 16 ▶sub base_url {
Subroutine base_url: called CC=3 SCAR=11.0
Pod documented
251 16 my ($url) = @_;
252  
253 16 ▶ return $url if $url =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[/$] 10 6 16
254 6 $url =~ s{/index\.[^./]+$}{/};
255 6 ▶ return $url if $url =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[/$] 2 4 6
256 4 $url =~ s{\.[^./]+$}{};
257 4 return "$url/";
258 }
259  
260 =head2 set_filename
261  
262 Takes the Dapper object and a page with a C<url>, and sets the page's
263 C<filename> and C<dirname> from it. A URL ending in C</> is written as an
264 index file.
265  
266 =cut
267  
268 18 ▶sub set_filename {
Subroutine set_filename: called CC=2 SCAR=6.9
Pod documented
269 18 my ($dapper, $page, $is_index) = @_;
270  
271 18 my $path = $page->{url};
272 18 ▶ $path .= "index$page->{extension}" if $path =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[/$] 16 2 18
273 18 $page->{filename} = canonpath(catfile($dapper->{output}, $path));
274 18 (my $dir = $page->{filename}) =~ s{[^/]*$}{};
275 18 $page->{dirname} = $dir;
276 }
277  
278 190 1;
« lib/App/Dapper/Publish.pm ↑ Summary lib/App/Dapper/Init.pm »