lib/Template/Filters.pm

filter: Statementstmt 94.6% 214 / 226 Branchbran 70.9% 44 / 62 Conditioncond 64.1% 50 / 78 MC/DCmcdc 36.3% 16 / 44 Subroutinesub 94.2% 49 / 52 Podpod 20.0% 5 / 25 totaltotal 77.6% 378 / 487 CC 68 SCAR 17.3
CC
68
Cov
84%
fetch
19.5
html_entity_filter_factory
6.3
stdout_filter_factory
6.0
CRAP 5.6
« lib/Template/Document.pm ↑ Summary lib/Template/Directive.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 106 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 106 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 106 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 106 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 106 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 106 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 106 our $VERSION = '3.106';
30 106 our $AVAILABLE = { };
31 106 our $TRUNCATE_LENGTH = 32;
32 106 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 106 '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 106 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 186 ▶sub fetch {
Subroutine fetch: called CC=19 SCAR=29.7
Pod documented
106 186 my ($self, $name, $args, $context) = @_;
107 186 my ($factory, $is_dynamic, $filter, $error);
108  
109 $self->debug("fetch($name, ",
110 defined $args ? ('[ ', join(', ', @$args), ' ]') : '<no args>', ', ',
111 $context // '<no context>',
112 186 ▶ ')') if $self->{ DEBUG };
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 186 186
defined $args ? : 0 0 0
Condition0%0/2
$context // "<no context>"
l!l
00
MC/DC0%0/1
$context // "<no context>"
$context
Truth table0%0/2
A
A= $context
Aresult
11
00
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 186 ▶ if (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) { } 6 180 186
120 6 ▶ if (blessed($name) && $name->isa($PLUGIN_FILTER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($name) and $name->isa($Template::Filters::PLUGIN_FILTER)) { } 2 4 6
Condition100%3/3
builtin::blessed($name) and $name->isa($Template::Filters::PLUGIN_FILTER)
!ll&&!rl&&r
222
MC/DC100%2/2
builtin::blessed($name) and $name->isa($Template::Filters::PLUGIN_FILTER)
builtin::blessed($name) $name->isa($Template::Filters::PLUGIN_FILTER)
Truth table100%3/3
A and B
A= builtin::blessed($name)B= $name->isa($Template::Filters::PLUGIN_FILTER)
ABresult
0X0
100
111
121 2 ▶ $factory = $name->factory()
Condition50%1/2
$name->factory || (return $self->error($name->error))
l!l
20
MC/DC0%0/1
$name->factory || (return $self->error($name->error))
$name->factory
Truth table50%1/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 181 ▶ if (ref $factory eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $factory eq 'ARRAY') { } 107 74 181
136 107 ($factory, $is_dynamic) = @$factory;
137 }
138 else {
139 74 $is_dynamic = 0;
140 }
141  
142 181 ▶ if (ref $factory eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $factory eq 'CODE') { } 179 2 181
143 179 ▶ if ($is_dynamic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dynamic) { } 105 74 179
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 74 $filter = $factory;
157 }
158 }
159 else {
160 2 $error = "invalid FILTER entry for '$name' (not a CODE ref)";
161 }
162  
163 181 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 9 172 181
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 172 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 14 ▶sub store {
Subroutine store: called CC=2 SCAR=7.0
Pod undocumented
183 14 my ($self, $name, $filter) = @_;
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 268 ▶sub _init {
Subroutine _init: called CC=4 SCAR=13.9
203 268 my ($self, $params) = @_;
204  
205 268 ▶ $self->{ FILTERS } = $params->{ FILTERS } || { };
Condition100%2/2
$params->{'FILTERS'} || {}
l!l
2266
MC/DC100%1/1
$params->{'FILTERS'} || {}
$params->{'FILTERS'}
Truth table100%2/2
A
A= $params->{'FILTERS'}
Aresult
11
00
206 268 ▶ $self->{ TOLERANT } = $params->{ TOLERANT } || 0;
Condition100%2/2
$params->{'TOLERANT'} || 0
l!l
2266
MC/DC100%1/1
$params->{'TOLERANT'} || 0
$params->{'TOLERANT'}
Truth table100%2/2
A
A= $params->{'TOLERANT'}
Aresult
11
00
207 268 ▶ $self->{ DEBUG } = ( $params->{ DEBUG } || 0 )
Condition100%2/2
$params->{'DEBUG'} || 0
l!l
8260
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 268 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 106 our $UNSAFE_SPEC = {
224 RFC2732 => q{A-Za-z0-9\-_.~!*'()},
225 RFC3986 => q{A-Za-z0-9\-_.~},
226 };
227 106 our $UNSAFE_CHARS = $UNSAFE_SPEC->{ RFC3986 };
228 106 our $URI_REGEX;
229 106 our $URL_REGEX;
230 106 our $URI_ESCAPES;
231  
232 1 ▶sub use_rfc2732 {
Subroutine use_rfc2732: called CC=1 SCAR=0.0
Pod documented
233 1 $UNSAFE_CHARS = $UNSAFE_SPEC->{ RFC2732 };
234 1 $URI_REGEX = $URL_REGEX = undef;
235 }
236  
237 1 ▶sub use_rfc3986 {
Subroutine use_rfc3986: called CC=1 SCAR=0.0
Pod documented
238 1 $UNSAFE_CHARS = $UNSAFE_SPEC->{ RFC3986 };
239 1 $URI_REGEX = $URL_REGEX = undef;
240 }
241  
242 2 ▶sub uri_escapes {
Subroutine uri_escapes: called CC=1 SCAR=0.0
Pod undocumented
243 return {
244 2 map { ( chr($_), sprintf("%%%02X", $_) ) } (0..255),
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 18 ▶sub uri_filter {
Subroutine uri_filter: called CC=5 SCAR=16.2
Pod undocumented
257 18 my $text = shift;
258  
259 18 ▶ $URI_REGEX ||= qr/([^$UNSAFE_CHARS])/;
Condition100%2/2
$Template::Filters::URI_REGEX ||= qr/([^$Template::Filters::UNSAFE_CHARS])/l
l!l
144
MC/DC100%1/1
$Template::Filters::URI_REGEX ||= qr/([^$Template::Filters::UNSAFE_CHARS])/l
$Template::Filters::URI_REGEX
Truth table100%2/2
A
A= $Template::Filters::URI_REGEX
Aresult
11
00
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 4 ▶sub url_filter {
Subroutine url_filter: called CC=5 SCAR=17.8
Pod undocumented
283 4 my $text = shift;
284  
285 4 ▶ $URL_REGEX ||= qr/([^;\/?:@&=+\$,$UNSAFE_CHARS])/;
Condition100%2/2
$Template::Filters::URL_REGEX ||= qr"([^;/?:\@&=+\$,$Template::Filters::UNSAFE_CHARS])"l
l!l
13
MC/DC100%1/1
$Template::Filters::URL_REGEX ||= qr"([^;/?:\@&=+\$,$Template::Filters::UNSAFE_CHARS])"l
$Template::Filters::URL_REGEX
Truth table100%2/2
A
A= $Template::Filters::URL_REGEX
Aresult
11
00
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 17 ▶sub html_filter {
Subroutine html_filter: called CC=2 SCAR=6.9
Pod undocumented
306 17 my $text = shift;
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 2 ▶sub xml_filter {
Subroutine xml_filter: called CC=2 SCAR=6.9
Pod undocumented
326 2 my $text = shift;
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 1 ▶sub html_paragraph {
Subroutine html_paragraph: called CC=1 SCAR=0.0
Pod undocumented
346 1 my $text = shift;
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 2 ▶sub html_para_break {
Subroutine html_para_break: called CC=1 SCAR=0.0
Pod undocumented
361 2 my $text = shift;
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 1 ▶sub html_line_break {
Subroutine html_line_break: called CC=1 SCAR=0.0
Pod undocumented
373 1 my $text = shift;
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 1 ▶sub use_html_entities {
Subroutine use_html_entities: called CC=1 SCAR=0.0
Pod documented
392 1 require HTML::Entities;
393 1 return ($AVAILABLE->{ HTML_ENTITY } = \&HTML::Entities::encode_entities);
394 }
395  
396 1 ▶sub use_apache_util {
Subroutine use_apache_util: called CC=1 SCAR=2.6
Pod documented
397 1 require Apache::Util;
398 0 Apache::Util::escape_html(''); # TODO: explain this
399 0 return ($AVAILABLE->{ HTML_ENTITY } = \&Apache::Util::escape_html);
400 }
401  
402 1 ▶sub html_entity_filter_factory {
Subroutine html_entity_filter_factory: called CC=5 SCAR=18.4
Pod undocumented
403 1 my $context = shift;
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 16 ▶sub indent_filter_factory {
Subroutine indent_filter_factory: called CC=3 SCAR=11.0
Pod undocumented
428 16 my ($context, $pad) = @_;
429 16 ▶ $pad //= 4;
Condition100%2/2
$pad //= 4
l!l
124
MC/DC100%1/1
$pad //= 4
$pad
Truth table100%2/2
A
A= $pad
Aresult
11
00
430 16 ▶ $pad = ' ' x $pad if $pad =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $pad =~ /^\d+$/l 13 3 16
431  
432 16 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
433 16 ▶ my $text = shift // '';
Condition50%1/2
shift() // ""
l!l
160
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
434 16 $text =~ s/^/$pad/mg;
435 16 return $text;
436 }
437 16 }
438  
439 #------------------------------------------------------------------------
440 # format_filter_factory() [% FILTER format(format) %]
441 #
442 # Create a filter to format text according to a printf()-like format
443 # string.
444 #------------------------------------------------------------------------
445  
446 11 ▶sub format_filter_factory {
Subroutine format_filter_factory: called CC=2 SCAR=7.0
Pod undocumented
447 11 my ($context, $format) = @_;
448 11 ▶ $format //= '%s';
Condition100%2/2
$format //= "%s"
l!l
83
MC/DC100%1/1
$format //= "%s"
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
449  
450 19 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
451 19 ▶ my $text = shift // '';
Condition50%1/2
shift() // ""
l!l
190
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
452 19 return join("\n", map{ sprintf($format, $_) } split(/\n/, $text));
453 }
454 11 }
455  
456  
457 #------------------------------------------------------------------------
458 # repeat_filter_factory($n) [% FILTER repeat(n) %]
459 #
460 # Create a filter to repeat text n times.
461 #------------------------------------------------------------------------
462  
463 3 ▶sub repeat_filter_factory {
Subroutine repeat_filter_factory: called CC=3 SCAR=12.0
Pod undocumented
464 3 my ($context, $iter) = @_;
465 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
466  
467 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
468 3 ▶ my $text = shift // '';
Condition50%1/2
shift() // ""
l!l
30
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
469 3 return join('\n', $text) x $iter;
470 }
471 3 }
472  
473  
474 #------------------------------------------------------------------------
475 # replace_filter_factory($s, $r) [% FILTER replace(search, replace) %]
476 #
477 # Create a filter to replace 'search' text with 'replace'
478 #------------------------------------------------------------------------
479  
480 12 ▶sub replace_filter_factory {
Subroutine replace_filter_factory: called CC=3 SCAR=11.6
Pod undocumented
481 12 my ($context, $search, $replace) = @_;
482 12 ▶ $search //= '';
Condition50%1/2
$search //= ""
l!l
120
MC/DC0%0/1
$search //= ""
$search
Truth table50%1/2
A
A= $search
Aresult
11
00
483 12 ▶ $replace //= '';
Condition50%1/2
$replace //= ""
l!l
120
MC/DC0%0/1
$replace //= ""
$replace
Truth table50%1/2
A
A= $replace
Aresult
11
00
484  
485 13 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
486 13 ▶ my $text = shift // '';
Condition50%1/2
shift() // ""
l!l
130
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
487 13 my $prev_end = -1;
488 13 $text =~ s{$search}{
489 47 my $s = $-[0];
490 47 my $e = $+[0];
491 47 ▶ if ($s == $e && $s == $prev_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == $e and $s == $prev_end) { } 0 47 47
Condition33%1/3
$s == $e and $s == $prev_end
!ll&&!rl&&r
4700
MC/DC0%0/2
$s == $e and $s == $prev_end
$s == $e $s == $prev_end
Truth table33%1/3
A and B
A= $s == $eB= $s == $prev_end
ABresult
0X0
100
111
492 0 '';
493 } else {
494 47 $prev_end = $e;
495 47 $replace;
496 }
497 }eg;
498 13 return $text;
499 }
500 12 }
501  
502  
503 #------------------------------------------------------------------------
504 # remove_filter_factory($text) [% FILTER remove(text) %]
505 #
506 # Create a filter to remove 'search' string from the input text.
507 #------------------------------------------------------------------------
508  
509 6 ▶sub remove_filter_factory {
Subroutine remove_filter_factory: called CC=1 SCAR=0.0
Pod undocumented
510 6 my ($context, $search) = @_;
511  
512 6 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
513 6 ▶ my $text = shift // '';
Condition50%1/2
shift() // ""
l!l
60
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
514 6 $text =~ s/$search//g;
515 6 return $text;
516 }
517 6 }
518  
519  
520 #------------------------------------------------------------------------
521 # truncate_filter_factory($n) [% FILTER truncate(n) %]
522 #
523 # Create a filter to truncate text after n characters.
524 #------------------------------------------------------------------------
525  
526 28 ▶sub truncate_filter_factory {
Subroutine truncate_filter_factory: called CC=4 SCAR=13.9
Pod undocumented
527 28 my ($context, $len, $char) = @_;
528 28 ▶ $len //= $TRUNCATE_LENGTH;
Condition66%2/3
$len //= $Template::Filters::TRUNCATE_LENGTH
l!l&&r!l&&!r
2710
MC/DC0%0/2
$len //= $Template::Filters::TRUNCATE_LENGTH
$len $Template::Filters::TRUNCATE_LENGTH
Truth table66%2/3
A //= B
A= $lenB= $Template::Filters::TRUNCATE_LENGTH
ABresult
1X1
011
000
529 28 ▶ $char //= $TRUNCATE_ADDON;
Condition66%2/3
$char //= $Template::Filters::TRUNCATE_ADDON
l!l&&r!l&&!r
12160
MC/DC0%0/2
$char //= $Template::Filters::TRUNCATE_ADDON
$char $Template::Filters::TRUNCATE_ADDON
Truth table66%2/3
A //= B
A= $charB= $Template::Filters::TRUNCATE_ADDON
ABresult
1X1
011
000
530  
531 # Calculate visual length of the addon, treating HTML character entity
532 # references (e.g. &hellip; &#8230; &#x2026;) as single characters
533 28 my $lchar = _visual_length($char);
534 28 ▶ if ($len < $lchar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $lchar) 2 26 28
535 2 $char = substr($char, 0, $len);
536 2 $lchar = $len;
537 }
538  
539 28 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
540 28 my $text = shift;
541 28 ▶ return $text if _visual_length($text) <= $len;
Branch: 1 branch
BranchTrueFalseTotal
if _visual_length($text) <= $len 7 21 28
542 21 return _truncate_visual($text, $len - $lchar) . $char;
543 }
544 28 }
545  
546  
547 #------------------------------------------------------------------------
548 # _visual_length($str)
549 #
550 # Returns the "visual" length of a string, counting each HTML character
551 # entity reference (&name; &#digits; &#xhex;) as a single character.
552 #------------------------------------------------------------------------
553  
554 56 ▶sub _visual_length {
Subroutine _visual_length: called CC=1 SCAR=0.0
555 56 my $str = shift;
556 56 (my $copy = $str) =~ s/&(?:[a-zA-Z][a-zA-Z0-9]*|#[0-9]+|#x[0-9a-fA-F]+);/_/g;
557 56 return length $copy;
558 }
559  
560  
561 #------------------------------------------------------------------------
562 # _truncate_visual($str, $maxlen)
563 #
564 # Truncates $str to at most $maxlen visual characters, treating HTML
565 # character entity references as single characters and never splitting
566 # one in the middle.
567 #------------------------------------------------------------------------
568  
569 21 ▶sub _truncate_visual {
Subroutine _truncate_visual: called CC=3 SCAR=11.0
570 21 my ($str, $maxlen) = @_;
571 21 my $result = '';
572 21 my $vlen = 0;
573  
574 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
575 246 ▶ last if $vlen >= $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
if $vlen >= $maxlen 21 225 246
576 225 $result .= $1;
577 225 $vlen++;
578 }
579 21 return $result;
580 }
581  
582  
583 #------------------------------------------------------------------------
584 # eval_filter_factory [% FILTER eval %]
585 #
586 # Create a filter to evaluate template text.
587 #------------------------------------------------------------------------
588  
589 3 ▶sub eval_filter_factory {
Subroutine eval_filter_factory: called CC=1 SCAR=0.0
Pod undocumented
590 3 my $context = shift;
591  
592 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
593 3 my $text = shift;
594 3 $context->process(\$text);
595 }
596 3 }
597  
598  
599 #------------------------------------------------------------------------
600 # perl_filter_factory [% FILTER perl %]
601 #
602 # Create a filter to process Perl text iff the context EVAL_PERL flag
603 # is set.
604 #------------------------------------------------------------------------
605  
606 4 ▶sub perl_filter_factory {
Subroutine perl_filter_factory: called CC=2 SCAR=6.9
Pod undocumented
607 4 my $context = shift;
608 4 my $stash = $context->stash;
609  
610 4 ▶ return (undef, Template::Exception->new('perl', 'EVAL_PERL is not set'))
Branch: 1 branch
BranchTrueFalseTotal
unless $context->eval_perl 1 3 4
611 unless $context->eval_perl();
612  
613 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
614 3 my $text = shift;
615 3 local($Template::Perl::context) = $context;
616 3 local($Template::Perl::stash) = $stash;
617 3 my $out = eval <<EOF;
618 package Template::Perl;
619 \$stash = \$context->stash();
620 $text
621 EOF
622 3 ▶ $context->throw($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
623 3 return $out;
624 }
625 3 }
626  
627  
628 #------------------------------------------------------------------------
629 # redirect_filter_factory($context, $file) [% FILTER redirect(file) %]
630 #
631 # Create a filter to redirect the block text to a file.
632 #------------------------------------------------------------------------
633  
634 2 ▶sub redirect_filter_factory {
Subroutine redirect_filter_factory: called CC=4 SCAR=14.1
Pod undocumented
635 2 my ($context, $file, $options) = @_;
636 2 my $outpath = $context->config->{ OUTPUT_PATH };
637  
638 2 ▶ return (undef, Template::Exception->new('redirect',
Branch: 1 branch
BranchTrueFalseTotal
unless $outpath 1 1 2
639 'OUTPUT_PATH is not set'))
640 unless $outpath;
641  
642 1 ▶ $context->throw('redirect', "relative filenames are not supported: $file")
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ m[(^|/)\.\./]l 0 1 1
643 if $file =~ m{(^|/)\.\./};
644  
645 1 ▶ $options = { binmode => $options } unless ref $options;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options 1 0 1
646  
647 1 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
648 1 my $text = shift;
649 my $outpath = $context->config->{ OUTPUT_PATH }
650 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
651 1 $outpath .= "/$file";
652 1 my $error = Template::_output($outpath, \$text, $options);
653 1 ▶ die Template::Exception->new('redirect', $error)
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 1 1
654 if $error;
655 1 return '';
656 }
657 1 }
658  
659  
660 #------------------------------------------------------------------------
661 # stdout_filter_factory($context, $binmode) [% FILTER stdout(binmode) %]
662 #
663 # Create a filter to print a block to stdout, with an optional binmode.
664 #------------------------------------------------------------------------
665  
666 0 ▶sub stdout_filter_factory {
Subroutine stdout_filter_factory: not called CC=2 SCAR=17.9
Pod undocumented
667 0 my ($context, $options) = @_;
668  
669 0 ▶ $options = { binmode => $options } unless ref $options;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options 0 0 0
670  
671 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
672 0 my $text = shift;
673 0 ▶ binmode(STDOUT) if $options->{ binmode };
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"binmode"} 0 0 0
674 0 print STDOUT $text;
675 0 return '';
676 }
677 0 }
678  
679  
680 106 1;
681  
682 __END__
« lib/Template/Document.pm ↑ Summary lib/Template/Directive.pm »