lib/Template/Filters.pm

filter: Statementstmt 94.6% 212 / 224 Branchbran 67.8% 57 / 84 Conditioncond 62.7% 32 / 51 MC/DCmcdc 33.3% 10 / 30 Subroutinesub 94.2% 49 / 52 Podpod 20.0% 5 / 25 totaltotal 78.3% 365 / 466 CC 66 SCAR 17.7
CC
66
Cov
84%
fetch
21.0
html_entity_filter_factory
6.3
url_filter
6.3
CRAP 5.9
« blib/lib/Template/Plugin/Assert.pm ↑ Summary lib/Template/Document.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Filters
4 #
5 # DESCRIPTION
6 # Defines filter plugins as used by the FILTER directive.
7 #
8 # AUTHORS
9 # Andy Wardley <abw@wardley.org>, with a number of filters contributed
10 # by Leslie Michael Orchard <deus_x@nijacode.com>
11 #
12 # COPYRIGHT
13 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #============================================================================
19  
20 package Template::Filters;
21  
22 102 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 102 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 102 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 102 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 102 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 102 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 102 our $VERSION = '3.100';
30 102 our $AVAILABLE = { };
31 102 our $TRUNCATE_LENGTH = 32;
32 102 our $TRUNCATE_ADDON = '...';
33  
34  
35 #------------------------------------------------------------------------
36 # standard filters, defined in one of the following forms:
37 # name => \&static_filter
38 # name => [ \&subref, $is_dynamic ]
39 # If the $is_dynamic flag is set then the sub-routine reference
40 # is called to create a new filter each time it is requested; if
41 # not set, then it is a single, static sub-routine which is returned
42 # for every filter request for that name.
43 #------------------------------------------------------------------------
44  
45 our $FILTERS = {
46 # static filters
47 'html' => \&html_filter,
48 'html_para' => \&html_paragraph,
49 'html_break' => \&html_para_break,
50 'html_para_break' => \&html_para_break,
51 'html_line_break' => \&html_line_break,
52 'xml' => \&xml_filter,
53 'uri' => \&uri_filter,
54 'url' => \&url_filter,
55 1 ▶ 'upper' => sub { uc $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 1 ▶ 'lower' => sub { lc $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 ▶ 'ucfirst' => sub { ucfirst $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 ▶ 'lcfirst' => sub { lcfirst $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 ▶ 'stderr' => sub { print STDERR @_; return '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 16 ▶ 'trim' => sub { for ($_[0]) { s/^\s+//; s/\s+$// }; $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
61 0 ▶ 'null' => sub { return '' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 1 ▶ 'collapse' => sub { for ($_[0]) { s/^\s+//; s/\s+$//; s/\s+/ /g };
Subroutine __ANON__: called CC=2 SCAR=6.9
63 1 $_[0] },
64  
65 # dynamic filters
66 102 'html_entity' => [ \&html_entity_filter_factory, 1 ],
67 'indent' => [ \&indent_filter_factory, 1 ],
68 'format' => [ \&format_filter_factory, 1 ],
69 'truncate' => [ \&truncate_filter_factory, 1 ],
70 'repeat' => [ \&repeat_filter_factory, 1 ],
71 'replace' => [ \&replace_filter_factory, 1 ],
72 'remove' => [ \&remove_filter_factory, 1 ],
73 'eval' => [ \&eval_filter_factory, 1 ],
74 'evaltt' => [ \&eval_filter_factory, 1 ], # alias
75 'perl' => [ \&perl_filter_factory, 1 ],
76 'evalperl' => [ \&perl_filter_factory, 1 ], # alias
77 'redirect' => [ \&redirect_filter_factory, 1 ],
78 'file' => [ \&redirect_filter_factory, 1 ], # alias
79 'stdout' => [ \&stdout_filter_factory, 1 ],
80 };
81  
82 # name of module implementing plugin filters
83 102 our $PLUGIN_FILTER = 'Template::Plugin::Filter';
84  
85  
86  
87 #========================================================================
88 # -- PUBLIC METHODS --
89 #========================================================================
90  
91 #------------------------------------------------------------------------
92 # fetch($name, \@args, $context)
93 #
94 # Attempts to instantiate or return a reference to a filter sub-routine
95 # named by the first parameter, $name, with additional constructor
96 # arguments passed by reference to a list as the second parameter,
97 # $args. A reference to the calling Template::Context object is
98 # passed as the third parameter.
99 #
100 # Returns a reference to a filter sub-routine or a pair of values
101 # (undef, STATUS_DECLINED) or ($error, STATUS_ERROR) to decline to
102 # deliver the filter or to indicate an error.
103 #------------------------------------------------------------------------
104  
105 sub fetch {
106 184 ▶ my ($self, $name, $args, $context) = @_;
Subroutine fetch: called CC=19 SCAR=30.5
Pod documented
107 184 my ($factory, $is_dynamic, $filter, $error);
108  
109 $self->debug("fetch($name, ",
110 defined $args ? ('[ ', join(', ', @$args), ' ]') : '<no args>', ', ',
111 defined $context ? $context : '<no context>',
112 184 ▶ ')') if $self->{ DEBUG };
Branch: 3 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 184 184
defined $args ? : 0 0 0
defined $context ? : 0 0 0
113  
114 # allow $name to be specified as a reference to
115 # a plugin filter object; any other ref is
116 # assumed to be a coderef and hence already a filter;
117 # non-refs are assumed to be regular name lookups
118  
119 184 ▶ if (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) { } 4 180 184
120 4 ▶ if (blessed($name) && $name->isa($PLUGIN_FILTER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($name) and $name->isa($Template::Filters::PLUGIN_FILTER)) { } 0 4 4
Condition66%2/3
builtin::blessed($name) and $name->isa($Template::Filters::PLUGIN_FILTER)
!ll&&!rl&&r
220
MC/DC0%0/2
builtin::blessed($name) and $name->isa($Template::Filters::PLUGIN_FILTER)
builtin::blessed($name) $name->isa($Template::Filters::PLUGIN_FILTER)
Truth table66%2/3
A and B
A= builtin::blessed($name)B= $name->isa($Template::Filters::PLUGIN_FILTER)
ABresult
0X0
100
111
121 0 ▶ $factory = $name->factory()
Condition0%0/2
$name->factory || (return $self->error($name->error))
l!l
00
MC/DC0%0/1
$name->factory || (return $self->error($name->error))
$name->factory
Truth table0%0/2
A
A= $name->factory
Aresult
11
00
122 || return $self->error($name->error());
123 }
124 else {
125 4 return $name;
126 }
127 }
128 else {
129 return (undef, Template::Constants::STATUS_DECLINED)
130 unless ($factory = $self->{ FILTERS }->{ $name }
131 180 ▶ || $FILTERS->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
unless $factory = $self->{"FILTERS"}{$name} || $Template::Filters::FILTERS->{$name} 1 179 180
Condition100%3/3
$self->{'FILTERS'}{$name} || $Template::Filters::FILTERS->{$name}
l!l&&r!l&&!r
231561
MC/DC100%2/2
$self->{'FILTERS'}{$name} || $Template::Filters::FILTERS->{$name}
$self->{'FILTERS'}{$name} $Template::Filters::FILTERS->{$name}
Truth table100%3/3
A || B
A= $self->{'FILTERS'}{$name}B= $Template::Filters::FILTERS->{$name}
ABresult
1X1
011
000
132 }
133  
134 # factory can be an [ $code, $dynamic ] or just $code
135 179 ▶ if (ref $factory eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $factory eq 'ARRAY') { } 107 72 179
136 107 ($factory, $is_dynamic) = @$factory;
137 }
138 else {
139 72 $is_dynamic = 0;
140 }
141  
142 179 ▶ if (ref $factory eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $factory eq 'CODE') { } 177 2 179
143 177 ▶ if ($is_dynamic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dynamic) { } 105 72 177
144 # if the dynamic flag is set then the sub-routine is a
145 # factory which should be called to create the actual
146 # filter...
147 105 eval {
148 105 ▶ ($filter, $error) = &$factory($context, $args ? @$args : ());
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 79 26 105
149 };
150 105 ▶ $error ||= $@;
Condition100%3/3
$error ||= $@
l!l&&r!l&&!r
4299
MC/DC100%2/2
$error ||= $@
$error $@
Truth table100%3/3
A ||= B
A= $errorB= $@
ABresult
1X1
011
000
151 105 ▶ $error = "invalid FILTER for '$name' (not a CODE ref)"
Branch: 1 branch
BranchTrueFalseTotal
unless $error or ref $filter eq "CODE" 1 104 105
Condition100%3/3
$error or ref $filter eq "CODE"
l!l&&r!l&&!r
6981
MC/DC100%2/2
$error or ref $filter eq "CODE"
$error ref $filter eq "CODE"
Truth table100%3/3
A or B
A= $errorB= ref $filter eq "CODE"
ABresult
1X1
011
000
152 unless $error || ref($filter) eq 'CODE';
153 }
154 else {
155 # ...otherwise, it's a static filter sub-routine
156 72 $filter = $factory;
157 }
158 }
159 else {
160 2 $error = "invalid FILTER entry for '$name' (not a CODE ref)";
161 }
162  
163 179 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 9 170 179
164 return $self->{ TOLERANT }
165 9 ▶ ? (undef, Template::Constants::STATUS_DECLINED)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'TOLERANT'} ? : 0 9 9
166 : ($error, Template::Constants::STATUS_ERROR) ;
167 }
168 else {
169 170 return $filter;
170 }
171 }
172  
173  
174 #------------------------------------------------------------------------
175 # store($name, \&filter)
176 #
177 # Stores a new filter in the internal FILTERS hash. The first parameter
178 # is the filter name, the second a reference to a subroutine or
179 # array, as per the standard $FILTERS entries.
180 #------------------------------------------------------------------------
181  
182 sub store {
183 14 ▶ my ($self, $name, $filter) = @_;
Subroutine store: called CC=2 SCAR=7.0
Pod undocumented
184  
185 14 ▶ $self->debug("store($name, $filter)") if $self->{ DEBUG };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 14 14
186  
187 14 $self->{ FILTERS }->{ $name } = $filter;
188 14 return 1;
189 }
190  
191  
192 #========================================================================
193 # -- PRIVATE METHODS --
194 #========================================================================
195  
196 #------------------------------------------------------------------------
197 # _init(\%config)
198 #
199 # Private initialisation method.
200 #------------------------------------------------------------------------
201  
202 sub _init {
203 249 ▶ my ($self, $params) = @_;
Subroutine _init: called CC=4 SCAR=13.9
204  
205 249 ▶ $self->{ FILTERS } = $params->{ FILTERS } || { };
Condition100%2/2
$params->{'FILTERS'} || {}
l!l
2247
MC/DC100%1/1
$params->{'FILTERS'} || {}
$params->{'FILTERS'}
Truth table100%2/2
A
A= $params->{'FILTERS'}
Aresult
11
00
206 249 ▶ $self->{ TOLERANT } = $params->{ TOLERANT } || 0;
Condition100%2/2
$params->{'TOLERANT'} || 0
l!l
2247
MC/DC100%1/1
$params->{'TOLERANT'} || 0
$params->{'TOLERANT'}
Truth table100%2/2
A
A= $params->{'TOLERANT'}
Aresult
11
00
207 249 ▶ $self->{ DEBUG } = ( $params->{ DEBUG } || 0 )
Condition100%2/2
$params->{'DEBUG'} || 0
l!l
8241
MC/DC100%1/1
$params->{'DEBUG'} || 0
$params->{'DEBUG'}
Truth table100%2/2
A
A= $params->{'DEBUG'}
Aresult
11
00
208 & Template::Constants::DEBUG_FILTERS;
209  
210  
211 249 return $self;
212 }
213  
214 #========================================================================
215 # -- STATIC FILTER SUBS --
216 #========================================================================
217  
218 #------------------------------------------------------------------------
219 # uri_filter() and url_filter() below can match using either RFC3986 or
220 # RFC2732. See https://github.com/abw/Template2/issues/13
221 #-----------------------------------------------------------------------
222  
223 102 our $UNSAFE_SPEC = {
224 RFC2732 => q{A-Za-z0-9\-_.~!*'()},
225 RFC3986 => q{A-Za-z0-9\-_.~},
226 };
227 102 our $UNSAFE_CHARS = $UNSAFE_SPEC->{ RFC3986 };
228 102 our $URI_REGEX;
229 102 our $URL_REGEX;
230 102 our $URI_ESCAPES;
231  
232 sub use_rfc2732 {
233 1 ▶ $UNSAFE_CHARS = $UNSAFE_SPEC->{ RFC2732 };
Subroutine use_rfc2732: called CC=1 SCAR=0.0
Pod documented
234 1 $URI_REGEX = $URL_REGEX = undef;
235 }
236  
237 sub use_rfc3986 {
238 1 ▶ $UNSAFE_CHARS = $UNSAFE_SPEC->{ RFC3986 };
Subroutine use_rfc3986: called CC=1 SCAR=0.0
Pod documented
239 1 $URI_REGEX = $URL_REGEX = undef;
240 }
241  
242 sub uri_escapes {
243 return {
244 2 ▶ map { ( chr($_), sprintf("%%%02X", $_) ) } (0..255),
Subroutine uri_escapes: called CC=1 SCAR=0.0
Pod undocumented
245 };
246 }
247  
248 #------------------------------------------------------------------------
249 # uri_filter() [% FILTER uri %]
250 #
251 # URI escape a string. This code is borrowed from Gisle Aas' URI::Escape
252 # module, copyright 1995-2004. See RFC2396, RFC2732 and RFC3986 for
253 # details.
254 #-----------------------------------------------------------------------
255  
256 sub uri_filter {
257 18 ▶ my $text = shift;
Subroutine uri_filter: called CC=5 SCAR=16.3
Pod undocumented
258  
259 18 ▶ $URI_REGEX ||= qr/([^$UNSAFE_CHARS])/;
Condition66%2/3
$Template::Filters::URI_REGEX ||= qr/([^$Template::Filters::UNSAFE_CHARS])/l
l!l&&r!l&&!r
1440
MC/DC0%0/2
$Template::Filters::URI_REGEX ||= qr/([^$Template::Filters::UNSAFE_CHARS])/l
$Template::Filters::URI_REGEX qr/([^$Template::Filters::UNSAFE_CHARS])/l
Truth table66%2/3
A ||= B
A= $Template::Filters::URI_REGEXB= qr/([^$Template::Filters::UNSAFE_CHARS])/l
ABresult
1X1
011
000
260 18 ▶ $URI_ESCAPES ||= uri_escapes();
Condition66%2/3
$Template::Filters::URI_ESCAPES ||= uri_escapes()
l!l&&r!l&&!r
1620
MC/DC0%0/2
$Template::Filters::URI_ESCAPES ||= uri_escapes()
$Template::Filters::URI_ESCAPES uri_escapes()
Truth table66%2/3
A ||= B
A= $Template::Filters::URI_ESCAPESB= uri_escapes()
ABresult
1X1
011
000
261  
262 18 ▶ if ($] >= 5.008 && utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008" and utf8::is_utf8($text)) 1 17 18
Condition66%2/3
$] >= "5.008" and utf8::is_utf8($text)
!ll&&!rl&&r
0171
MC/DC50%1/2
$] >= "5.008" and utf8::is_utf8($text)
$] >= "5.008" utf8::is_utf8($text)
Truth table66%2/3
A and B
A= $] >= "5.008"B= utf8::is_utf8($text)
ABresult
0X0
100
111
263 1 utf8::encode($text);
264 }
265  
266 18 $text =~ s/$URI_REGEX/$URI_ESCAPES->{$1}/eg;
267 18 $text;
268 }
269  
270  
271  
272 #------------------------------------------------------------------------
273 # url_filter() [% FILTER uri %]
274 #
275 # NOTE: the difference: url vs uri.
276 # This implements the old-style, non-strict behaviour of the uri filter
277 # which allows any valid URL characters to pass through so that
278 # http://example.com/blah.html does not get the ':' and '/' characters
279 # munged.
280 #-----------------------------------------------------------------------
281  
282 sub url_filter {
283 4 ▶ my $text = shift;
Subroutine url_filter: called CC=5 SCAR=18.3
Pod undocumented
284  
285 4 ▶ $URL_REGEX ||= qr/([^;\/?:@&=+\$,$UNSAFE_CHARS])/;
Condition66%2/3
$Template::Filters::URL_REGEX ||= qr"([^;/?:\@&=+\$,$Template::Filters::UNSAFE_CHARS])"l
l!l&&r!l&&!r
130
MC/DC0%0/2
$Template::Filters::URL_REGEX ||= qr"([^;/?:\@&=+\$,$Template::Filters::UNSAFE_CHARS])"l
$Template::Filters::URL_REGEX qr"([^;/?:\@&=+\$,$Template::Filters::UNSAFE_CHARS])"l
Truth table66%2/3
A ||= B
A= $Template::Filters::URL_REGEXB= qr"([^;/?:\@&=+\$,$Template::Filters::UNSAFE_CHARS])"l
ABresult
1X1
011
000
286 4 ▶ $URI_ESCAPES ||= uri_escapes();
Condition33%1/3
$Template::Filters::URI_ESCAPES ||= uri_escapes()
l!l&&r!l&&!r
400
MC/DC0%0/2
$Template::Filters::URI_ESCAPES ||= uri_escapes()
$Template::Filters::URI_ESCAPES uri_escapes()
Truth table33%1/3
A ||= B
A= $Template::Filters::URI_ESCAPESB= uri_escapes()
ABresult
1X1
011
000
287  
288 4 ▶ if ($] >= 5.008 && utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.008" and utf8::is_utf8($text)) 0 4 4
Condition33%1/3
$] >= "5.008" and utf8::is_utf8($text)
!ll&&!rl&&r
040
MC/DC0%0/2
$] >= "5.008" and utf8::is_utf8($text)
$] >= "5.008" utf8::is_utf8($text)
Truth table33%1/3
A and B
A= $] >= "5.008"B= utf8::is_utf8($text)
ABresult
0X0
100
111
289 0 utf8::encode($text);
290 }
291  
292 4 $text =~ s/$URL_REGEX/$URI_ESCAPES->{$1}/eg;
293 4 $text;
294 }
295  
296  
297 #------------------------------------------------------------------------
298 # html_filter() [% FILTER html %]
299 #
300 # Convert any '<', '>', '&', '"' or "'" characters to the HTML
301 # equivalents, '&lt;', '&gt;', '&amp;', '&quot;' and '&#39;',
302 # respectively.
303 #------------------------------------------------------------------------
304  
305 sub html_filter {
306 17 ▶ my $text = shift;
Subroutine html_filter: called CC=2 SCAR=6.9
Pod undocumented
307 17 for ($text) {
308 17 s/&/&amp;/g;
309 17 s/</&lt;/g;
310 17 s/>/&gt;/g;
311 17 s/"/&quot;/g;
312 17 s/'/&#39;/g;
313 }
314 17 return $text;
315 }
316  
317  
318 #------------------------------------------------------------------------
319 # xml_filter() [% FILTER xml %]
320 #
321 # Same as the html filter, but uses &apos; for single quotes (the XML
322 # named entity) instead of &#39; (the numeric reference used for HTML).
323 #------------------------------------------------------------------------
324  
325 sub xml_filter {
326 2 ▶ my $text = shift;
Subroutine xml_filter: called CC=2 SCAR=6.9
Pod undocumented
327 2 for ($text) {
328 2 s/&/&amp;/g;
329 2 s/</&lt;/g;
330 2 s/>/&gt;/g;
331 2 s/"/&quot;/g;
332 2 s/'/&apos;/g;
333 }
334 2 return $text;
335 }
336  
337  
338 #------------------------------------------------------------------------
339 # html_paragraph() [% FILTER html_para %]
340 #
341 # Wrap each paragraph of text (delimited by two or more newlines) in the
342 # <p>...</p> HTML tags.
343 #------------------------------------------------------------------------
344  
345 sub html_paragraph {
346 1 ▶ my $text = shift;
Subroutine html_paragraph: called CC=1 SCAR=0.0
Pod undocumented
347 1 return "<p>\n"
348 . join("\n</p>\n\n<p>\n", split(/(?:\r?\n){2,}/, $text))
349 . "</p>\n";
350 }
351  
352  
353 #------------------------------------------------------------------------
354 # html_para_break() [% FILTER html_para_break %]
355 #
356 # Join each paragraph of text (delimited by two or more newlines) with
357 # <br><br> HTML tags.
358 #------------------------------------------------------------------------
359  
360 sub html_para_break {
361 2 ▶ my $text = shift;
Subroutine html_para_break: called CC=1 SCAR=0.0
Pod undocumented
362 2 $text =~ s|(\r?\n){2,}|$1<br />$1<br />$1|g;
363 2 return $text;
364 }
365  
366 #------------------------------------------------------------------------
367 # html_line_break() [% FILTER html_line_break %]
368 #
369 # replaces any newlines with <br> HTML tags.
370 #------------------------------------------------------------------------
371  
372 sub html_line_break {
373 1 ▶ my $text = shift;
Subroutine html_line_break: called CC=1 SCAR=0.0
Pod undocumented
374 1 $text =~ s|(\r?\n)|<br />$1|g;
375 1 return $text;
376 }
377  
378 #========================================================================
379 # -- DYNAMIC FILTER FACTORIES --
380 #========================================================================
381  
382 #------------------------------------------------------------------------
383 # html_entity_filter_factory(\%options) [% FILTER html %]
384 #
385 # Dynamic version of the static html filter which attempts to locate the
386 # Apache::Util or HTML::Entities modules to perform full entity encoding
387 # of the text passed. Returns an exception if one or other of the
388 # modules can't be located.
389 #------------------------------------------------------------------------
390  
391 sub use_html_entities {
392 1 ▶ require HTML::Entities;
Subroutine use_html_entities: called CC=1 SCAR=0.0
Pod documented
393 1 return ($AVAILABLE->{ HTML_ENTITY } = \&HTML::Entities::encode_entities);
394 }
395  
396 sub use_apache_util {
397 1 ▶ require Apache::Util;
Subroutine use_apache_util: called CC=1 SCAR=2.6
Pod documented
398 0 Apache::Util::escape_html(''); # TODO: explain this
399 0 return ($AVAILABLE->{ HTML_ENTITY } = \&Apache::Util::escape_html);
400 }
401  
402 sub html_entity_filter_factory {
403 1 ▶ my $context = shift;
Subroutine html_entity_filter_factory: called CC=5 SCAR=18.4
Pod undocumented
404 1 my $haz;
405  
406 # if Apache::Util is installed then we use escape_html
407 $haz = $AVAILABLE->{ HTML_ENTITY }
408 1 || eval { use_apache_util() }
409 1 ▶ || eval { use_html_entities() }
Condition37%3/8
$Template::Filters::AVAILABLE->{'HTML_ENTITY'} || eval { do { use_apache_util() } } || eval { do { use_html_entities() } } || -1
l!l
10
$Template::Filters::AVAILABLE->{"HTML_ENTITY"} or eval { do { use_apache_util() } } or eval { do { use_html_entities() } }
l!l&&r!l&&!r
010
$Template::Filters::AVAILABLE->{"HTML_ENTITY"} or eval { do { use_apache_util() } }
l!l&&r!l&&!r
001
MC/DC0%0/3
$Template::Filters::AVAILABLE->{'HTML_ENTITY'} || eval { do { use_apache_util() } } || eval { do { use_html_entities() } } || -1
$Template::Filters::AVAILABLE->{"HTML_ENTITY"} eval { do { use_apache_util() } } eval { do { use_html_entities() } }
Truth table25%1/4
A or B or C
A= $Template::Filters::AVAILABLE->{"HTML_ENTITY"}B= eval { do { use_apache_util() } }C= eval { do { use_html_entities() } }
ABCresult
1XX1
01X1
0011
0000
410 || -1; # we use -1 for "not available" because it's a true value
411  
412 1 ▶ return ref $haz eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $haz eq 'CODE' ? : 1 0 1
413 ? $haz
414 : (undef, Template::Exception->new(
415 html_entity => 'cannot locate Apache::Util or HTML::Entities' )
416 );
417 }
418  
419  
420 #------------------------------------------------------------------------
421 # indent_filter_factory($pad) [% FILTER indent(pad) %]
422 #
423 # Create a filter to indent text by a fixed pad string or when $pad is
424 # numerical, a number of space.
425 #------------------------------------------------------------------------
426  
427 sub indent_filter_factory {
428 16 ▶ my ($context, $pad) = @_;
Subroutine indent_filter_factory: called CC=3 SCAR=11.0
Pod undocumented
429 16 ▶ $pad = 4 unless defined $pad;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pad 4 12 16
430 16 ▶ $pad = ' ' x $pad if $pad =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $pad =~ /^\d+$/l 13 3 16
431  
432 return sub {
433 16 ▶ my $text = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
434 16 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 16 16
435 16 $text =~ s/^/$pad/mg;
436 16 return $text;
437 }
438 16 }
439  
440 #------------------------------------------------------------------------
441 # format_filter_factory() [% FILTER format(format) %]
442 #
443 # Create a filter to format text according to a printf()-like format
444 # string.
445 #------------------------------------------------------------------------
446  
447 sub format_filter_factory {
448 11 ▶ my ($context, $format) = @_;
Subroutine format_filter_factory: called CC=2 SCAR=6.9
Pod undocumented
449 11 ▶ $format = '%s' unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 3 8 11
450  
451 return sub {
452 19 ▶ my $text = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
453 19 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 19 19
454 19 return join("\n", map{ sprintf($format, $_) } split(/\n/, $text));
455 }
456 11 }
457  
458  
459 #------------------------------------------------------------------------
460 # repeat_filter_factory($n) [% FILTER repeat(n) %]
461 #
462 # Create a filter to repeat text n times.
463 #------------------------------------------------------------------------
464  
465 sub repeat_filter_factory {
466 3 ▶ my ($context, $iter) = @_;
Subroutine repeat_filter_factory: called CC=3 SCAR=11.8
Pod undocumented
467 3 ▶ $iter = 1 unless defined $iter and length $iter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iter and length $iter 0 3 3
Condition33%1/3
defined $iter and length $iter
!ll&&!rl&&r
003
MC/DC0%0/2
defined $iter and length $iter
defined $iter length $iter
Truth table33%1/3
A and B
A= defined $iterB= length $iter
ABresult
0X0
100
111
468  
469 return sub {
470 3 ▶ my $text = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
471 3 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 3 3
472 3 return join('\n', $text) x $iter;
473 }
474 3 }
475  
476  
477 #------------------------------------------------------------------------
478 # replace_filter_factory($s, $r) [% FILTER replace(search, replace) %]
479 #
480 # Create a filter to replace 'search' text with 'replace'
481 #------------------------------------------------------------------------
482  
483 sub replace_filter_factory {
484 12 ▶ my ($context, $search, $replace) = @_;
Subroutine replace_filter_factory: called CC=3 SCAR=11.3
Pod undocumented
485 12 ▶ $search = '' unless defined $search;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $search 0 12 12
486 12 ▶ $replace = '' unless defined $replace;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replace 0 12 12
487  
488 return sub {
489 13 ▶ my $text = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
490 13 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 13 13
491 13 $text =~ s/$search/$replace/g;
492 13 return $text;
493 }
494 12 }
495  
496  
497 #------------------------------------------------------------------------
498 # remove_filter_factory($text) [% FILTER remove(text) %]
499 #
500 # Create a filter to remove 'search' string from the input text.
501 #------------------------------------------------------------------------
502  
503 sub remove_filter_factory {
504 6 ▶ my ($context, $search) = @_;
Subroutine remove_filter_factory: called CC=1 SCAR=0.0
Pod undocumented
505  
506 return sub {
507 6 ▶ my $text = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
508 6 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 6 6
509 6 $text =~ s/$search//g;
510 6 return $text;
511 }
512 6 }
513  
514  
515 #------------------------------------------------------------------------
516 # truncate_filter_factory($n) [% FILTER truncate(n) %]
517 #
518 # Create a filter to truncate text after n characters.
519 #------------------------------------------------------------------------
520  
521 sub truncate_filter_factory {
522 28 ▶ my ($context, $len, $char) = @_;
Subroutine truncate_filter_factory: called CC=4 SCAR=13.9
Pod undocumented
523 28 ▶ $len = $TRUNCATE_LENGTH unless defined $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 1 27 28
524 28 ▶ $char = $TRUNCATE_ADDON unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 16 12 28
525  
526 # Calculate visual length of the addon, treating HTML character entity
527 # references (e.g. &hellip; &#8230; &#x2026;) as single characters
528 28 my $lchar = _visual_length($char);
529 28 ▶ if ($len < $lchar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $lchar) 2 26 28
530 2 $char = substr($char, 0, $len);
531 2 $lchar = $len;
532 }
533  
534 return sub {
535 28 ▶ my $text = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
536 28 ▶ return $text if _visual_length($text) <= $len;
Branch: 1 branch
BranchTrueFalseTotal
if _visual_length($text) <= $len 7 21 28
537 21 return _truncate_visual($text, $len - $lchar) . $char;
538 }
539 28 }
540  
541  
542 #------------------------------------------------------------------------
543 # _visual_length($str)
544 #
545 # Returns the "visual" length of a string, counting each HTML character
546 # entity reference (&name; &#digits; &#xhex;) as a single character.
547 #------------------------------------------------------------------------
548  
549 sub _visual_length {
550 56 ▶ my $str = shift;
Subroutine _visual_length: called CC=1 SCAR=0.0
551 56 (my $copy = $str) =~ s/&(?:[a-zA-Z][a-zA-Z0-9]*|#[0-9]+|#x[0-9a-fA-F]+);/_/g;
552 56 return length $copy;
553 }
554  
555  
556 #------------------------------------------------------------------------
557 # _truncate_visual($str, $maxlen)
558 #
559 # Truncates $str to at most $maxlen visual characters, treating HTML
560 # character entity references as single characters and never splitting
561 # one in the middle.
562 #------------------------------------------------------------------------
563  
564 sub _truncate_visual {
565 21 ▶ my ($str, $maxlen) = @_;
Subroutine _truncate_visual: called CC=3 SCAR=11.0
566 21 my $result = '';
567 21 my $vlen = 0;
568  
569 21 ▶ while ($str =~ /\G(&(?:[a-zA-Z][a-zA-Z0-9]*|#[0-9]+|#x[0-9a-fA-F]+);|.)/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G(&(?:[a-zA-Z][a-zA-Z0-9]*|#[0-9]+|#x[0-9a-fA-F]+);|.)/gls) 246 0 246
570 246 ▶ last if $vlen >= $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
if $vlen >= $maxlen 21 225 246
571 225 $result .= $1;
572 225 $vlen++;
573 }
574 21 return $result;
575 }
576  
577  
578 #------------------------------------------------------------------------
579 # eval_filter_factory [% FILTER eval %]
580 #
581 # Create a filter to evaluate template text.
582 #------------------------------------------------------------------------
583  
584 sub eval_filter_factory {
585 3 ▶ my $context = shift;
Subroutine eval_filter_factory: called CC=1 SCAR=0.0
Pod undocumented
586  
587 return sub {
588 3 ▶ my $text = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
589 3 $context->process(\$text);
590 }
591 3 }
592  
593  
594 #------------------------------------------------------------------------
595 # perl_filter_factory [% FILTER perl %]
596 #
597 # Create a filter to process Perl text iff the context EVAL_PERL flag
598 # is set.
599 #------------------------------------------------------------------------
600  
601 sub perl_filter_factory {
602 4 ▶ my $context = shift;
Subroutine perl_filter_factory: called CC=2 SCAR=6.9
Pod undocumented
603 4 my $stash = $context->stash;
604  
605 4 ▶ return (undef, Template::Exception->new('perl', 'EVAL_PERL is not set'))
Branch: 1 branch
BranchTrueFalseTotal
unless $context->eval_perl 1 3 4
606 unless $context->eval_perl();
607  
608 return sub {
609 3 ▶ my $text = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
610 3 local($Template::Perl::context) = $context;
611 3 local($Template::Perl::stash) = $stash;
612 3 my $out = eval <<EOF;
613 package Template::Perl;
614 \$stash = \$context->stash();
615 $text
616 EOF
617 3 ▶ $context->throw($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
618 3 return $out;
619 }
620 3 }
621  
622  
623 #------------------------------------------------------------------------
624 # redirect_filter_factory($context, $file) [% FILTER redirect(file) %]
625 #
626 # Create a filter to redirect the block text to a file.
627 #------------------------------------------------------------------------
628  
629 sub redirect_filter_factory {
630 2 ▶ my ($context, $file, $options) = @_;
Subroutine redirect_filter_factory: called CC=4 SCAR=14.1
Pod undocumented
631 2 my $outpath = $context->config->{ OUTPUT_PATH };
632  
633 2 ▶ return (undef, Template::Exception->new('redirect',
Branch: 1 branch
BranchTrueFalseTotal
unless $outpath 1 1 2
634 'OUTPUT_PATH is not set'))
635 unless $outpath;
636  
637 1 ▶ $context->throw('redirect', "relative filenames are not supported: $file")
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[(^|/)\.\./]l 0 1 1
638 if $file =~ m{(^|/)\.\./};
639  
640 1 ▶ $options = { binmode => $options } unless ref $options;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options 1 0 1
641  
642 sub {
643 1 ▶ my $text = shift;
Subroutine __ANON__: called CC=3 SCAR=11.2
644 my $outpath = $context->config->{ OUTPUT_PATH }
645 1 ▶ || return '';
Condition50%1/2
$context->config->{'OUTPUT_PATH'} || (return '')
l!l
10
MC/DC0%0/1
$context->config->{'OUTPUT_PATH'} || (return '')
$context->config->{'OUTPUT_PATH'}
Truth table50%1/2
A
A= $context->config->{'OUTPUT_PATH'}
Aresult
11
00
646 1 $outpath .= "/$file";
647 1 my $error = Template::_output($outpath, \$text, $options);
648 1 ▶ die Template::Exception->new('redirect', $error)
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 1 1
649 if $error;
650 1 return '';
651 }
652 1 }
653  
654  
655 #------------------------------------------------------------------------
656 # stdout_filter_factory($context, $binmode) [% FILTER stdout(binmode) %]
657 #
658 # Create a filter to print a block to stdout, with an optional binmode.
659 #------------------------------------------------------------------------
660  
661 sub stdout_filter_factory {
662 0 ▶ my ($context, $options) = @_;
Subroutine stdout_filter_factory: not called CC=2 SCAR=17.9
Pod undocumented
663  
664 0 ▶ $options = { binmode => $options } unless ref $options;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options 0 0 0
665  
666 sub {
667 0 ▶ my $text = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
668 0 ▶ binmode(STDOUT) if $options->{ binmode };
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"binmode"} 0 0 0
669 0 print STDOUT $text;
670 0 return '';
671 }
672 0 }
673  
674  
675 102 1;
676  
677 __END__
« blib/lib/Template/Plugin/Assert.pm ↑ Summary lib/Template/Document.pm »