Template/Plugin/Qgoda.pm

filter: Statementstmt 31.3% 121 / 386 Branchbran 12.7% 14 / 110 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 38.7% 24 / 62 Podpod 4.0% 1 / 25 totaltotal 26.5% 160 / 602 CC 64 SCAR 39.8
CC
64
Cov
27%
paginate
306.5
__sanitizeHashref
42.0
bust_cache
30.0
CRAP 53.4
« Qgoda/HTMLFilter/TOC.pm ↑ Summary Qgoda/Util/Translate.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Template::Plugin::Qgoda;
20 2 $Template::Plugin::Qgoda::VERSION = 'v0.9.2';
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use base qw(Template::Plugin);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use Locale::TextDomain qw('qgoda');
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Date::Parse qw(str2time);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use POSIX qw(strftime setlocale LC_ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use List::Util qw(pairmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Locale::Util qw(web_set_locale);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 ▶use Qgoda;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 use Qgoda::Util qw(collect_defaults merge_data empty read_file html_escape
39 2 ▶ escape_link);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use Qgoda::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 sub new {
43 7 ▶ my ($class, $context) = @_;
Subroutine new: called CC=2 SCAR=7.7
Pod documented
44  
45 7 ▶ return $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 7 7
46  
47 my $get_values = sub {
48 5 ▶ my ($assets, @fields) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
49  
50 5 my $stash = $context->stash->clone;
51  
52 # Find a random variable name.
53 5 my $name = 'a';
54 5 while (1) {
55 5 ▶ last if empty $stash->get($name);
Branch: 1 branch
BranchTrueFalseTotal
if &empty(scalar $stash->get($name)) 5 0 5
56 0 ++$name;
57 }
58  
59 5 my @values;
60 5 my $i = 0;
61 5 foreach my $asset (@$assets) {
62 60 my @subvalues;
63 60 push @values, [$i++, \@subvalues];
64  
65 # The variable name 'asset' is therefore not available.
66 60 $stash->set($name => $asset);
67 60 foreach my $field (@fields) {
68 60 push @subvalues, $stash->get("$name.$field");
69 }
70 }
71  
72 5 $stash->declone;
73  
74 5 return @values;
75 7 };
76  
77 my $compare_array = sub {
78 104 ▶ my $arr1 = $a->[1];
Subroutine __ANON__: called CC=3 SCAR=11.0
79 104 my $arr2 = $b->[1];
80  
81 104 ▶ for (my $i = 0; $i < @$arr1; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr1) 104 24 128
82 104 my ($val1, $val2) = ($arr1->[$i], $arr2->[$i]);
83  
84 104 ▶ return $val1 cmp $val2 if $val1 cmp $val2;
Branch: 1 branch
BranchTrueFalseTotal
if $val1 cmp $val2 80 24 104
85 }
86  
87 24 return 0;
88 7 };
89  
90 my $ncompare_array = sub {
91 24 ▶ my $arr1 = $a->[1];
Subroutine __ANON__: called CC=3 SCAR=11.8
92 24 my $arr2 = $b->[1];
93  
94 24 ▶ for (my $i = 0; $i < @$arr1; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr1) 24 0 24
95 24 my ($val1, $val2) = ($arr1->[$i], $arr2->[$i]);
96  
97 24 ▶ return $val1 <=> $val2 if $val1 <=> $val2;
Branch: 1 branch
BranchTrueFalseTotal
if $val1 <=> $val2 24 0 24
98 }
99  
100 0 return 0;
101 7 };
102  
103 my $sort_by = sub {
104 4 ▶ my ($assets, $field) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
105  
106 4 my @sorted = map { $assets->[$_->[0]] }
107 sort $compare_array $get_values->($assets, $field);
108 4 return \@sorted;
109 7 };
110  
111 my $nsort_by = sub {
112 1 ▶ my ($assets, $field) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
113  
114 1 my @sorted = map { $assets->[$_->[0]] }
115 sort $ncompare_array $get_values->($assets, $field);
116 1 return \@sorted;
117 7 };
118  
119 my $vmap = sub {
120 0 ▶ my ($objs, $field) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
121  
122 0 my @keys;
123 0 ▶ if ('ARRAY' eq reftype $objs) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq builtin::reftype($objs)) { } 0 0 0
elsif ('HASH' eq builtin::reftype($objs)) { } 0 0 0
124 0 @keys = (0 .. $#$objs);
125 } elsif ('HASH' eq reftype $objs) {
126 # Make it deterministic.
127 0 @keys = sort keys %$objs;
128 } else {
129 0 return [];
130 }
131  
132 0 my $stash = Template::Stash->new({objs => $objs});
133 0 return [map { $stash->get("objs.$_.$field") } @keys];
134 7 };
135  
136 my $kebap_snake = sub {
137 return {
138 pairmap {
139 0 ▶ $a =~ s/-/_/g;
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 $a, $b;
141 0 ▶ } %{$_[0]}
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 };
143 7 };
144  
145 my $kebap_camel = sub {
146 return {
147 pairmap {
148 0 ▶ $a =~ s/-(.)/uc $1/ge;
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 0 $a =~ s/-$/_/;
150 0 $a, $b;
151 0 ▶ } %{$_[0]}
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 };
153 7 };
154  
155 7 my %escapes = ('"' => 'quot', '&' => 'amp');
156 my $quote_values = sub {
157 return {
158 pairmap {
159 0 ▶ $b =~ s/(["&])/&$escapes{$1};/og;
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 $a, qq{"$b"}
161 0 ▶ } %{$_[0]}
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 };
163 7 };
164  
165 7 $context->define_vmethod(list => sortBy => $sort_by);
166 7 $context->define_vmethod(list => nsortBy => $nsort_by);
167 7 $context->define_vmethod(list => vmap => $vmap);
168 7 $context->define_vmethod(scalar => slugify => \&Qgoda::Util::slugify);
169 7 $context->define_vmethod(scalar => unmarkup => \&Qgoda::Util::unmarkup);
170 7 $context->define_vmethod(hash => kebapSnake => $kebap_snake);
171 7 $context->define_vmethod(hash => kebapCamel => $kebap_camel);
172 7 $context->define_vmethod(hash => quoteValues => $quote_values);
173  
174 7 my $self = {
175 __context => $context
176 };
177 7 bless $self, $class;
178 }
179  
180 sub __getContext {
181 0 ▶ shift->{__context};
Subroutine __getContext: not called CC=1 SCAR=6.9
182 }
183  
184 sub __getStash {
185 0 ▶ shift->{__context}->stash;
Subroutine __getStash: not called CC=1 SCAR=6.9
186 }
187  
188 sub __getAsset {
189 0 ▶ shift->{__context}->stash->{asset};
Subroutine __getAsset: not called CC=1 SCAR=6.9
190 }
191  
192 sub __getConfig {
193 0 ▶ shift->{__context}->stash->{config};
Subroutine __getConfig: not called CC=1 SCAR=6.9
194 }
195  
196 sub bust_cache {
197 0 ▶ my ($self, $uri) = @_;
Subroutine bust_cache: not called CC=5 SCAR=34.0
Pod undocumented
198  
199 0 ▶ return $uri if $uri !~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^/] 0 0 0
200  
201 0 my($scheme, $authority, $path, $query, $fragment) =
202 $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|o;
203 0 ▶ return if !defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
204  
205 0 require Qgoda;
206 0 my $srcdir = Qgoda->new->config->{srcdir};
207 0 my $fullpath = File::Spec->canonpath(File::Spec->catfile($srcdir, $path));
208  
209 0 ▶ my @stat = stat $fullpath or return $uri;
Branch: 1 branch
BranchTrueFalseTotal
my(@stat) = stat $fullpath or return $uri 0 0 0
210 0 ▶ if (defined $query) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query) { } 0 0 0
211 0 return "$uri&$stat[9]"
212 } else {
213 0 return "$uri?$stat[9]"
214 }
215 }
216  
217 sub include {
218 0 ▶ my ($self, $path, $overlay, $extra) = @_;
Subroutine include: not called CC=3 SCAR=24.8
Pod undocumented
219  
220 0 ▶ die "usage: include(PATH, OVERLAY, KEY = VALUE, ...\n"
Branch: 1 branch
BranchTrueFalseTotal
if &empty($path) or &empty($overlay) 0 0 0
Condition0%0/3
&empty($path) or &empty($overlay)
l!l&&r!l&&!r
000
MC/DC0%0/2
&empty($path) or &empty($overlay)
&empty($path) &empty($overlay)
Truth table0%0/3
A or B
A= &empty($path)B= &empty($overlay)
ABresult
1X1
011
000
221 if empty $path || empty $overlay;
222 0 $overlay = $self->__sanitizeHashref($overlay, 'include');
223 0 $extra = $self->__sanitizeHashref($extra, 'include', 1);
224  
225 0 my $asset = $self->__include($path, $overlay, $extra);
226  
227 0 return $asset->{content};
228 }
229  
230 sub __include {
231 0 ▶ my ($self, $_path, $overlay, $extra) = @_;
Subroutine __include: not called CC=4 SCAR=30.0
232  
233 0 require Qgoda;
234 0 my $q = Qgoda->new;
235 0 my $srcdir = $q->config->{srcdir};
236  
237 0 my $path = Cwd::abs_path($_path);
238 0 ▶ if (!defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 0 0
239 0 die __x("error including '{path}': {error}.\n",
240 path => $_path, error => $!);
241 }
242  
243 0 my $relpath = File::Spec->abs2rel($path, $srcdir);
244 0 my $asset = Qgoda::Asset->new($path, $relpath);
245  
246 0 ▶ if ($overlay) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overlay) 0 0 0
247 0 my %overlay = %$overlay;
248 0 delete $overlay{path};
249 0 delete $overlay{relpath};
250 0 delete $overlay{reldir};
251 0 delete $overlay{view};
252 0 delete $overlay{chain};
253 0 delete $overlay{wrapper};
254 0 merge_data $asset, \%overlay;
255 }
256  
257 0 $q->analyzeAssets([$asset], $extra);
258 0 $q->locateAsset($asset);
259  
260 0 my $builders = $q->getBuilders;
261 0 my $site = $q->getSite;
262 0 foreach my $builder (@{$builders}) {
263 0 $builder->processAsset($asset, $site);
264 0 $builder->wrapAsset($asset, $site);
265 }
266  
267 0 return $asset;
268 }
269  
270 sub __sanitizeFilters {
271 0 ▶ my ($self, $filters) = @_;
Subroutine __sanitizeFilters: not called CC=5 SCAR=34.0
272  
273 0 ▶ return {} if empty $filters;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($filters) 0 0 0
274 0 ▶ if (!ref $filters) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $filters) 0 0 0
275 0 die __x("invalid filters '{filters}' (use named arguments!)\n",
276 filters => $filters);
277 }
278  
279 0 my $reftype = reftype $filters;
280 0 ▶ if ('ARRAY' eq $reftype) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq $reftype) { } 0 0 0
elsif ('HASH' ne $reftype) { } 0 0 0
281 0 my $json = encode_json($filters);
282 0 $json =~ s{.(.*).}{$1};
283 0 die __x("invalid filters '{filters}' (use named arguments!)\n",
284 filters => $json);
285 } elsif ('HASH' ne $reftype) {
286 0 die __x("invalid filters '{filters}' (use named arguments!)\n",
287 filters => $filters);
288 }
289  
290 # The caller may want to add default filters. Make sure that the original
291 # reference is kept untouched.
292 0 return {%$filters};
293 }
294  
295 sub __sanitizeHashref {
296 0 ▶ my ($self, $hashref, $method, $optional) = @_;
Subroutine __sanitizeHashref: not called CC=6 SCAR=37.4
297  
298 0 ▶ if (empty $hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (&empty($hashref)) 0 0 0
299 0 ▶ if ($optional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional) { } 0 0 0
300 0 return {};
301 } else {
302 0 die __x("named arguments for '{method}()' are mandatory\n",
303 method => $method);
304 }
305 }
306  
307 0 ▶ if (!ref $hashref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hashref) 0 0 0
308 0 die __x("method '{method}' requires named arguments, not '{args}'",
309 method => $method, args => $hashref);
310 }
311  
312 0 my $reftype = reftype $hashref;
313 0 ▶ if ('ARRAY' eq $reftype) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq $reftype) { } 0 0 0
elsif ('HASH' ne $reftype) { } 0 0 0
314 0 my $json = encode_json($hashref);
315 0 $json =~ s{.(.*).}{$1};
316 0 die __x("invalid arguments '{args}' for method '{method}()'"
317 . " (use named arguments!)\n",
318 args => $json, method => $method);
319 } elsif ('HASH' ne $reftype) {
320 0 die __x("invalid arguments '{args}' for method '{method}()'"
321 . " (use named arguments!)\n",
322 args => $hashref, method => $method);
323 }
324  
325 0 return $hashref;
326 }
327  
328 sub list {
329 0 ▶ my ($self, $filters) = @_;
Subroutine list: not called CC=1 SCAR=6.9
Pod undocumented
330  
331 0 $filters = $self->__sanitizeFilters($filters);
332  
333 0 my $site = Qgoda->new->getSite;
334 0 return $site->searchAssets(%$filters);
335 }
336  
337 sub llist {
338 0 ▶ my ($self, $filters) = @_;
Subroutine llist: not called CC=1 SCAR=6.9
Pod undocumented
339  
340 0 $filters = $self->__sanitizeFilters($filters);
341 0 $filters->{lingua} = $self->__getAsset->{lingua};
342  
343 0 return $self->list($filters);
344 }
345  
346 sub listPosts {
347 0 ▶ my ($self, $filters) = @_;
Subroutine listPosts: not called CC=1 SCAR=6.9
Pod undocumented
348  
349 0 $filters = $self->__sanitizeFilters($filters);
350 0 $filters->{type} = 'post';
351  
352 0 return $self->list($filters);
353 }
354  
355 sub llistPosts {
356 0 ▶ my ($self, $filters) = @_;
Subroutine llistPosts: not called CC=1 SCAR=6.9
Pod undocumented
357  
358 0 $filters = $self->__sanitizeFilters($filters);
359 0 $filters->{lingua} = $self->__getAsset->{lingua};
360  
361 0 return $self->listPosts($filters);
362 }
363  
364 sub link {
365 0 ▶ my ($self, $filters) = @_;
Subroutine link: not called CC=3 SCAR=24.8
Pod undocumented
366  
367 0 $filters = $self->__sanitizeFilters($filters);
368  
369 0 my $set = $self->list($filters);
370 0 ▶ if (@$set == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$set == 0) 0 0 0
371 0 my $json = encode_json($filters);
372 0 $json =~ s{.(.*).}{$1};
373 0 warn "broken link($json)\n";
374 0 return '';
375 0 ▶ } if (@$set > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$set > 1) 0 0 0
376 0 my $json = encode_json($filters);
377 0 $json =~ s{.(.*).}{$1};
378 0 warn "ambiguous link($json)\n";
379 }
380  
381 0 return $set->[0]->{permalink};
382 }
383  
384 sub xref {
385 0 ▶ my ($self, $variable, $filters) = @_;
Subroutine xref: not called CC=3 SCAR=24.8
Pod undocumented
386  
387 0 $filters = $self->__sanitizeFilters($filters);
388  
389 0 my $set = $self->list($filters);
390 0 ▶ if (@$set == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$set == 0) 0 0 0
391 0 my $json = encode_json($filters);
392 0 $json =~ s{.(.*).}{$1};
393 0 warn "broken xref($json)\n";
394 0 ▶ } if (@$set > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$set > 1) 0 0 0
395 0 my $json = encode_json($filters);
396 0 $json =~ s{.(.*).}{$1};
397 0 warn "ambiguous xref($json)\n";
398 }
399  
400 0 return $set->[0]->{$variable};
401 }
402  
403 sub llink {
404 0 ▶ my ($self, $filters) = @_;
Subroutine llink: not called CC=1 SCAR=6.9
Pod undocumented
405  
406 0 $filters = $self->__sanitizeFilters($filters);
407 0 $filters->{lingua} = $self->__getAsset->{lingua};
408  
409 0 return $self->link($filters);
410 }
411  
412 sub lxref {
413 0 ▶ my ($self, $variable, $filters) = @_;
Subroutine lxref: not called CC=1 SCAR=6.9
Pod undocumented
414  
415 0 $filters = $self->__sanitizeFilters($filters);
416 0 $filters->{lingua} = $self->__getAsset->{lingua};
417  
418 0 return $self->xref($variable, $filters);
419 }
420  
421 sub anchor {
422 0 ▶ my ($self, $filters) = @_;
Subroutine anchor: not called CC=1 SCAR=6.9
Pod undocumented
423  
424 0 my $href = escape_link $self->link($filters);
425 0 my $title = html_escape $self->xref(title => $filters);
426  
427 0 return qq{<a href="$href">$title</a>};
428 }
429  
430 sub lanchor {
431 0 ▶ my ($self, $filters) = @_;
Subroutine lanchor: not called CC=1 SCAR=6.9
Pod undocumented
432  
433 0 my $href = escape_link $self->llink($filters);
434 0 my $title = html_escape $self->lxref(title => $filters);
435  
436 0 return qq{<a href="$href">$title</a>};
437 }
438  
439 sub linkPost {
440 0 ▶ my ($self, $filters) = @_;
Subroutine linkPost: not called CC=1 SCAR=6.9
Pod undocumented
441  
442 0 $filters = $self->__sanitizeFilters($filters);
443 0 $filters->{type} = 'post';
444  
445 0 return $self->link($filters);
446 }
447  
448 sub llinkPost {
449 0 ▶ my ($self, $filters) = @_;
Subroutine llinkPost: not called CC=1 SCAR=6.9
Pod undocumented
450  
451 0 $filters = $self->__sanitizeFilters($filters);
452 0 $filters->{lingua} = $self->__getAsset->{lingua};
453  
454 0 return $self->linkPost($filters);
455 }
456  
457 sub writeAsset {
458 0 ▶ my ($self, $path, $overlay, $extra) = @_;
Subroutine writeAsset: not called CC=3 SCAR=24.8
Pod undocumented
459  
460 0 ▶ die "usage: writeAsset(PATH, OVERLAY, KEY = VALUE, ...\n"
Branch: 1 branch
BranchTrueFalseTotal
if &empty($path) or &empty($overlay) 0 0 0
Condition0%0/3
&empty($path) or &empty($overlay)
l!l&&r!l&&!r
000
MC/DC0%0/2
&empty($path) or &empty($overlay)
&empty($path) &empty($overlay)
Truth table0%0/3
A or B
A= &empty($path)B= &empty($overlay)
ABresult
1X1
011
000
461 if empty $path || empty $overlay;
462 0 $overlay = $self->__sanitizeHashref($overlay, 'include');
463 0 $extra = $self->__sanitizeHashref($extra, 'include', 1);
464  
465 0 my $asset = $self->__include($path, $overlay, $extra);
466  
467 0 my $q = Qgoda->new;
468 0 my $logger = $q->logger('template');
469 0 my $builder = Qgoda::Builder->new;
470 0 my $site = $q->getSite;
471  
472 0 $builder->saveArtefact($asset, $site, $asset->{location});
473 $logger->debug(__x("successfully built '{location}'",
474 0 location => $asset->{location}));
475  
476 0 return '';
477 }
478  
479 sub clone {
480 0 ▶ my ($self, $extra) = @_;
Subroutine clone: not called CC=3 SCAR=24.8
Pod undocumented
481  
482 0 $extra = $self->__sanitizeHashref($extra, 'clone', 1);
483 die __"the argument 'location' is mandatory for clone()\n"
484 0 ▶ if empty $extra->{location};
Branch: 1 branch
BranchTrueFalseTotal
if &empty($extra->{"location"}) 0 0 0
485  
486 0 my $asset = $self->__getAsset;
487 my $parent = $asset->{parent} ? $asset->{parent} : {
488 location => $asset->{location},
489 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$asset->{'parent'} ? : 0 0 0
490  
491 # Force these values.
492 0 $extra->{relpath} = $asset->getRelpath;
493 0 $extra->{path} = $asset->getPath;
494 0 $extra->{parent} = $parent;
495 0 return $self->writeAsset($asset->getRelpath, $asset, $extra);
496 }
497  
498 sub strftime {
499 2 ▶ my ($self, $format, $date, $lingua) = @_;
Subroutine strftime: called CC=7 SCAR=20.5
500  
501 2 ▶ my $time = $date =~ /^[-+]?[1-9][0-9]*$/ ? "$date" : str2time $date;
Branch: 1 branch
BranchTrueFalseTotal
$date =~ /^[-+]?[1-9][0-9]*$/ ? : 2 0 2
502 2 ▶ $time = $date if !defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 2 2
503  
504 2 ▶ $format = '%c' if empty $format;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($format) 0 2 2
505  
506 2 my $saved_locale;
507 2 ▶ if (!empty $lingua) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&empty($lingua)) 2 0 2
508 2 $saved_locale = POSIX::setlocale(LC_ALL);
509 2 ▶ web_set_locale($lingua, 'utf-8') if $lingua;
Branch: 1 branch
BranchTrueFalseTotal
if $lingua 2 0 2
510 }
511  
512 2 my $formatted_date = POSIX::strftime($format, localtime $time);
513  
514 2 ▶ POSIX::setlocale(LC_ALL, $saved_locale) if defined $saved_locale;
Branch: 1 branch
BranchTrueFalseTotal
if defined $saved_locale 2 0 2
515  
516 2 Encode::_utf8_on($formatted_date);
517  
518 2 return $formatted_date;
519 }
520  
521 sub try {
522 0 ▶ require Carp;
Subroutine try: not called CC=1 SCAR=6.9
Pod undocumented
523 0 Carp::croak("q.try is now invalid");
524 }
525  
526 sub paginate {
527 0 ▶ my ($self, $data) = @_;
Subroutine paginate: not called CC=18 SCAR=57.3
Pod undocumented
528  
529 0 $data = $self->__sanitizeHashref($data, 'paginate', 1);
530 die __"argument '{total}' is mandatory for paginate()\n"
531 0 ▶ if empty $data->{total};
Branch: 1 branch
BranchTrueFalseTotal
if &empty($data->{"total"}) 0 0 0
532 die __"argument '{total}' cannot be zero paginate()\n"
533 0 ▶ if !$data->{total};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"total"} 0 0 0
534  
535 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
536  
537 0 ▶ my $start = $data->{start} || 0;
Condition0%0/2
$data->{'start'} || 0
l!l
00
MC/DC0%0/1
$data->{'start'} || 0
$data->{'start'}
Truth table0%0/2
A
A= $data->{'start'}
Aresult
11
00
538 0 ▶ my $total = $data->{total} || return {};
Condition0%0/2
$data->{'total'} || (return {})
l!l
00
MC/DC0%0/1
$data->{'total'} || (return {})
$data->{'total'}
Truth table0%0/2
A
A= $data->{'total'}
Aresult
11
00
539 0 ▶ my $per_page = $data->{per_page} || 10;
Condition0%0/2
$data->{'per_page'} || 10
l!l
00
MC/DC0%0/1
$data->{'per_page'} || 10
$data->{'per_page'}
Truth table0%0/2
A
A= $data->{'per_page'}
Aresult
11
00
540 0 my $page0 = $start / $per_page;
541 0 my $page = $page0 + 1;
542 0 my $stem = $data->{stem};
543 0 my $extender = $data->{extender};
544 0 my $total_pages = 1 + ($total - 1) / $per_page;
545  
546 0 my $asset = $self->__getAsset;
547 0 my $location;
548 0 ▶ if (!empty $asset->{plocation}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not &empty($asset->{'plocation'})) { } 0 0 0
elsif ($asset->{'parent'}) { } 0 0 0
549 0 $location = $asset->{plocation};
550 } elsif ($asset->{parent}) {
551 0 $location = $asset->{parent}->{location};
552 } else {
553 0 $location = $asset->{location};
554 }
555 0 my $basename = basename $location;
556  
557 0 $basename =~ m{(.*?)(\..+)?$};
558 0 ▶ $stem = $1 if empty $stem;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($stem) 0 0 0
559 0 ▶ $extender = $2 if empty $extender;
Branch: 1 branch
BranchTrueFalseTotal
if &empty($extender) 0 0 0
560  
561 0 my ($next_start, $next_location);
562 0 ▶ if ($page < $total_pages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page < $total_pages) 0 0 0
563 0 $next_start = $start + $per_page;
564 0 $next_location = dirname $location;
565 0 $next_location .= "/${stem}-" . ($page + 1) . $extender;
566 }
567  
568 # FIXME! Not flexible enough. We cannot put pages into a subdirectory.
569 0 my @links;
570 0 ▶ for (my $i = 1; $i <= $total_pages; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $total_pages) 0 0 0
571 0 my $link = $stem;
572 0 ▶ $link .= "-$i" if $i > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 1 0 0 0
573 0 $link .= $extender;
574 0 push @links, $link;
575 }
576 0 ▶ my $previous_page = $page0 ? $page0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$page0 ? : 0 0 0
577 0 ▶ my $next_page = $start + $per_page < $total ? $page0 + 2 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$start + $per_page < $total ? : 0 0 0
578  
579 0 ▶ $previous_page = $links[$previous_page - 1] if defined $previous_page;
Branch: 1 branch
BranchTrueFalseTotal
if defined $previous_page 0 0 0
580 0 ▶ $next_page = $links[$next_page - 1] if defined $next_page;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next_page 0 0 0
581  
582 0 my @tabindexes = (0) x $#links;
583 0 $tabindexes[$page0] = -1;
584 0 ▶ $tabindexes[0] = -1 if !defined $previous_page;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $previous_page 0 0 0
585  
586 0 my $retval = {
587 start => $start,
588 page0 => $page0,
589 page => $page,
590 per_page => $per_page,
591 total_pages => $total_pages,
592 previous_link => $previous_page,
593 next_link => $next_page,
594 links => \@links,
595 tabindices => \@tabindexes,
596 tabindexes => \@tabindexes,
597 next_start => $next_start,
598 next_location => $next_location,
599 };
600  
601 0 return $retval;
602 }
603  
604 sub taxonomyValues {
605 0 ▶ my ($self, $taxonomy, $filters) = @_;
Subroutine taxonomyValues: not called CC=1 SCAR=6.9
Pod undocumented
606  
607 0 my $filters = $self->__sanitizeFilters($filters);
608 0 my $site = Qgoda->new->getSite;
609  
610 0 my @values = $site->getTaxonomyValues($taxonomy, %$filters);
611  
612 0 return [sort @values];
613 }
614  
615 sub ltaxonomyValues {
616 0 ▶ my ($self, $taxonomy, $filters) = @_;
Subroutine ltaxonomyValues: not called CC=1 SCAR=6.9
Pod undocumented
617  
618 0 $filters = $self->__sanitizeFilters($filters);
619 0 $filters->{lingua} = $self->__getAsset->{lingua};
620  
621 0 return $self->taxonomyValues($taxonomy, $filters);
622 }
623  
624 sub sprintf {
625 0 ▶ my ($self, $fmt, @args) = @_;
Subroutine sprintf: not called CC=1 SCAR=6.9
Pod undocumented
626  
627 0 return sprintf $fmt, @args;
628 }
629  
630 sub loadJSON {
631 0 ▶ my ($self, $filename) = @_;
Subroutine loadJSON: not called CC=5 SCAR=34.0
Pod undocumented
632  
633 0 ▶ die __x("loadJSON('{filenname}'): absolute paths are not allowed!\n",
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($filename) 0 0 0
634 filename => $filename)
635 if File::Spec->file_name_is_absolute($filename);
636  
637 0 my $absolute = File::Spec->rel2abs($filename, Qgoda->new->config->{srcdir});
638 0 my ($volume, $directories, undef) = File::Spec->splitpath($absolute);
639 0 my @directories = File::Spec->splitdir($absolute);
640 map {
641 0 ▶ $_ eq File::Spec->updir and
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "File::Spec"->updir and die &__x("'{filenname}'): '{updir}' is not allowed!\n", "filename", $filename, "updir", "File::Spec"->updir) 0 0 0
642 die __x("'{filenname}'): '{updir}' is not allowed!\n",
643 filename => $filename, updir => File::Spec->updir);
644 } File::Spec->splitdir($absolute);
645  
646 0 ▶ my $json = read_file $filename or return;
Branch: 1 branch
BranchTrueFalseTotal
my $json = &read_file($filename) or return 0 0 0
647 0 my $data = eval { decode_json $json };
648 0 ▶ return if !defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
649  
650 0 return $data;
651 }
652  
653 sub related {
654 0 ▶ my ($self, $threshold, $filters) = @_;
Subroutine related: not called CC=1 SCAR=6.9
Pod undocumented
655  
656 0 my $asset = $self->__getAsset;
657 0 my @related = map { $_->[0] }
658 0 sort { $b->[1] <=> $a->[1] }
659 0 grep { $_->[1] >= $threshold }
660 0 @{$asset->{related}};
661  
662 0 $filters = $self->__sanitizeFilters($filters);
663  
664 0 my $site = Qgoda->new->getSite;
665  
666 0 return $site->filter(\@related, %$filters);
667 }
668  
669 sub lrelated {
670 0 ▶ my ($self, $threshold, $filters) = @_;
Subroutine lrelated: not called CC=1 SCAR=6.9
Pod undocumented
671  
672 0 $filters = $self->__sanitizeFilters($filters);
673 0 $filters->{lingua} = $self->__getAsset->{lingua};
674  
675 0 return $self->related($threshold, $filters);
676 }
677  
678 2 1;
679  
680 =head1 NAME
681  
682 Template::Plugin::Qgoda - Interface to Qgoda from Template Toolkit
683  
684 =head1 SYNOPSIS
685  
686 In your template:
687  
688 [% USE q = Qgoda %]
689  
690 =head1 DESCRIPTION
691  
692 Please see L<http://www.qgoda.net/en/docs/qgoda-plug-in/> for exhaustive
693 documentation.
694  
695 =head1 SEE ALSO
696  
697 L<http://www.qgoda.net/en/docs/qgoda-plug-in/>,
698 L<qgoda>, L<Template>, L<Perl>
« Qgoda/HTMLFilter/TOC.pm ↑ Summary Qgoda/Util/Translate.pm »