| 1 | package Template::Recall; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use base qw(Template::Recall::Base); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # This version: only single file template or template string | ||||||||||||||||||||||||||||
| 10 | 6 | our $VERSION='0.18'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 5 | my $self = {}; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | my ( %h ) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Set default values | ||||||||||||||||||||||||||||
| 21 | 5 | $self->{'is_file_template'} = 0; | |||||||||||||||||||||||||||
| 22 | 5 | $self->{'template_secpat'} = qr/\[\s*=+\s*\w+\s*=+\s*\]/; # Section pattern | |||||||||||||||||||||||||||
| 23 | 5 | $self->{'secpat_delims'} = [ '\[\s*=+\s*', '\s*=+\s*\]' ]; # Section delimiters | |||||||||||||||||||||||||||
| 24 | 5 | $self->{'val_delims'} = [ '\[\'', '\'\]' ]; | |||||||||||||||||||||||||||
| 25 | 5 | $self->{'trim'} = undef; # undef=off | |||||||||||||||||||||||||||
| 26 | #TODO remove? | ||||||||||||||||||||||||||||
| 27 | 5 | $self->{'stored_secs'} = {}; # Store rendered sections internally | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 5 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # User defines section pattern | ||||||||||||||||||||||||||||
| 33 | 5 | ▶ | $self->{'template_secpat'} = qr/$h{'secpat'}/ if defined( $h{'secpat'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Section: User sets 'no delimiters' | ||||||||||||||||||||||||||||
| 36 | $self->{'secpat_delims'} = undef | ||||||||||||||||||||||||||||
| 37 | 5 | ▶ | if defined($h{'secpat_delims'}) and !ref($h{'secpat_delims'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $h{"secpat_delims"} and not ref $h{"secpat_delims"}
MC/DC50%1/2
defined $h{"secpat_delims"} and not ref $h{"secpat_delims"}
defined $h{"secpat_delims"}
not ref $h{"secpat_delims"}
Truth table66%2/3
A and not(B)
A= defined $h{"secpat_delims"}B= not ref $h{"secpat_delims"}
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Section: User specifies delimiters | ||||||||||||||||||||||||||||
| 40 | 0 | $self->{'secpat_delims'} = [ @{ $h{'secpat_delims'} } ] | |||||||||||||||||||||||||||
| 41 | 5 | ▶ | if defined($h{'secpat_delims'}) and ref($h{'secpat_delims'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $h{"secpat_delims"} and ref $h{"secpat_delims"}
MC/DC0%0/2
defined $h{"secpat_delims"} and ref $h{"secpat_delims"}
defined $h{"secpat_delims"}
ref $h{"secpat_delims"}
Truth table66%2/3
A and B
A= defined $h{"secpat_delims"}B= ref $h{"secpat_delims"}
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # User sets 'no delimiters' | ||||||||||||||||||||||||||||
| 45 | 5 | ▶ | $self->{'val_delims'} = undef if defined($h{'val_delims'}) and !ref($h{'val_delims'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $h{"val_delims"} and not ref $h{"val_delims"}
MC/DC0%0/2
defined $h{"val_delims"} and not ref $h{"val_delims"}
defined $h{"val_delims"}
not ref $h{"val_delims"}
Truth table33%1/3
A and not(B)
A= defined $h{"val_delims"}B= not ref $h{"val_delims"}
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # User specifies delimiters | ||||||||||||||||||||||||||||
| 48 | 0 | $self->{'val_delims'} = [ @{ $h{'val_delims'} } ] | |||||||||||||||||||||||||||
| 49 | 5 | ▶ | if defined($h{'val_delims'}) and ref($h{'val_delims'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $h{"val_delims"} and ref $h{"val_delims"}
MC/DC0%0/2
defined $h{"val_delims"} and ref $h{"val_delims"}
defined $h{"val_delims"}
ref $h{"val_delims"}
Truth table33%1/3
A and B
A= defined $h{"val_delims"}B= ref $h{"val_delims"}
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # User supplied the template from a string | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 5 | ▶ | if ( defined($h{'template_str'}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 2 | $self->init_template($h{'template_str'}); | |||||||||||||||||||||||||||
| 55 | 2 | return $self; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 3 | ▶ | die if ( not defined($h{'template_path'}) or !-e $h{'template_path'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $h{"template_path"} and -e $h{"template_path"}
MC/DC0%0/2
defined $h{"template_path"} and -e $h{"template_path"}
defined $h{"template_path"}
-e $h{"template_path"}
Truth table33%1/3
A and B
A= defined $h{"template_path"}B= -e $h{"template_path"}
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 3 | $self->{'template_path'} = $h{'template_path'}; | |||||||||||||||||||||||||||
| 62 | 3 | $self->init_template_from_file(); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | return $self; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | } # new() | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub render { | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 12 | ▶ | my ( $self, $section, $hash_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 12 | ▶ | return "Error: no section to render: $section\n" if !defined($section); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 12 | ▶ | return if (!exists $self->{'template_secs'}->{$section}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 12 | my $sectemp = $self->{'template_secs'}->{$section}; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | #TODO is it faster if the default delim allows for *no* spaces between ID | ||||||||||||||||||||||||||||
| 84 | 12 | return $self->SUPER::render( $sectemp, $hash_ref, $self->{'val_delims'} ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } # render() | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Load the single file template into array of sections | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub init_template_from_file { | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
init_template_from_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 3 | my $t; | |||||||||||||||||||||||||||
| 100 | 3 | ▶ | open my $fh, $self->{'template_path'} or die "Couldn't open $self->{'template_path'} $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 3 | ▶ | while(<$fh>) { $t .= $_; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 3 | close $fh; | |||||||||||||||||||||||||||
| 103 | 3 | $self->init_template($t); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | } # init_file_template() | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Handle template passed by user as string | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | sub init_template { | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 5 | ▶ | my ($self, $template) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init_template: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 5 | my $sec = [ split( /($self->{'template_secpat'})/, $template ) ]; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 5 | my %h; | |||||||||||||||||||||||||||
| 120 | 5 | my $curr = ''; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # top-down + only one 'body' follows section, why this parse hack works | ||||||||||||||||||||||||||||
| 123 | 5 | for (@$sec) { | |||||||||||||||||||||||||||
| 124 | 23 | ▶ | next if /^$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 18 | ▶ | if (/$self->{'template_secpat'}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 9 | $curr = $_; | |||||||||||||||||||||||||||
| 127 | 9 | $curr =~ s/$self->{'secpat_delims'}[0]|$self->{'secpat_delims'}[1]//g; | |||||||||||||||||||||||||||
| 128 | 9 | $h{$curr} = ''; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||
| 131 | 9 | $h{$curr} = $_; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 5 | $self->{'template_secs'} = \%h; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Set trim flags | ||||||||||||||||||||||||||||
| 145 | sub trim { | ||||||||||||||||||||||||||||
| 146 | 4 | ▶ | my ($self, $flag) = @_; | ||||||||||||||||||||||||||
|
Subroutine
trim: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # trim() with no params defaults to trimming both ends | ||||||||||||||||||||||||||||
| 151 | 4 | ▶ | if (!defined($flag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 1 | $self->{'trim'} = 'both'; | |||||||||||||||||||||||||||
| 153 | 1 | return; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Turn trimming off | ||||||||||||||||||||||||||||
| 159 | 3 | ▶ | if ($flag =~ /^(off|o)$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 1 | $self->{'trim'} = undef; | |||||||||||||||||||||||||||
| 161 | 1 | return; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Make sure we get something valid | ||||||||||||||||||||||||||||
| 166 | 2 | ▶ | if ($flag !~ /^(off|left|right|both|l|r|b|o)$/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | $self->{'trim'} = undef; | |||||||||||||||||||||||||||
| 168 | 0 | return; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 2 | $self->{'trim'} = $flag; | |||||||||||||||||||||||||||
| 173 | 2 | return; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | } # trim() | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 6 | 1; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||