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