| 1 | |||||||||||||||||||||
| 2 | =head1 NAME | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | Templer::Plugin::Perl - A simple inline-perl plugin | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =cut | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 DESCRIPTION | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | This class implements a formatter plugin for C<templer> which allows | ||||||||||||||||||||
| 11 | inline code to be executed as the page is generated. | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | This is carried out via the L<Text::Template> module, and allows input | ||||||||||||||||||||
| 14 | such as this to be executed: | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =for example begin | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | Title: This is my page | ||||||||||||||||||||
| 19 | format: perl | ||||||||||||||||||||
| 20 | name: Steve | ||||||||||||||||||||
| 21 | ---- | ||||||||||||||||||||
| 22 | <p>The sum of 1 + 1 is { 1 + 1 }.</p> | ||||||||||||||||||||
| 23 | <p>My name is { $name }</p> | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =for example end | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head1 LICENSE | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 32 | under the terms of either: | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | a) the GNU General Public License as published by the Free Software | ||||||||||||||||||||
| 35 | Foundation; either version 2, or (at your option) any later version, | ||||||||||||||||||||
| 36 | or | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | b) the Perl "Artistic License". | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =cut | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head1 AUTHOR | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | Steve Kemp <steve@steve.org.uk> | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | Copyright (C) 2012-2015 Steve Kemp <steve@steve.org.uk>. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | This library is free software. You can modify and or distribute it under | ||||||||||||||||||||
| 53 | the same terms as Perl itself. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =head1 METHODS | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 11 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 11 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | package Templer::Plugin::Perl; | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =head2 new | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | Constructor. No arguments are supported/expected. | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | sub new | ||||||||||||||||||||
| 77 | 5 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 78 | 5 | my ( $proto, %supplied ) = (@_); | |||||||||||||||||||
| 79 | 5 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 5 | my $self = {}; | |||||||||||||||||||
| 82 | 5 | bless( $self, $class ); | |||||||||||||||||||
| 83 | 5 | return $self; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =head2 available | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | This plugin is available if we've got the L<Text::Template> module. | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | sub available | ||||||||||||||||||||
| 94 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
available: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 95 | 2 | my $str = "use Text::Template;"; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | ## no critic (Eval) | ||||||||||||||||||||
| 98 | 2 | ▶ | eval($str); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 99 | ## use critic | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 2 | ▶ | return ( $@ ? undef : 1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | =head2 format | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | Format the given text. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | sub format | ||||||||||||||||||||
| 112 | 1 | ▶ | { | ||||||||||||||||||
|
Subroutine
format: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||
| 113 | 1 | my ( $self, $str, $data ) = (@_); | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 1 | ▶ | if ( $self->available() ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | { | ||||||||||||||||||||
| 117 | 1 | my $template = | |||||||||||||||||||
| 118 | Text::Template->new( TYPE => "STRING", | ||||||||||||||||||||
| 119 | SOURCE => $str ); | ||||||||||||||||||||
| 120 | 1 | return ( $template->fill_in( HASH => $data ) ); | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | else | ||||||||||||||||||||
| 123 | { | ||||||||||||||||||||
| 124 | 0 | warn | |||||||||||||||||||
| 125 | "Perl formatting disabled as the Text::Template module isn't present.\n"; | ||||||||||||||||||||
| 126 | 0 | $str; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 11 | Templer::Plugin::Factory->new() | |||||||||||||||||||
| 131 | ->register_formatter( "perl", "Templer::Plugin::Perl" ); | ||||||||||||||||||||