| 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 <abw@wardley.org> | ||
| 12 | # | ||
| 13 | # COPYRIGHT | ||
| 14 | # Copyright (C) 2001-2022 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 | use strict; | ||
| 24 | use warnings; | ||
| 25 | use base 'Template::Plugin'; | ||
| 26 | use Scalar::Util 'weaken', 'isweak'; | ||
| 27 | |||
| 28 | |||
| 29 | our $VERSION = '3.105'; | ||
| 30 | our $DYNAMIC = 0 unless defined $DYNAMIC; | ||
| 31 | |||
| 32 | |||
| 33 | sub new { | ||
| 34 | my ($class, $context, @args) = @_; | ||
| 35 | my $config = @args && ref $args[-1] eq 'HASH' ? pop(@args) : { }; | ||
| 36 | |||
| 37 | # look for $DYNAMIC | ||
| 38 | my $dynamic; | ||
| 39 | { | ||
| 40 | no strict 'refs'; | ||
| 41 | $dynamic = ${"$class\::DYNAMIC"}; | ||
| 42 | } | ||
| 43 | $dynamic //= $DYNAMIC; | ||
| 44 | |||
| 45 | my $self = bless { | ||
| 46 | _CONTEXT => $context, | ||
| 47 | _DYNAMIC => $dynamic, | ||
| 48 | _ARGS => \@args, | ||
| 49 | _CONFIG => $config, | ||
| 50 | }, $class; | ||
| 51 | |||
| 52 | return $self->init($config) | ||
| 53 | || $class->error($self->error()); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub init { | ||
| 58 | my ($self, $config) = @_; | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub factory { | ||
| 64 | my $self = shift; | ||
| 65 | my $this = $self; | ||
| 66 | |||
| 67 | # avoid a memory leak | ||
| 68 | weaken( $this->{_CONTEXT} ) if ref $this->{_CONTEXT} | ||
| 69 | && !isweak $this->{_CONTEXT}; | ||
| 70 | |||
| 71 | if ($self->{ _DYNAMIC }) { | ||
| 72 | return [ sub { | ||
| 73 | my ($context, @args) = @_; | ||
| 74 | my $config = ref $args[-1] eq 'HASH' ? pop(@args) : { }; | ||
| 75 | |||
| 76 | return sub { | ||
| 77 | $this->filter(shift, \@args, $config); | ||
| 78 | }; | ||
| 79 | }, 1 ]; | ||
| 80 | } | ||
| 81 | else { | ||
| 82 | return sub { | ||
| 83 | $this->filter(shift); | ||
| 84 | }; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | sub filter { | ||
| 89 | my ($self, $text, $args, $config) = @_; | ||
| 90 | return $text; | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub merge_config { | ||
| 95 | my ($self, $newcfg) = @_; | ||
| 96 | my $owncfg = $self->{ _CONFIG }; | ||
| 97 | return $owncfg unless $newcfg; | ||
| 98 | return { %$owncfg, %$newcfg }; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub merge_args { | ||
| 103 | my ($self, $newargs) = @_; | ||
| 104 | my $ownargs = $self->{ _ARGS }; | ||
| 105 | return $ownargs unless $newargs; | ||
| 106 | return [ @$ownargs, @$newargs ]; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | sub install_filter { | ||
| 111 | my ($self, $name) = @_; | ||
| 112 | $self->{ _CONTEXT }->define_filter( $name => $self->factory ); | ||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ |