line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#============================================================= -*-Perl-*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Template::Plugin::Filter |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION |
6
|
|
|
|
|
|
|
# Template Toolkit module implementing a base class plugin |
7
|
|
|
|
|
|
|
# object which acts like a filter and can be used with the |
8
|
|
|
|
|
|
|
# FILTER directive. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# AUTHOR |
11
|
|
|
|
|
|
|
# Andy Wardley |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# COPYRIGHT |
14
|
|
|
|
|
|
|
# Copyright (C) 2001-2009 Andy Wardley. All Rights Reserved. |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or |
17
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
#============================================================================ |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
package Template::Plugin::Filter; |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
580
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
80
|
|
24
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
49
|
|
25
|
1
|
|
|
1
|
|
4
|
use base 'Template::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
101
|
|
26
|
1
|
|
|
1
|
|
7
|
use Scalar::Util 'weaken'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
163
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $VERSION = 1.38; |
30
|
|
|
|
|
|
|
our $DYNAMIC = 0 unless defined $DYNAMIC; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new { |
34
|
2
|
|
|
2
|
1
|
6
|
my ($class, $context, @args) = @_; |
35
|
2
|
50
|
33
|
|
|
12
|
my $config = @args && ref $args[-1] eq 'HASH' ? pop(@args) : { }; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# look for $DYNAMIC |
38
|
2
|
|
|
|
|
4
|
my $dynamic; |
39
|
|
|
|
|
|
|
{ |
40
|
1
|
|
|
1
|
|
6
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
490
|
|
|
2
|
|
|
|
|
3
|
|
41
|
2
|
|
|
|
|
4
|
$dynamic = ${"$class\::DYNAMIC"}; |
|
2
|
|
|
|
|
11
|
|
42
|
|
|
|
|
|
|
} |
43
|
2
|
50
|
|
|
|
7
|
$dynamic = $DYNAMIC unless defined $dynamic; |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
14
|
my $self = bless { |
46
|
|
|
|
|
|
|
_CONTEXT => $context, |
47
|
|
|
|
|
|
|
_DYNAMIC => $dynamic, |
48
|
|
|
|
|
|
|
_ARGS => \@args, |
49
|
|
|
|
|
|
|
_CONFIG => $config, |
50
|
|
|
|
|
|
|
}, $class; |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
33
|
|
|
14
|
return $self->init($config) |
53
|
|
|
|
|
|
|
|| $class->error($self->error()); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub init { |
58
|
0
|
|
|
0
|
0
|
0
|
my ($self, $config) = @_; |
59
|
0
|
|
|
|
|
0
|
return $self; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub factory { |
64
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
65
|
2
|
|
|
|
|
4
|
my $this = $self; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# This causes problems: https://rt.cpan.org/Ticket/Display.html?id=46691 |
68
|
|
|
|
|
|
|
# If the plugin is loaded twice in different templates (one INCLUDEd into |
69
|
|
|
|
|
|
|
# another) then the filter gets garbage collected when the inner template |
70
|
|
|
|
|
|
|
# ends (at least, I think that's what's happening). So I'm going to take |
71
|
|
|
|
|
|
|
# the "suck it and see" approach, comment it out, and wait for someone to |
72
|
|
|
|
|
|
|
# complain that this module is leaking memory. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# weaken($this); |
75
|
|
|
|
|
|
|
|
76
|
2
|
50
|
|
|
|
7
|
if ($self->{ _DYNAMIC }) { |
77
|
|
|
|
|
|
|
return $self->{ _DYNAMIC_FILTER } ||= [ sub { |
78
|
3
|
|
|
3
|
|
7
|
my ($context, @args) = @_; |
79
|
3
|
50
|
|
|
|
10
|
my $config = ref $args[-1] eq 'HASH' ? pop(@args) : { }; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
return sub { |
82
|
3
|
|
|
|
|
17
|
$this->filter(shift, \@args, $config); |
83
|
3
|
|
|
|
|
16
|
}; |
84
|
2
|
|
50
|
|
|
28
|
}, 1 ]; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
|
|
|
|
|
|
return $self->{ _STATIC_FILTER } ||= sub { |
88
|
0
|
|
|
0
|
|
0
|
$this->filter(shift); |
89
|
0
|
|
0
|
|
|
0
|
}; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub filter { |
94
|
0
|
|
|
0
|
0
|
0
|
my ($self, $text, $args, $config) = @_; |
95
|
0
|
|
|
|
|
0
|
return $text; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub merge_config { |
100
|
0
|
|
|
0
|
0
|
0
|
my ($self, $newcfg) = @_; |
101
|
0
|
|
|
|
|
0
|
my $owncfg = $self->{ _CONFIG }; |
102
|
0
|
0
|
|
|
|
0
|
return $owncfg unless $newcfg; |
103
|
0
|
|
|
|
|
0
|
return { %$owncfg, %$newcfg }; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub merge_args { |
108
|
0
|
|
|
0
|
0
|
0
|
my ($self, $newargs) = @_; |
109
|
0
|
|
|
|
|
0
|
my $ownargs = $self->{ _ARGS }; |
110
|
0
|
0
|
|
|
|
0
|
return $ownargs unless $newargs; |
111
|
0
|
|
|
|
|
0
|
return [ @$ownargs, @$newargs ]; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub install_filter { |
116
|
2
|
|
|
2
|
0
|
29
|
my ($self, $name) = @_; |
117
|
2
|
|
|
|
|
12
|
$self->{ _CONTEXT }->define_filter( $name => $self->factory ); |
118
|
2
|
|
|
|
|
5
|
return $self; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__END__ |