| 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 are of two forms: | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =over 8 | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | =item formatters | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | These plugins operate upon the text contained in L objects | 
| 20 |  |  |  |  |  |  | and transform the input into HTML. | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =item variable expanders | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | These plugins, also operating on L 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 | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =cut | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | Copyright (C) 2012-2015 Steve Kemp . | 
| 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 |  | 273466 | use strict; | 
|  | 11 |  |  |  |  | 50 |  | 
|  | 11 |  |  |  |  | 269 |  | 
| 69 | 11 |  |  | 11 |  | 48 | use warnings; | 
|  | 11 |  |  |  |  | 16 |  | 
|  | 11 |  |  |  |  | 564 |  | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | package Templer::Plugin::Factory; | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | my $singleton; | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | # | 
| 79 |  |  |  |  |  |  | #  Look for plugins beneath the `Templer::Plugin` namespace. | 
| 80 |  |  |  |  |  |  | # | 
| 81 |  |  |  |  |  |  | use Module::Pluggable | 
| 82 | 11 |  |  |  |  | 85 | search_path => ['Templer::Plugin'], | 
| 83 | 11 |  |  | 11 |  | 4437 | require     => 1; | 
|  | 11 |  |  |  |  | 101701 |  | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | # | 
| 87 |  |  |  |  |  |  | # Invoking `plugins` ensures that we've actually loaded our plugins | 
| 88 |  |  |  |  |  |  | # by the time this module is loaded. | 
| 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 | 190 |  |  | 190 | 1 | 5904 | my $class = shift; | 
| 107 | 190 |  | 100 |  |  | 927 | $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 | 5 | my ( $self, $site ) = (@_); | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 2 |  |  |  |  | 3 | foreach my $plugin ( @{ $self->{ 'plugins' } } ) | 
|  | 2 |  |  |  |  | 6 |  | 
| 141 |  |  |  |  |  |  | { | 
| 142 | 20 | 100 |  |  |  | 92 | if ( UNIVERSAL::can( $plugin, "init" ) ) | 
| 143 |  |  |  |  |  |  | { | 
| 144 | 2 |  |  |  |  | 6 | $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 and L are | 
| 158 |  |  |  |  |  |  | example classes. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =cut | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | sub register_formatter | 
| 163 |  |  |  |  |  |  | { | 
| 164 | 45 |  |  | 45 | 1 | 543 | my ( $self, $name, $obj ) = (@_); | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 45 | 50 |  |  |  | 116 | die "No name" unless ($name); | 
| 167 | 45 |  |  |  |  | 82 | $name = lc($name); | 
| 168 | 45 |  |  |  |  | 388 | $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 and L are | 
| 181 |  |  |  |  |  |  | example classes. | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =cut | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | sub register_filter | 
| 186 |  |  |  |  |  |  | { | 
| 187 | 22 |  |  | 22 | 1 | 55 | my ( $self, $name, $obj ) = (@_); | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 22 | 50 |  |  |  | 49 | die "No name" unless ($name); | 
| 190 | 22 |  |  |  |  | 39 | $name = lc($name); | 
| 191 | 22 |  |  |  |  | 181 | $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, L, and | 
| 203 |  |  |  |  |  |  | L 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 | 111 |  |  | 111 | 1 | 249 | my ( $self, $obj ) = (@_); | 
| 213 |  |  |  |  |  |  |  | 
| 214 | 111 |  |  |  |  | 126 | push( @{ $self->{ 'plugins' } }, $obj->new() ); | 
|  | 111 |  |  |  |  | 336 |  | 
| 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 | 690 | my ( $self, $site, $page, $data ) = (@_); | 
| 228 |  |  |  |  |  |  |  | 
| 229 | 9 |  |  |  |  | 14 | my $out; | 
| 230 |  |  |  |  |  |  |  | 
| 231 | 9 |  |  |  |  | 14 | foreach my $plugin ( @{ $self->{ 'plugins' } } ) | 
|  | 9 |  |  |  |  | 35 |  | 
| 232 |  |  |  |  |  |  | { | 
| 233 | 91 | 100 |  |  |  | 409 | if ( UNIVERSAL::can( $plugin, "expand_variables" ) ) | 
| 234 |  |  |  |  |  |  | { | 
| 235 | 82 |  |  |  |  | 331 | my %in = %$data; | 
| 236 | 82 |  |  |  |  | 436 | $out = $plugin->expand_variables( $site, $page, \%in ); | 
| 237 | 82 |  |  |  |  | 327 | $data = \%$out; | 
| 238 |  |  |  |  |  |  | } | 
| 239 |  |  |  |  |  |  | } | 
| 240 | 9 |  |  |  |  | 51 | return ($data); | 
| 241 |  |  |  |  |  |  | } | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | =head2 cleanup | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | For each loaded plugin invoke the "cleanup" method, if it exists. | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | This can be useful if you wish a plugin to generate a site-map, or similar. | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | =cut | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | sub cleanup | 
| 254 |  |  |  |  |  |  | { | 
| 255 | 2 |  |  | 2 | 1 | 6 | my ($self) = (@_); | 
| 256 |  |  |  |  |  |  |  | 
| 257 | 2 |  |  |  |  | 5 | foreach my $plugin ( @{ $self->{ 'plugins' } } ) | 
|  | 2 |  |  |  |  | 4 |  | 
| 258 |  |  |  |  |  |  | { | 
| 259 | 20 | 100 |  |  |  | 66 | if ( UNIVERSAL::can( $plugin, "cleanup" ) ) | 
| 260 |  |  |  |  |  |  | { | 
| 261 | 2 |  |  |  |  | 7 | $plugin->cleanup(); | 
| 262 |  |  |  |  |  |  | } | 
| 263 |  |  |  |  |  |  | } | 
| 264 |  |  |  |  |  |  | } | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  |  | 
| 268 |  |  |  |  |  |  | =head2 formatter | 
| 269 |  |  |  |  |  |  |  | 
| 270 |  |  |  |  |  |  | Return a new instance of the formatter class with the given name. | 
| 271 |  |  |  |  |  |  |  | 
| 272 |  |  |  |  |  |  | C is returned if no such plugin is registered. | 
| 273 |  |  |  |  |  |  |  | 
| 274 |  |  |  |  |  |  | =cut | 
| 275 |  |  |  |  |  |  |  | 
| 276 |  |  |  |  |  |  | sub formatter | 
| 277 |  |  |  |  |  |  | { | 
| 278 | 29 |  |  | 29 | 1 | 22108 | my ( $self, $name ) = (@_); | 
| 279 |  |  |  |  |  |  |  | 
| 280 | 29 | 100 |  |  |  | 67 | die "No name" unless ($name); | 
| 281 | 27 |  |  |  |  | 37 | $name = lc($name); | 
| 282 |  |  |  |  |  |  |  | 
| 283 |  |  |  |  |  |  | # | 
| 284 |  |  |  |  |  |  | #  Lookup the formatter by name, if it is found | 
| 285 |  |  |  |  |  |  | # then instantiate the clsee. | 
| 286 |  |  |  |  |  |  | # | 
| 287 | 27 |  | 100 |  |  | 67 | my $obj = $self->{ 'formatters' }{ $name } || undef; | 
| 288 | 27 | 100 |  |  |  | 88 | $obj = $obj->new() if ($obj); | 
| 289 |  |  |  |  |  |  |  | 
| 290 | 27 |  |  |  |  | 92 | return ($obj); | 
| 291 |  |  |  |  |  |  | } | 
| 292 |  |  |  |  |  |  |  | 
| 293 |  |  |  |  |  |  |  | 
| 294 |  |  |  |  |  |  |  | 
| 295 |  |  |  |  |  |  | =head2 formatters | 
| 296 |  |  |  |  |  |  |  | 
| 297 |  |  |  |  |  |  | Return the names of each registered formatter-plugin, this is only | 
| 298 |  |  |  |  |  |  | used by the test-suite. | 
| 299 |  |  |  |  |  |  |  | 
| 300 |  |  |  |  |  |  | =cut | 
| 301 |  |  |  |  |  |  |  | 
| 302 |  |  |  |  |  |  | sub formatters | 
| 303 |  |  |  |  |  |  | { | 
| 304 | 3 |  |  | 3 | 1 | 1795 | my ($self) = (@_); | 
| 305 |  |  |  |  |  |  |  | 
| 306 | 3 |  |  |  |  | 4 | keys( %{ $self->{ 'formatters' } } ); | 
|  | 3 |  |  |  |  | 15 |  | 
| 307 |  |  |  |  |  |  | } | 
| 308 |  |  |  |  |  |  |  | 
| 309 |  |  |  |  |  |  |  | 
| 310 |  |  |  |  |  |  |  | 
| 311 |  |  |  |  |  |  | =head2 filter | 
| 312 |  |  |  |  |  |  |  | 
| 313 |  |  |  |  |  |  | Return a new instance of the filter class with the given name. | 
| 314 |  |  |  |  |  |  |  | 
| 315 |  |  |  |  |  |  | C is returned if no such plugin is registered. | 
| 316 |  |  |  |  |  |  |  | 
| 317 |  |  |  |  |  |  | =cut | 
| 318 |  |  |  |  |  |  |  | 
| 319 |  |  |  |  |  |  | sub filter | 
| 320 |  |  |  |  |  |  | { | 
| 321 | 15 |  |  | 15 | 1 | 2866 | my ( $self, $name ) = (@_); | 
| 322 |  |  |  |  |  |  |  | 
| 323 | 15 | 100 |  |  |  | 42 | die "No name" unless ($name); | 
| 324 | 13 |  |  |  |  | 19 | $name = lc($name); | 
| 325 |  |  |  |  |  |  |  | 
| 326 |  |  |  |  |  |  | # | 
| 327 |  |  |  |  |  |  | #  Lookup the filter by name, if it is found | 
| 328 |  |  |  |  |  |  | # then instantiate the class. | 
| 329 |  |  |  |  |  |  | # | 
| 330 | 13 |  | 100 |  |  | 43 | my $obj = $self->{ 'filters' }{ $name } || undef; | 
| 331 | 13 | 100 |  |  |  | 32 | $obj = $obj->new() if ($obj); | 
| 332 |  |  |  |  |  |  |  | 
| 333 | 13 |  |  |  |  | 43 | return ($obj); | 
| 334 |  |  |  |  |  |  | } | 
| 335 |  |  |  |  |  |  |  | 
| 336 |  |  |  |  |  |  |  | 
| 337 |  |  |  |  |  |  |  | 
| 338 |  |  |  |  |  |  | =head2 filters | 
| 339 |  |  |  |  |  |  |  | 
| 340 |  |  |  |  |  |  | Return the names of each registered filter-plugin, this is only | 
| 341 |  |  |  |  |  |  | used by the test-suite. | 
| 342 |  |  |  |  |  |  |  | 
| 343 |  |  |  |  |  |  | =cut | 
| 344 |  |  |  |  |  |  |  | 
| 345 |  |  |  |  |  |  | sub filters | 
| 346 |  |  |  |  |  |  | { | 
| 347 | 1 |  |  | 1 | 1 | 611 | my ($self) = (@_); | 
| 348 |  |  |  |  |  |  |  | 
| 349 | 1 |  |  |  |  | 3 | keys( %{ $self->{ 'filters' } } ); | 
|  | 1 |  |  |  |  | 6 |  | 
| 350 |  |  |  |  |  |  | } | 
| 351 |  |  |  |  |  |  |  | 
| 352 |  |  |  |  |  |  |  | 
| 353 |  |  |  |  |  |  |  | 
| 354 |  |  |  |  |  |  | 1; |