Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / Template::Plugin::Filter.3pm
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "Template::Plugin::Filter 3"
132 .TH Template::Plugin::Filter 3 "2009-07-04" "perl v5.8.7" "User Contributed Perl Documentation"
133 .SH "NAME"
134 Template::Plugin::Filter \- Base class for plugin filters
135 .SH "SYNOPSIS"
136 .IX Header "SYNOPSIS"
137 .Vb 1
138 \&    package MyOrg::Template::Plugin::MyFilter;
139 .Ve
140 .PP
141 .Vb 2
142 \&    use Template::Plugin::Filter;
143 \&    use base qw( Template::Plugin::Filter );
144 .Ve
145 .PP
146 .Vb 2
147 \&    sub filter {
148 \&        my ($self, $text) = @_;
149 .Ve
150 .PP
151 .Vb 1
152 \&        # ...mungify $text...
153 .Ve
154 .PP
155 .Vb 2
156 \&        return $text;
157 \&    }
158 .Ve
159 .PP
160 .Vb 2
161 \&    # now load it...
162 \&    [% USE MyFilter %]
163 .Ve
164 .PP
165 .Vb 4
166 \&    # ...and use the returned object as a filter
167 \&    [% FILTER $MyFilter %]
168 \&      ...
169 \&    [% END %]
170 .Ve
171 .SH "DESCRIPTION"
172 .IX Header "DESCRIPTION"
173 This module implements a base class for plugin filters.  It hides
174 the underlying complexity involved in creating and using filters
175 that get defined and made available by loading a plugin.
176 .PP
177 To use the module, simply create your own plugin module that is 
178 inherited from the \f(CW\*(C`Template::Plugin::Filter\*(C'\fR class.
179 .PP
180 .Vb 1
181 \&    package MyOrg::Template::Plugin::MyFilter;
182 .Ve
183 .PP
184 .Vb 2
185 \&    use Template::Plugin::Filter;
186 \&    use base qw( Template::Plugin::Filter );
187 .Ve
188 .PP
189 Then simply define your \f(CW\*(C`filter()\*(C'\fR method.  When called, you get
190 passed a reference to your plugin object (\f(CW$self\fR) and the text
191 to be filtered.
192 .PP
193 .Vb 2
194 \&    sub filter {
195 \&        my ($self, $text) = @_;
196 .Ve
197 .PP
198 .Vb 1
199 \&        # ...mungify $text...
200 .Ve
201 .PP
202 .Vb 2
203 \&        return $text;
204 \&    }
205 .Ve
206 .PP
207 To use your custom plugin, you have to make sure that the Template
208 Toolkit knows about your plugin namespace.
209 .PP
210 .Vb 3
211 \&    my $tt2 = Template\->new({
212 \&        PLUGIN_BASE => 'MyOrg::Template::Plugin',
213 \&    });
214 .Ve
215 .PP
216 Or for individual plugins you can do it like this:
217 .PP
218 .Vb 5
219 \&    my $tt2 = Template\->new({
220 \&        PLUGINS => {
221 \&            MyFilter => 'MyOrg::Template::Plugin::MyFilter',
222 \&        },
223 \&    });
224 .Ve
225 .PP
226 Then you \f(CW\*(C`USE\*(C'\fR your plugin in the normal way.
227 .PP
228 .Vb 1
229 \&    [% USE MyFilter %]
230 .Ve
231 .PP
232 The object returned is stored in the variable of the same name,
233 \&'\f(CW\*(C`MyFilter\*(C'\fR'.  When you come to use it as a \f(CW\*(C`FILTER\*(C'\fR, you should add
234 a dollar prefix.  This indicates that you want to use the filter 
235 stored in the variable '\f(CW\*(C`MyFilter\*(C'\fR' rather than the filter named 
236 \&'\f(CW\*(C`MyFilter\*(C'\fR', which is an entirely different thing (see later for 
237 information on defining filters by name).
238 .PP
239 .Vb 3
240 \&    [% FILTER $MyFilter %]
241 \&       ...text to be filtered...
242 \&    [% END %]
243 .Ve
244 .PP
245 You can, of course, assign it to a different variable.
246 .PP
247 .Vb 1
248 \&    [% USE blat = MyFilter %]
249 .Ve
250 .PP
251 .Vb 3
252 \&    [% FILTER $blat %]
253 \&       ...text to be filtered...
254 \&    [% END %]
255 .Ve
256 .PP
257 Any configuration parameters passed to the plugin constructor from the
258 \&\f(CW\*(C`USE\*(C'\fR directive are stored internally in the object for inspection by
259 the \f(CW\*(C`filter()\*(C'\fR method (or indeed any other method).  Positional
260 arguments are stored as a reference to a list in the \f(CW\*(C`_ARGS\*(C'\fR item while
261 named configuration parameters are stored as a reference to a hash
262 array in the \f(CW\*(C`_CONFIG\*(C'\fR item.
263 .PP
264 For example, loading a plugin as shown here:
265 .PP
266 .Vb 1
267 \&    [% USE blat = MyFilter 'foo' 'bar' baz = 'blam' %]
268 .Ve
269 .PP
270 would allow the \f(CW\*(C`filter()\*(C'\fR method to do something like this:
271 .PP
272 .Vb 2
273 \&    sub filter {
274 \&        my ($self, $text) = @_;
275 .Ve
276 .PP
277 .Vb 2
278 \&        my $args = $self\->{ _ARGS   };  # [ 'foo', 'bar' ]
279 \&        my $conf = $self\->{ _CONFIG };  # { baz => 'blam' }
280 .Ve
281 .PP
282 .Vb 1
283 \&        # ...munge $text...
284 .Ve
285 .PP
286 .Vb 2
287 \&        return $text;
288 \&    }
289 .Ve
290 .PP
291 By default, plugins derived from this module will create static
292 filters.  A static filter is created once when the plugin gets 
293 loaded via the \f(CW\*(C`USE\*(C'\fR directive and re-used for all subsequent
294 \&\f(CW\*(C`FILTER\*(C'\fR operations.  That means that any argument specified with
295 the \f(CW\*(C`FILTER\*(C'\fR directive are ignored.
296 .PP
297 Dynamic filters, on the other hand, are re-created each time 
298 they are used by a \f(CW\*(C`FILTER\*(C'\fR directive.  This allows them to act
299 on any parameters passed from the \f(CW\*(C`FILTER\*(C'\fR directive and modify
300 their behaviour accordingly.  
301 .PP
302 There are two ways to create a dynamic filter.  The first is to
303 define a \f(CW$DYNAMIC\fR class variable set to a true value.
304 .PP
305 .Vb 3
306 \&    package MyOrg::Template::Plugin::MyFilter;
307 \&    use base 'Template::Plugin::Filter';
308 \&    our $DYNAMIC = 1;
309 .Ve
310 .PP
311 The other way is to set the internal \f(CW\*(C`_DYNAMIC\*(C'\fR value within the \f(CW\*(C`init()\*(C'\fR
312 method which gets called by the \f(CW\*(C`new()\*(C'\fR constructor.
313 .PP
314 .Vb 5
315 \&    sub init {
316 \&        my $self = shift;
317 \&        $self\->{ _DYNAMIC } = 1;
318 \&        return $self;
319 \&    }
320 .Ve
321 .PP
322 When this is set to a true value, the plugin will automatically
323 create a dynamic filter.  The outcome is that the \f(CW\*(C`filter()\*(C'\fR method
324 will now also get passed a reference to an array of postional
325 arguments and a reference to a hash array of named parameters.
326 .PP
327 So, using a plugin filter like this:
328 .PP
329 .Vb 1
330 \&    [% FILTER $blat 'foo' 'bar' baz = 'blam' %]
331 .Ve
332 .PP
333 would allow the \f(CW\*(C`filter()\*(C'\fR method to work like this:
334 .PP
335 .Vb 2
336 \&    sub filter {
337 \&        my ($self, $text, $args, $conf) = @_;
338 .Ve
339 .PP
340 .Vb 3
341 \&        # $args = [ 'foo', 'bar' ]
342 \&        # $conf = { baz => 'blam' }
343 \&    }
344 .Ve
345 .PP
346 In this case can pass parameters to both the \s-1USE\s0 and \s-1FILTER\s0 directives,
347 so your \fIfilter()\fR method should probably take that into account.  
348 .PP
349 .Vb 1
350 \&    [% USE MyFilter 'foo' wiz => 'waz' %]
351 .Ve
352 .PP
353 .Vb 3
354 \&    [% FILTER $MyFilter 'bar' biz => 'baz' %]
355 \&       ...
356 \&    [% END %]
357 .Ve
358 .PP
359 You can use the \f(CW\*(C`merge_args()\*(C'\fR and \f(CW\*(C`merge_config()\*(C'\fR methods to do a quick
360 and easy job of merging the local (e.g. \f(CW\*(C`FILTER\*(C'\fR) parameters with the
361 internal (e.g. \f(CW\*(C`USE\*(C'\fR) values and returning new sets of conglomerated
362 data.
363 .PP
364 .Vb 2
365 \&    sub filter {
366 \&        my ($self, $text, $args, $conf) = @_;
367 .Ve
368 .PP
369 .Vb 2
370 \&        $args = $self\->merge_args($args); 
371 \&        $conf = $self\->merge_config($conf);
372 .Ve
373 .PP
374 .Vb 4
375 \&        # $args = [ 'foo', 'bar' ]      
376 \&        # $conf = { wiz => 'waz', biz => 'baz' }        
377 \&        ...
378 \&    }
379 .Ve
380 .PP
381 You can also have your plugin install itself as a named filter by
382 calling the \f(CW\*(C`install_filter()\*(C'\fR method from the \f(CW\*(C`init()\*(C'\fR method.  You 
383 should provide a name for the filter, something that you might 
384 like to make a configuration option.
385 .PP
386 .Vb 6
387 \&    sub init {
388 \&        my $self = shift;
389 \&        my $name = $self\->{ _CONFIG }\->{ name } || 'myfilter';
390 \&        $self\->install_filter($name);
391 \&        return $self;
392 \&    }
393 .Ve
394 .PP
395 This allows the plugin filter to be used as follows:
396 .PP
397 .Vb 1
398 \&    [% USE MyFilter %]
399 .Ve
400 .PP
401 .Vb 3
402 \&    [% FILTER myfilter %] 
403 \&       ... 
404 \&    [% END %]
405 .Ve
406 .PP
407 or
408 .PP
409 .Vb 1
410 \&    [% USE MyFilter name = 'swipe' %]
411 .Ve
412 .PP
413 .Vb 3
414 \&    [% FILTER swipe %] 
415 \&       ... 
416 \&    [% END %]
417 .Ve
418 .PP
419 Alternately, you can allow a filter name to be specified as the 
420 first positional argument.
421 .PP
422 .Vb 6
423 \&    sub init {
424 \&        my $self = shift;
425 \&        my $name = $self\->{ _ARGS }\->[0] || 'myfilter';
426 \&        $self\->install_filter($name);
427 \&        return $self;
428 \&    }
429 .Ve
430 .PP
431 .Vb 1
432 \&    [% USE MyFilter 'swipe' %]
433 .Ve
434 .PP
435 .Vb 3
436 \&    [% FILTER swipe %]
437 \&       ...
438 \&    [% END %]
439 .Ve
440 .SH "EXAMPLE"
441 .IX Header "EXAMPLE"
442 Here's a complete example of a plugin filter module.
443 .PP
444 .Vb 3
445 \&    package My::Template::Plugin::Change;
446 \&    use Template::Plugin::Filter;
447 \&    use base qw( Template::Plugin::Filter );
448 .Ve
449 .PP
450 .Vb 2
451 \&    sub init {
452 \&        my $self = shift;
453 .Ve
454 .PP
455 .Vb 1
456 \&        $self\->{ _DYNAMIC } = 1;
457 .Ve
458 .PP
459 .Vb 2
460 \&        # first arg can specify filter name
461 \&        $self\->install_filter($self\->{ _ARGS }\->[0] || 'change');
462 .Ve
463 .PP
464 .Vb 2
465 \&        return $self;
466 \&    }
467 .Ve
468 .PP
469 .Vb 2
470 \&    sub filter {
471 \&        my ($self, $text, $args, $config) = @_;
472 .Ve
473 .PP
474 .Vb 2
475 \&        $config = $self\->merge_config($config);
476 \&        my $regex = join('|', keys %$config);
477 .Ve
478 .PP
479 .Vb 1
480 \&        $text =~ s/($regex)/$config\->{ $1 }/ge;
481 .Ve
482 .PP
483 .Vb 2
484 \&        return $text;
485 \&    }
486 .Ve
487 .PP
488 .Vb 1
489 \&    1;
490 .Ve
491 .SH "AUTHOR"
492 .IX Header "AUTHOR"
493 Andy Wardley <abw@wardley.org> <http://wardley.org/>
494 .SH "COPYRIGHT"
495 .IX Header "COPYRIGHT"
496 Copyright (C) 1996\-2007 Andy Wardley.  All Rights Reserved.
497 .PP
498 This module is free software; you can redistribute it and/or
499 modify it under the same terms as Perl itself.
500 .SH "SEE ALSO"
501 .IX Header "SEE ALSO"
502 Template::Plugin, Template::Filters, Template::Manual::Filters