line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
=head1 NAME |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
Templer::Plugin::Factory - A simple plugin class |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=cut |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 DESCRIPTION |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
This class implements a singleton within which plugin classes may |
11
|
|
|
|
|
|
|
be registered and retrieved. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
The plugins used by C<templer> are of two forms: |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=over 8 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=item formatters |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
These plugins operate upon the text contained in L<Templer::Site::Page> objects |
20
|
|
|
|
|
|
|
and transform the input into HTML. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=item variable expanders |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
These plugins, also operating on L<Templer::Site::Page> objects, are allowed |
25
|
|
|
|
|
|
|
the opportunity to modify, update, replace, or delete the various per-page |
26
|
|
|
|
|
|
|
variables. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=back |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Plugins of each type register themselves by calling the appropriate methods |
31
|
|
|
|
|
|
|
in this class. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=cut |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 LICENSE |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
38
|
|
|
|
|
|
|
under the terms of either: |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
a) the GNU General Public License as published by the Free Software |
41
|
|
|
|
|
|
|
Foundation; either version 2, or (at your option) any later version, |
42
|
|
|
|
|
|
|
or |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
b) the Perl "Artistic License". |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 AUTHOR |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Steve Kemp <steve@steve.org.uk> |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
This library is free software. You can modify and or distribute it under |
59
|
|
|
|
|
|
|
the same terms as Perl itself. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 METHODS |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
11
|
|
|
11
|
|
93598
|
use strict; |
|
11
|
|
|
|
|
16
|
|
|
11
|
|
|
|
|
259
|
|
69
|
11
|
|
|
11
|
|
37
|
use warnings; |
|
11
|
|
|
|
|
13
|
|
|
11
|
|
|
|
|
443
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
package Templer::Plugin::Factory; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $singleton; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
use Module::Pluggable |
82
|
11
|
|
|
|
|
57
|
search_path => ['Templer::Plugin'], |
83
|
11
|
|
|
11
|
|
4359
|
require => 1; |
|
11
|
|
|
|
|
85717
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my @plugins = plugins(); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 new |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Constructor. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
This class is a singleton, so this method will either construct |
100
|
|
|
|
|
|
|
an instance of this class or return the global instance. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=cut |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub new |
105
|
|
|
|
|
|
|
{ |
106
|
179
|
|
|
179
|
1
|
4973
|
my $class = shift; |
107
|
179
|
|
100
|
|
|
870
|
$singleton ||= bless {}, $class; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 load_plugins |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This method loads "*.pm" from the given directory. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub load_plugins |
119
|
|
|
|
|
|
|
{ |
120
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $directory ) = (@_); |
121
|
0
|
0
|
|
|
|
0
|
return unless ( -d $directory ); |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
0
|
foreach my $file ( sort( glob( $directory . "/*.pm" ) ) ) |
124
|
|
|
|
|
|
|
{ |
125
|
0
|
|
|
|
|
0
|
require $file; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 init |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
For each loaded plugin invoke the "init" method, if it exists. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub init |
137
|
|
|
|
|
|
|
{ |
138
|
2
|
|
|
2
|
1
|
4
|
my ( $self, $site ) = (@_); |
139
|
|
|
|
|
|
|
|
140
|
2
|
|
|
|
|
3
|
foreach my $plugin ( @{ $self->{ 'plugins' } } ) |
|
2
|
|
|
|
|
7
|
|
141
|
|
|
|
|
|
|
{ |
142
|
18
|
50
|
|
|
|
98
|
if ( UNIVERSAL::can( $plugin, "init" ) ) |
143
|
|
|
|
|
|
|
{ |
144
|
0
|
|
|
|
|
0
|
$plugin->init($site); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 register_formatter |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This method should be called by all formatting plugins to register |
153
|
|
|
|
|
|
|
themselves. The two arguments are the name of the input-format, |
154
|
|
|
|
|
|
|
and the class-name which may be instantiated to process that kind |
155
|
|
|
|
|
|
|
of input. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
L<Templer::Plugin::Textile> and L<Templer::Plugin::Markdown> are |
158
|
|
|
|
|
|
|
example classes. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub register_formatter |
163
|
|
|
|
|
|
|
{ |
164
|
45
|
|
|
45
|
1
|
353
|
my ( $self, $name, $obj ) = (@_); |
165
|
|
|
|
|
|
|
|
166
|
45
|
50
|
|
|
|
80
|
die "No name" unless ($name); |
167
|
45
|
|
|
|
|
58
|
$name = lc($name); |
168
|
45
|
|
|
|
|
303
|
$self->{ 'formatters' }{ $name } = $obj; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 register_filter |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
This method should be called by all template filter plugins to register |
176
|
|
|
|
|
|
|
themselves. The two arguments are the name of the template-filter, |
177
|
|
|
|
|
|
|
and the class-name which may be instantiated to process that kind |
178
|
|
|
|
|
|
|
of input. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
L<Templer::Plugin::Dollar> and L<Templer::Plugin::Strict> are |
181
|
|
|
|
|
|
|
example classes. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub register_filter |
186
|
|
|
|
|
|
|
{ |
187
|
22
|
|
|
22
|
1
|
38
|
my ( $self, $name, $obj ) = (@_); |
188
|
|
|
|
|
|
|
|
189
|
22
|
50
|
|
|
|
54
|
die "No name" unless ($name); |
190
|
22
|
|
|
|
|
35
|
$name = lc($name); |
191
|
22
|
|
|
|
|
161
|
$self->{ 'filters' }{ $name } = $obj; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head2 register_plugin |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
This method should be called by all variable-expanding plugins to register |
199
|
|
|
|
|
|
|
themselves. The expected argument is the class-name which may be instantiated |
200
|
|
|
|
|
|
|
to expand variables. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
L<Templer::Plugin::ShellCommand>, L<Templer::Plugin::FileGlob>, and |
203
|
|
|
|
|
|
|
L<Templer::Plugin::FileContents> are examples of such plugins. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
NOTE: The plugin is instantiated immediately, and kept alive for the duration |
206
|
|
|
|
|
|
|
of a templer-run. |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=cut |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub register_plugin |
211
|
|
|
|
|
|
|
{ |
212
|
100
|
|
|
100
|
1
|
138
|
my ( $self, $obj ) = (@_); |
213
|
|
|
|
|
|
|
|
214
|
100
|
|
|
|
|
82
|
push( @{ $self->{ 'plugins' } }, $obj->new() ); |
|
100
|
|
|
|
|
325
|
|
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head2 expand_variables |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Expand variables via all loaded plugins. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=cut |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub expand_variables |
226
|
|
|
|
|
|
|
{ |
227
|
9
|
|
|
9
|
1
|
518
|
my ( $self, $site, $page, $data ) = (@_); |
228
|
|
|
|
|
|
|
|
229
|
9
|
|
|
|
|
11
|
my $out; |
230
|
|
|
|
|
|
|
|
231
|
9
|
|
|
|
|
13
|
foreach my $plugin ( @{ $self->{ 'plugins' } } ) |
|
9
|
|
|
|
|
26
|
|
232
|
|
|
|
|
|
|
{ |
233
|
82
|
|
|
|
|
269
|
my %in = %$data; |
234
|
82
|
|
|
|
|
285
|
$out = $plugin->expand_variables( $site, $page, \%in ); |
235
|
82
|
|
|
|
|
222
|
$data = \%$out; |
236
|
|
|
|
|
|
|
} |
237
|
9
|
|
|
|
|
31
|
return ($data); |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head2 cleanup |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
For each loaded plugin invoke the "cleanup" method, if it exists. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
This can be useful if you wish a plugin to generate a site-map, or similar. |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=cut |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
sub cleanup |
251
|
|
|
|
|
|
|
{ |
252
|
2
|
|
|
2
|
1
|
4
|
my ($self) = (@_); |
253
|
|
|
|
|
|
|
|
254
|
2
|
|
|
|
|
3
|
foreach my $plugin ( @{ $self->{ 'plugins' } } ) |
|
2
|
|
|
|
|
5
|
|
255
|
|
|
|
|
|
|
{ |
256
|
18
|
50
|
|
|
|
62
|
if ( UNIVERSAL::can( $plugin, "cleanup" ) ) |
257
|
|
|
|
|
|
|
{ |
258
|
0
|
|
|
|
|
0
|
$plugin->cleanup(); |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head2 formatter |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Return a new instance of the formatter class with the given name. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
C<undef> is returned if no such plugin is registered. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=cut |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub formatter |
274
|
|
|
|
|
|
|
{ |
275
|
29
|
|
|
29
|
1
|
17519
|
my ( $self, $name ) = (@_); |
276
|
|
|
|
|
|
|
|
277
|
29
|
100
|
|
|
|
70
|
die "No name" unless ($name); |
278
|
27
|
|
|
|
|
32
|
$name = lc($name); |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
|
284
|
27
|
|
100
|
|
|
60
|
my $obj = $self->{ 'formatters' }{ $name } || undef; |
285
|
27
|
100
|
|
|
|
94
|
$obj = $obj->new() if ($obj); |
286
|
|
|
|
|
|
|
|
287
|
27
|
|
|
|
|
72
|
return ($obj); |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
=head2 formatters |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
Return the names of each registered formatter-plugin, this is only |
295
|
|
|
|
|
|
|
used by the test-suite. |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=cut |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
sub formatters |
300
|
|
|
|
|
|
|
{ |
301
|
3
|
|
|
3
|
1
|
1031
|
my ($self) = (@_); |
302
|
|
|
|
|
|
|
|
303
|
3
|
|
|
|
|
3
|
keys( %{ $self->{ 'formatters' } } ); |
|
3
|
|
|
|
|
17
|
|
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=head2 filter |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
Return a new instance of the filter class with the given name. |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
C<undef> is returned if no such plugin is registered. |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=cut |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
sub filter |
317
|
|
|
|
|
|
|
{ |
318
|
15
|
|
|
15
|
1
|
2977
|
my ( $self, $name ) = (@_); |
319
|
|
|
|
|
|
|
|
320
|
15
|
100
|
|
|
|
40
|
die "No name" unless ($name); |
321
|
13
|
|
|
|
|
14
|
$name = lc($name); |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
|
327
|
13
|
|
100
|
|
|
32
|
my $obj = $self->{ 'filters' }{ $name } || undef; |
328
|
13
|
100
|
|
|
|
37
|
$obj = $obj->new() if ($obj); |
329
|
|
|
|
|
|
|
|
330
|
13
|
|
|
|
|
32
|
return ($obj); |
331
|
|
|
|
|
|
|
} |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=head2 filters |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
Return the names of each registered filter-plugin, this is only |
338
|
|
|
|
|
|
|
used by the test-suite. |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
=cut |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
sub filters |
343
|
|
|
|
|
|
|
{ |
344
|
1
|
|
|
1
|
1
|
569
|
my ($self) = (@_); |
345
|
|
|
|
|
|
|
|
346
|
1
|
|
|
|
|
2
|
keys( %{ $self->{ 'filters' } } ); |
|
1
|
|
|
|
|
5
|
|
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
1; |
352
|
|
|
|
|
|
|
|