| 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 | '&' => '&', | ||||||||||||||||||||||||||||||||||||||||||
| 61 | '<' => '<', | ||||||||||||||||||||||||||||||||||||||||||
| 62 | '>' => '>', | ||||||||||||||||||||||||||||||||||||||||||
| 63 | '"' => '"', | ||||||||||||||||||||||||||||||||||||||||||
| 64 | "'" => ''', | ||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
defined $input and defined $string and length $string
defined $input and defined $string
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
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | $replacement = defined $replacement ? $replacement : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | my $index = rindex($input, $string); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | return $input if $index < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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<—>) and C<--> with an en dash | ||||||||||||||||||||||||||||||||||||||||||
| 122 | (C<–>). 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
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | $input =~ s/(?<!-)---(?!-)/—/g; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 4 | $input =~ s/(?<!-)--(?!-)/–/g; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 4 | return $input; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 32 | 1; | |||||||||||||||||||||||||||||||||||||||||