| 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 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 69 | 11 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | |||||||||||||
| 71 | |||||||||||||
| 72 | package Templer::Plugin::Factory; | ||||||||||||
| 73 | |||||||||||||
| 74 | |||||||||||||
| 75 | 11 | my $singleton; | |||||||||||
| 76 | |||||||||||||
| 77 | |||||||||||||
| 78 | # | ||||||||||||
| 79 | # Look for plugins beneath the `Templer::Plugin` namespace. | ||||||||||||
| 80 | # | ||||||||||||
| 81 | use Module::Pluggable | ||||||||||||
| 82 | 11 | ▶ | search_path => ['Templer::Plugin'], | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 83 | 11 | require => 1; | |||||||||||
| 84 | |||||||||||||
| 85 | |||||||||||||
| 86 | # | ||||||||||||
| 87 | # Invoking `plugins` ensures that we've actually loaded our plugins | ||||||||||||
| 88 | # by the time this module is loaded. | ||||||||||||
| 89 | # | ||||||||||||
| 90 | 11 | 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 | 190 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 106 | 190 | my $class = shift; | |||||||||||
| 107 | 190 | ▶ | $singleton ||= bless {}, $class; | ||||||||||
|
Condition100%2/2
$singleton ||= bless({}, $class)
MC/DC100%1/1
$singleton ||= bless({}, $class)
$singleton
Truth table100%2/2
A
A= $singleton
| |||||||||||||
| 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 | 0 | ▶ | { | ||||||||||
|
Subroutine
load_plugins: not called CC=3 SCAR=17.3
Pod
documented
| |||||||||||||
| 120 | 0 | my ( $self, $directory ) = (@_); | |||||||||||
| 121 | 0 | ▶ | return unless ( -d $directory ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | |||||||||||||
| 123 | 0 | ▶ | foreach my $file ( sort( glob( $directory . "/*.pm" ) ) ) | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||
| 124 | { | ||||||||||||
| 125 | 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 | 2 | ▶ | { | ||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 138 | 2 | my ( $self, $site ) = (@_); | |||||||||||
| 139 | |||||||||||||
| 140 | 2 | foreach my $plugin ( @{ $self->{ 'plugins' } } ) | |||||||||||
| 141 | { | ||||||||||||
| 142 | 20 | ▶ | if ( UNIVERSAL::can( $plugin, "init" ) ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | { | ||||||||||||
| 144 | 2 | $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 | 45 | ▶ | { | ||||||||||
|
Subroutine
register_formatter: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 164 | 45 | my ( $self, $name, $obj ) = (@_); | |||||||||||
| 165 | |||||||||||||
| 166 | 45 | ▶ | die "No name" unless ($name); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | 45 | $name = lc($name); | |||||||||||
| 168 | 45 | $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 | 22 | ▶ | { | ||||||||||
|
Subroutine
register_filter: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 187 | 22 | my ( $self, $name, $obj ) = (@_); | |||||||||||
| 188 | |||||||||||||
| 189 | 22 | ▶ | die "No name" unless ($name); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 190 | 22 | $name = lc($name); | |||||||||||
| 191 | 22 | $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 | 111 | ▶ | { | ||||||||||
|
Subroutine
register_plugin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 212 | 111 | my ( $self, $obj ) = (@_); | |||||||||||
| 213 | |||||||||||||
| 214 | 111 | push( @{ $self->{ 'plugins' } }, $obj->new() ); | |||||||||||
| 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 | 9 | ▶ | { | ||||||||||
|
Subroutine
expand_variables: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 227 | 9 | my ( $self, $site, $page, $data ) = (@_); | |||||||||||
| 228 | |||||||||||||
| 229 | 9 | my $out; | |||||||||||
| 230 | |||||||||||||
| 231 | 9 | foreach my $plugin ( @{ $self->{ 'plugins' } } ) | |||||||||||
| 232 | { | ||||||||||||
| 233 | 91 | ▶ | if ( UNIVERSAL::can( $plugin, "expand_variables" ) ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 234 | { | ||||||||||||
| 235 | 82 | my %in = %$data; | |||||||||||
| 236 | 82 | $out = $plugin->expand_variables( $site, $page, \%in ); | |||||||||||
| 237 | 82 | $data = \%$out; | |||||||||||
| 238 | } | ||||||||||||
| 239 | } | ||||||||||||
| 240 | 9 | 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 | 2 | ▶ | { | ||||||||||
|
Subroutine
cleanup: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 255 | 2 | my ($self) = (@_); | |||||||||||
| 256 | |||||||||||||
| 257 | 2 | foreach my $plugin ( @{ $self->{ 'plugins' } } ) | |||||||||||
| 258 | { | ||||||||||||
| 259 | 20 | ▶ | if ( UNIVERSAL::can( $plugin, "cleanup" ) ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 260 | { | ||||||||||||
| 261 | 2 | $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<undef> is returned if no such plugin is registered. | ||||||||||||
| 273 | |||||||||||||
| 274 | =cut | ||||||||||||
| 275 | |||||||||||||
| 276 | sub formatter | ||||||||||||
| 277 | 29 | ▶ | { | ||||||||||
|
Subroutine
formatter: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 278 | 29 | my ( $self, $name ) = (@_); | |||||||||||
| 279 | |||||||||||||
| 280 | 29 | ▶ | die "No name" unless ($name); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 281 | 27 | $name = lc($name); | |||||||||||
| 282 | |||||||||||||
| 283 | # | ||||||||||||
| 284 | # Lookup the formatter by name, if it is found | ||||||||||||
| 285 | # then instantiate the clsee. | ||||||||||||
| 286 | # | ||||||||||||
| 287 | 27 | ▶ | my $obj = $self->{ 'formatters' }{ $name } || undef; | ||||||||||
|
Condition100%2/2
$self->{'formatters'}{$name} || undef
MC/DC100%1/1
$self->{'formatters'}{$name} || undef
$self->{'formatters'}{$name}
Truth table100%2/2
A
A= $self->{'formatters'}{$name}
| |||||||||||||
| 288 | 27 | ▶ | $obj = $obj->new() if ($obj); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 289 | |||||||||||||
| 290 | 27 | 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 | 3 | ▶ | { | ||||||||||
|
Subroutine
formatters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 304 | 3 | my ($self) = (@_); | |||||||||||
| 305 | |||||||||||||
| 306 | 3 | keys( %{ $self->{ 'formatters' } } ); | |||||||||||
| 307 | } | ||||||||||||
| 308 | |||||||||||||
| 309 | |||||||||||||
| 310 | |||||||||||||
| 311 | =head2 filter | ||||||||||||
| 312 | |||||||||||||
| 313 | Return a new instance of the filter class with the given name. | ||||||||||||
| 314 | |||||||||||||
| 315 | C<undef> is returned if no such plugin is registered. | ||||||||||||
| 316 | |||||||||||||
| 317 | =cut | ||||||||||||
| 318 | |||||||||||||
| 319 | sub filter | ||||||||||||
| 320 | 15 | ▶ | { | ||||||||||
|
Subroutine
filter: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 321 | 15 | my ( $self, $name ) = (@_); | |||||||||||
| 322 | |||||||||||||
| 323 | 15 | ▶ | die "No name" unless ($name); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 324 | 13 | $name = lc($name); | |||||||||||
| 325 | |||||||||||||
| 326 | # | ||||||||||||
| 327 | # Lookup the filter by name, if it is found | ||||||||||||
| 328 | # then instantiate the class. | ||||||||||||
| 329 | # | ||||||||||||
| 330 | 13 | ▶ | my $obj = $self->{ 'filters' }{ $name } || undef; | ||||||||||
|
Condition100%2/2
$self->{'filters'}{$name} || undef
MC/DC100%1/1
$self->{'filters'}{$name} || undef
$self->{'filters'}{$name}
Truth table100%2/2
A
A= $self->{'filters'}{$name}
| |||||||||||||
| 331 | 13 | ▶ | $obj = $obj->new() if ($obj); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 332 | |||||||||||||
| 333 | 13 | 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 | 1 | ▶ | { | ||||||||||
|
Subroutine
filters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 347 | 1 | my ($self) = (@_); | |||||||||||
| 348 | |||||||||||||
| 349 | 1 | keys( %{ $self->{ 'filters' } } ); | |||||||||||
| 350 | } | ||||||||||||
| 351 | |||||||||||||
| 352 | |||||||||||||
| 353 | |||||||||||||
| 354 | 11 | 1; | |||||||||||