lib/App/Dapper/Filters.pm

filter: Statementstmt 90.4% 38 / 42 Branchbran 58.3% 7 / 12 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 76.9% 10 / 13 Podpod 100% 5 / 5 totaltotal 76.5% 62 / 81 CC 9 SCAR 11.6
CC
9
Cov
78%
replace_last
7.1
date_to_xmlschema
2.0
xml_escape
2.0
CRAP 3.2
« bin/dapper ↑ Summary lib/App/Dapper/Defaults.pm »
1 package App::Dapper::Filters;
2  
3 =head1 NAME
4  
5 App::Dapper::Filters - Default template filters available to all projects.
6  
7 =head1 DESCRIPTION
8  
9 Filters contained here can be called from layout and source files using the
10 filter/pipe mechanism of the template language. Example:
11  
12 [% page.title | xml_escape %]
13  
14 They are in addition to the filters and methods built in to
15 L<Template::Alloy>, such as C<html>, C<upper> and C<json>. These filters only
16 apply to text: given a hash, a list or an object (such as a date) they print
17 nothing. For dates use the C<xmlschema> method (C<[% page.date.xmlschema %]>);
18 for dumping variables use the built-in C<json> method
19 (C<[% page.json(1) %]>).
20  
21 =cut
22  
23 32 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 32 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 32 ▶use DateTime::Format::XSD;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 32 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 =head2 filters
33  
34 Returns the hash of filters in the form expected by the C<FILTERS> option of
35 L<Template::Alloy>. Filters that take arguments (like C<replace_last>) are
36 registered as dynamic filters so the arguments reach them.
37  
38 =cut
39  
40 0 ▶sub filters {
Subroutine filters: not called CC=1 SCAR=6.9
Pod documented
41 return {
42 'xml_escape' => \&xml_escape,
43 'date_to_xmlschema' => \&date_to_xmlschema,
44 0 ▶ 'replace_last' => [ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 my ($context, @args) = @_;
46 0 ▶ return sub { replace_last($_[0], @args) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 }, 1 ],
48 'smart' => \&smart,
49 };
50 }
51  
52 =head2 xml_escape
53  
54 Escape a string so that it can be included in XML or HTML files. The
55 characters C<&>, C<< < >>, C<< > >>, C<"> and C<'> are replaced with entities.
56  
57 =cut
58  
59 32 my %XML_ENTITIES = (
60 '&' => '&amp;',
61 '<' => '&lt;',
62 '>' => '&gt;',
63 '"' => '&quot;',
64 "'" => '&#39;',
65 );
66  
67 1 ▶sub xml_escape {
Subroutine xml_escape: called CC=2 SCAR=7.0
Pod documented
68 1 my $str = shift;
69  
70 1 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 1 1
71 1 $str =~ s/([&<>"'])/$XML_ENTITIES{$1}/g;
72 1 return $str;
73 }
74  
75 =head2 date_to_xmlschema
76  
77 Convert a date string to an XML Schema date that also includes the timezone.
78  
79 [% "2014-05-02T10:30:00-05:00" | date_to_xmlschema %]
80  
81 Filters only apply to text in templates, so for C<page.date> and
82 C<site.time> (which are date objects) use their C<xmlschema> method instead:
83  
84 [% page.date.xmlschema %]
85  
86 Called from Perl, it also accepts a L<DateTime> object.
87  
88 =cut
89  
90 2 ▶sub date_to_xmlschema {
Subroutine date_to_xmlschema: called CC=2 SCAR=7.1
Pod documented
91 2 my $date = shift;
92  
93 2 ▶ return '' unless defined $date;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date 0 2 2
94 2 return DateTime::Format::XSD->format_datetime(App::Dapper::Utils::parse_date($date));
95 }
96  
97 =head2 replace_last
98  
99 Replace the last occurrence of a string with another string. The string is
100 matched literally, not as a regular expression.
101  
102 [% "a, b, c" | replace_last(", ", " and ") %] # a, b and c
103  
104 =cut
105  
106 3 ▶sub replace_last {
Subroutine replace_last: called CC=6 SCAR=19.6
Pod documented
107 3 my ($input, $string, $replacement) = @_;
108  
109 3 ▶ return $input unless defined $input and defined $string and length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input and defined $string and length $string 0 3 3
Condition33%2/6
defined $input and defined $string and length $string
!ll&&!rl&&r
003
defined $input and defined $string
!ll&&!rl&&r
003
MC/DC0%0/3
defined $input and defined $string and length $string
defined $input defined $string length $string
Truth table25%1/4
A and B and C
A= defined $inputB= defined $stringC= length $string
ABCresult
0XX0
10X0
1100
1111
110  
111 3 ▶ $replacement = defined $replacement ? $replacement : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $replacement ? : 3 0 3
112 3 my $index = rindex($input, $string);
113 3 ▶ return $input if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 1 2 3
114  
115 2 substr($input, $index, length $string) = $replacement;
116 2 return $input;
117 }
118  
119 =head2 smart
120  
121 Replace C<---> with an em dash (C<&mdash;>) and C<--> with an en dash
122 (C<&ndash;>). Longer runs of dashes are left alone.
123  
124 =cut
125  
126 4 ▶sub smart {
Subroutine smart: called CC=2 SCAR=7.0
Pod documented
127 4 my $input = shift;
128  
129 4 ▶ return '' unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 4 4
130 4 $input =~ s/(?<!-)---(?!-)/&mdash;/g;
131 4 $input =~ s/(?<!-)--(?!-)/&ndash;/g;
132  
133 4 return $input;
134 }
135  
136 32 1;
« bin/dapper ↑ Summary lib/App/Dapper/Defaults.pm »