lib/App/Dapper/Filters.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 12 / 12 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 100% 81 / 81 CC 9 SCAR 10.0
CC
9
Cov
100%
replace_last
6.0
xml_escape
2.0
date_to_xmlschema
2.0
CRAP 2.7
« lib/App/Dapper/Markdown.pm ↑ 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 191 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 191 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 191 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 191 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 191 ▶use DateTime::Format::XSD;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 191 ▶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 37 ▶sub filters {
Subroutine filters: called CC=1 SCAR=0.0
Pod documented
41 return {
42 'xml_escape' => \&xml_escape,
43 'date_to_xmlschema' => \&date_to_xmlschema,
44 2 ▶ 'replace_last' => [ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 2 my ($context, @args) = @_;
46 2 ▶ return sub { replace_last($_[0], @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
47 37 }, 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 191 my %XML_ENTITIES = (
60 '&' => '&amp;',
61 '<' => '&lt;',
62 '>' => '&gt;',
63 '"' => '&quot;',
64 "'" => '&#39;',
65 );
66  
67 138 ▶sub xml_escape {
Subroutine xml_escape: called CC=2 SCAR=6.9
Pod documented
68 138 my $str = shift;
69  
70 138 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 2 136 138
71 136 $str =~ s/([&<>"'])/$XML_ENTITIES{$1}/g;
72 136 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 10 ▶sub date_to_xmlschema {
Subroutine date_to_xmlschema: called CC=2 SCAR=6.9
Pod documented
91 10 my $date = shift;
92  
93 10 ▶ return '' unless defined $date;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date 1 9 10
94 9 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 22 ▶sub replace_last {
Subroutine replace_last: called CC=6 SCAR=17.9
Pod documented
107 22 my ($input, $string, $replacement) = @_;
108  
109 22 ▶ return $input unless defined $input and defined $string and length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input and defined $string and length $string 3 19 22
Condition100%6/6
defined $input and defined $string and length $string
!ll&&!rl&&r
2119
defined $input and defined $string
!ll&&!rl&&r
1120
MC/DC100%3/3
defined $input and defined $string and length $string
defined $input defined $string length $string
Truth table100%4/4
A and B and C
A= defined $inputB= defined $stringC= length $string
ABCresult
0XX0
10X0
1100
1111
110  
111 19 ▶ $replacement = defined $replacement ? $replacement : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $replacement ? : 16 3 19
112 19 my $index = rindex($input, $string);
113 19 ▶ return $input if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 4 15 19
114  
115 15 substr($input, $index, length $string) = $replacement;
116 15 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 20 ▶sub smart {
Subroutine smart: called CC=2 SCAR=6.9
Pod documented
127 20 my $input = shift;
128  
129 20 ▶ return '' unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 1 19 20
130 19 $input =~ s/(?<!-)---(?!-)/&mdash;/g;
131 19 $input =~ s/(?<!-)--(?!-)/&ndash;/g;
132  
133 19 return $input;
134 }
135  
136 191 1;
« lib/App/Dapper/Markdown.pm ↑ Summary lib/App/Dapper/Defaults.pm »