| 1 | package Template::Pure::Filters; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 14 | ▶ | use Data::Dumper (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use URI::Escape (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use Template::Pure::EncodedString; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | sub all { | ||||||||||||||||||||||||||||||||||||||||||
| 11 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | format => \&format, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | strftime => \&strftime, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | dump => \&dump, | ||||||||||||||||||||||||||||||||||||||||||
| 15 | uri_escape_utf8 => \&uri_escape_utf8, | ||||||||||||||||||||||||||||||||||||||||||
| 16 | uri_escape => \&uri_escape, | ||||||||||||||||||||||||||||||||||||||||||
| 17 | upper => \&upper, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | lower => \&lower, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | upper_first => \&upper_first, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | lower_first => \&lower_first, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | collapse => \&collapse, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | encoded_string => \&encoded_string, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | escape_html => \&escape_html, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | truncate => \&truncate, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | repeat => \&repeat, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | remove => \&remove, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | replace => \&replace, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | comma => \&comma, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ltrim => \<rim, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | rtrim => \&rtrim, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | trim => \&trim, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | default => \&default, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | sub escape_html { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 69 | ▶ | my ($value) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
escape_html: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 69 | my %_escape_table = ( | |||||||||||||||||||||||||||||||||||||||||
| 39 | '&' => '&', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | '>' => '>', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | '<' => '<', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | q{"} => '"', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | q{'} => ''' ); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 69 | ▶ | if( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($value) and $value->isa("Template::Pure::EncodedString")
MC/DC100%2/2
builtin::blessed($value) and $value->isa("Template::Pure::EncodedString")
builtin::blessed($value)
$value->isa("Template::Pure::EncodedString")
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa("Template::Pure::EncodedString")
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | Scalar::Util::blessed($value) && | ||||||||||||||||||||||||||||||||||||||||||
| 47 | $value->isa('Template::Pure::EncodedString') | ||||||||||||||||||||||||||||||||||||||||||
| 48 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 15 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 54 | ▶ | $value =~ s/([&><"'])/$_escape_table{$1}/ge unless | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not defined $value or builtin::blessed($value) and $value->isa("Template::Pure::UndefObject")
builtin::blessed($value) and $value->isa("Template::Pure::UndefObject")
MC/DC0%0/3
not defined $value or builtin::blessed($value) and $value->isa("Template::Pure::UndefObject")
not defined $value
builtin::blessed($value)
$value->isa("Template::Pure::UndefObject")
Truth table50%2/4
not(A) or B and C
A= not defined $valueB= builtin::blessed($value)C= $value->isa("Template::Pure::UndefObject")
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | !defined($value) || | ||||||||||||||||||||||||||||||||||||||||||
| 53 | (Scalar::Util::blessed($value) && $value->isa('Template::Pure::UndefObject')); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 54 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | sub format { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | my ($template, $data, $format) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | return sprintf($format, $data); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | sub strftime { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | my ($template, $date_obj, $format) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
strftime: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | die "Must be an object that does 'strftime'" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($date_obj) and $date_obj->can("strftime")
MC/DC0%0/2
builtin::blessed($date_obj) and $date_obj->can("strftime")
builtin::blessed($date_obj)
$date_obj->can("strftime")
Truth table33%1/3
A and B
A= builtin::blessed($date_obj)B= $date_obj->can("strftime")
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | unless Scalar::Util::blessed($date_obj) && $date_obj->can('strftime'); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | return $date_obj->strftime($format); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | sub dump { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my ($template, $value) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | warn Data::Dumper::Dumper $value; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | sub uri_escape { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | my ($template, $data, $unsafe) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
uri_escape: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if($unsafe) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return URI::Escape::uri_escape($data, $unsafe); | |||||||||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | return URI::Escape::uri_escape($data); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | sub uri_escape_utf8 { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | my ($template, $data, $unsafe) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
uri_escape_utf8: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | ▶ | if($unsafe) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return URI::Escape::uri_escape_utf8($data, $unsafe); | |||||||||||||||||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | return URI::Escape::uri_escape_utf8($data); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | sub upper { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
upper: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | return uc $data; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | sub lower { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lower: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | return lc $data; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | sub upper_first { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
upper_first: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | return ucfirst $data; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | sub lower_first { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lower_first: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | return lcfirst $data; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | sub collapse { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
collapse: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | for ($data) { | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | sub encoded_string { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encoded_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | return Template::Pure::EncodedString->new($data); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | sub truncate { | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 7 | ▶ | my ($template, $data, $length, $affix) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
truncate: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 7 | ▶ | my $allowed_length = $length - length($affix||''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$affix || ''
MC/DC100%1/1
$affix || ''
$affix
Truth table100%2/2
A
A= $affix
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 7 | ▶ | if(length $data > $allowed_length) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 5 | ▶ | $data = substr($data, 0, $allowed_length) . ($affix||''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$affix || ''
MC/DC100%1/1
$affix || ''
$affix
Truth table100%2/2
A
A= $affix
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 7 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | sub repeat { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | ▶ | my ($template, $data, $times) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
repeat: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 3 | return $data x $times; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | sub remove { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | ▶ | my ($template, $data, $match) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | $data =~ s/$match//g; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | return $data | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | sub replace { | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | my ($template, $data, $match, $replace) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | $data =~ s/$match/$replace/g; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | return $data | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | sub default { | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | my ($template, $data, $default) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return defined($data) ? $data : $default; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | sub ltrim { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ltrim: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | $data =~s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | sub rtrim { | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rtrim: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | $data =~s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | sub trim { | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trim: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | $data =~s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | sub comma { | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | ▶ | my ($template, $data) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
comma: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | 1 while $data =~ s/((?:\A|[^.0-9])[-+]?\d+)(\d{3})/$1,$2/s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | Template::Pure::Filters - Default Data Filters | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | my $html = qq[ | ||||||||||||||||||||||||||||||||||||||||||
| 194 | <html> | ||||||||||||||||||||||||||||||||||||||||||
| 195 | <head> | ||||||||||||||||||||||||||||||||||||||||||
| 196 | <title>Page Title</title> | ||||||||||||||||||||||||||||||||||||||||||
| 197 | </head> | ||||||||||||||||||||||||||||||||||||||||||
| 198 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 199 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 200 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 201 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | my $pure = Template::Pure->new( | ||||||||||||||||||||||||||||||||||||||||||
| 204 | template=>$html, | ||||||||||||||||||||||||||||||||||||||||||
| 205 | directives=> [ | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 'body' => 'content | repeat(3) | escape_html', | ||||||||||||||||||||||||||||||||||||||||||
| 207 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 208 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | my $data = +{ | ||||||||||||||||||||||||||||||||||||||||||
| 211 | content => q[ | ||||||||||||||||||||||||||||||||||||||||||
| 212 | <p>Are you <b>doomed</b> to discover that you never recovered from the narcoleptic | ||||||||||||||||||||||||||||||||||||||||||
| 213 | country in which you once <u>stood?</u> Where the fire's always burning, but | ||||||||||||||||||||||||||||||||||||||||||
| 214 | there's <i>never enough wood</i></p>? | ||||||||||||||||||||||||||||||||||||||||||
| 215 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 216 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | print $pure->render($data); | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | Returns: | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | <html> | ||||||||||||||||||||||||||||||||||||||||||
| 223 | <head> | ||||||||||||||||||||||||||||||||||||||||||
| 224 | <title>Page Title</title> | ||||||||||||||||||||||||||||||||||||||||||
| 225 | </head> | ||||||||||||||||||||||||||||||||||||||||||
| 226 | <body> | ||||||||||||||||||||||||||||||||||||||||||
| 227 | <p>Are you <b>doomed</b> to discover that you never recovered from the narcoleptic | ||||||||||||||||||||||||||||||||||||||||||
| 228 | country in which you once <u>stood?</u> Where the fire's always burning, but | ||||||||||||||||||||||||||||||||||||||||||
| 229 | there's <i>never enough wood</i></p> | ||||||||||||||||||||||||||||||||||||||||||
| 230 | <p>Are you <b>doomed</b> to discover that you never recovered from the narcoleptic | ||||||||||||||||||||||||||||||||||||||||||
| 231 | country in which you once <u>stood?</u> Where the fire's always burning, but | ||||||||||||||||||||||||||||||||||||||||||
| 232 | there's <i>never enough wood</i></p> | ||||||||||||||||||||||||||||||||||||||||||
| 233 | <p>Are you <b>doomed</b> to discover that you never recovered from the narcoleptic | ||||||||||||||||||||||||||||||||||||||||||
| 234 | country in which you once <u>stood?</u> Where the fire's always burning, but | ||||||||||||||||||||||||||||||||||||||||||
| 235 | there's <i>never enough wood</i></p> | ||||||||||||||||||||||||||||||||||||||||||
| 236 | </body> | ||||||||||||||||||||||||||||||||||||||||||
| 237 | </html> | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | (Note that the embedded HTML was not HTML encoded due to the use of the 'escape_html' | ||||||||||||||||||||||||||||||||||||||||||
| 240 | filter.) | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | Container modules for all the filters that are bundled with L<Template::Pure>. Please | ||||||||||||||||||||||||||||||||||||||||||
| 245 | see L<Template::Pure/"Filtering your data"> for usage. A lot of this is copied from | ||||||||||||||||||||||||||||||||||||||||||
| 246 | L<Template::Filters> and other filters from template languages like L<Xslate>. | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | Filters are arrange in 'UNIX pipe' syntax, so the output of the first filter becomes the | ||||||||||||||||||||||||||||||||||||||||||
| 249 | input of the second and so forth. It also means filter are order sensitive. Some filters | ||||||||||||||||||||||||||||||||||||||||||
| 250 | like the 'escape_html' filter only make sense if they are the last in the pipe. | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | Some filters may take arguments, for example: | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | directives=> [ | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 'body' => 'content | repeat(3)', | ||||||||||||||||||||||||||||||||||||||||||
| 256 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | Generally these are literals which are parsed out via regular expressions and then we use C<eval> | ||||||||||||||||||||||||||||||||||||||||||
| 259 | to generate Perl values. As a result you are strongly encouraged to properly untaint and secure | ||||||||||||||||||||||||||||||||||||||||||
| 260 | these arguments (for example don't pass them in from a HTML form POST...). | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | You may pass arguments to filters via the data context using placeholder notation. Placeholder | ||||||||||||||||||||||||||||||||||||||||||
| 263 | notation may be freely mixed in with argument literals. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | directives=> [ | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 'body' => 'content | repeat(#{times_to_repeat})', | ||||||||||||||||||||||||||||||||||||||||||
| 267 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | Filters may be added to your current L<Template::Pure> instance: | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | my $pure = Template::Pure->new( | ||||||||||||||||||||||||||||||||||||||||||
| 272 | filters => { | ||||||||||||||||||||||||||||||||||||||||||
| 273 | my_custom_filter => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 274 | my ($template_pure, $data, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 275 | return $modified_data; | ||||||||||||||||||||||||||||||||||||||||||
| 276 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 277 | }, ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | Custom filters get the current L<Template::Pure> instance, the current data context and any | ||||||||||||||||||||||||||||||||||||||||||
| 280 | arguments; you are expected to return modified data. | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | =head1 FILTERS | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | This module defines the following subroutines that are used as filters for L<Template::Pure>: | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | =head2 format ($format) | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | The C<format> filter takes a format string as a parameter (as per C<sprintf()>) and formats the | ||||||||||||||||||||||||||||||||||||||||||
| 289 | data accordingly. | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | my $pure = Template::Pure->new( | ||||||||||||||||||||||||||||||||||||||||||
| 292 | template => '<p>Price: $<span>1.00</span></p>' | ||||||||||||||||||||||||||||||||||||||||||
| 293 | directives => [ | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 'span' => 'price | format(%.2f)', | ||||||||||||||||||||||||||||||||||||||||||
| 295 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 296 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | print $pure->render({ | ||||||||||||||||||||||||||||||||||||||||||
| 299 | price => '2.0000001' | ||||||||||||||||||||||||||||||||||||||||||
| 300 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | Output: | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | <p>Price: $<span>2.00</span></p> | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | =head2 strftime ($format) | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | Given an object that does 'strftime' return a string formatted date / time; | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | my $pure = Template::Pure->new( | ||||||||||||||||||||||||||||||||||||||||||
| 311 | template => '<p>Year</p>' | ||||||||||||||||||||||||||||||||||||||||||
| 312 | directives => [ | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 'p' => 'date | strftime(%Y)', | ||||||||||||||||||||||||||||||||||||||||||
| 314 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 315 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | print $pure->render({ | ||||||||||||||||||||||||||||||||||||||||||
| 318 | date => DateTime->new(year=>2016, month=>12), | ||||||||||||||||||||||||||||||||||||||||||
| 319 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | Output: | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | <p>2016</p> | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | =head2 dump | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | This filter doesn't actually change the data. It just uses L<Data::Dumper> and sends | ||||||||||||||||||||||||||||||||||||||||||
| 328 | C<Dumper> output to STDOUT via C<warn>. Can be useful during debugging when you can't | ||||||||||||||||||||||||||||||||||||||||||
| 329 | figure out what is amiss with your data. After it dumps to STDOUT we just return the | ||||||||||||||||||||||||||||||||||||||||||
| 330 | value unaltered. | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | =head2 uri_escape | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | =head2 uri_escape_utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | These filters are just wrappers around the same in L<URI::Escape>. | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | =head2 upper | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | =head2 lower | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | =head2 upper_first | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | =head2 lower_first | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | Does text case conversions using Perl built in functions C<uc>, C<lc>, C<ucfirst> and C<lcfirst>. | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | =head2 collapse | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | Collapse any whitespace sequences in the input text into a single space. Leading and trailing whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 351 | (which would be reduced to a single space) is removed, as per trim. | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | =head2 encoded_string | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | By default L<Template::Pure> escapes your values using a simple HTML escape function so that your | ||||||||||||||||||||||||||||||||||||||||||
| 356 | output is 'safe' from HTML injection attacks. However there might be cases when you wish to all raw | ||||||||||||||||||||||||||||||||||||||||||
| 357 | HTML to be injected into your template, froom known, safe data. In this case you can use this function | ||||||||||||||||||||||||||||||||||||||||||
| 358 | to mark your data as "don't encode". We will assume you know what you are doing... | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | =head2 escape_html | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | As mentioned in the previous filter documentation, we nearly always automatically escape your data values | ||||||||||||||||||||||||||||||||||||||||||
| 363 | when they get rendered into your template to produce a document. However as also mentioned there are | ||||||||||||||||||||||||||||||||||||||||||
| 364 | a few case where we don't, since we think its the more common desired behavior, such as when you | ||||||||||||||||||||||||||||||||||||||||||
| 365 | are injecting a template object or you are setting the value from the contents of a different node inside | ||||||||||||||||||||||||||||||||||||||||||
| 366 | the same template. In those cases, should HTML escaping be desired you can use this filter to make it so. | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | =head2 truncate ($length, $affix) | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | Truncates string data to $length (where $length is the total allowed number of characters). In the case | ||||||||||||||||||||||||||||||||||||||||||
| 371 | when we need to truncate, add $affix to the end to indicate truncation. For example you may set $affix | ||||||||||||||||||||||||||||||||||||||||||
| 372 | to '...' to indicate characters were removed. There is no default $affix. | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | B<NOTE> Should you use an $affix we automatically increase the required truncation so that the new string | ||||||||||||||||||||||||||||||||||||||||||
| 375 | INCLUDING THE $affix fits into the required $length. | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | =head2 repeat ($times) | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | Repeat a value by $times. | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | =head2 remove ($match) | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | Searches the input text for any occurrences of the specified string and removes them. | ||||||||||||||||||||||||||||||||||||||||||
| 384 | A Perl regular expression may be specified as the search string. | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | =head2 replace ($match, $replacement) | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | Like L</remove> but does a global search and replace instead of removing. Can also use | ||||||||||||||||||||||||||||||||||||||||||
| 389 | a regular expression in the $match | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | =head2 default ($value) | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | Should $data be undefined, use $value instead. | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | =head2 rtrim | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | =head2 ltrim | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | =head2 trim | ||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | Removes all whitespace from either the right side, left side, or both sides of the data | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | =head2 comma | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | Given a data value which is a number, comma-fy it for readability (100000 = > 100,000). | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | L<Template::Pure>. | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | But lots of this code was copied from L<Template::Filters> and other prior art on CPAN. Thanks! | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | Please see L<Template::Pure> for copyright and license information. | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||