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