line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Recall;
|
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
75368
|
use 5.008001;
|
|
4
|
|
|
|
|
15
|
|
|
4
|
|
|
|
|
197
|
|
4
|
4
|
|
|
4
|
|
31
|
use strict;
|
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
147
|
|
5
|
4
|
|
|
4
|
|
17
|
use warnings;
|
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
152
|
|
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
23
|
use base qw(Template::Recall::Base);
|
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
1887
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# This version: only single file template or template string
|
10
|
|
|
|
|
|
|
our $VERSION='0.21';
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new {
|
14
|
|
|
|
|
|
|
|
15
|
3
|
|
|
3
|
1
|
70
|
my $class = shift;
|
16
|
3
|
|
|
|
|
4
|
my $self = {};
|
17
|
3
|
|
|
|
|
7
|
bless $self, $class;
|
18
|
|
|
|
|
|
|
|
19
|
3
|
|
|
|
|
11
|
my ( %h ) = @_;
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Default values
|
22
|
3
|
|
|
|
|
27
|
$self->{'secpat'} = qr/\[\s*=+\s*(\w+)\s*=+\s*\]/;
|
23
|
3
|
|
|
|
|
17
|
$self->{'val_delims'} = [ qr/\['/, qr/'\]/ ];
|
24
|
3
|
|
|
|
|
6
|
$self->{'trim'} = undef; # undef=off
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
3
|
50
|
33
|
|
|
15
|
if (exists $h{'secpat'} and ref $h{'secpat'}) {
|
28
|
0
|
|
|
|
|
0
|
$self->{'secpat'} = $h{'secpat'};
|
29
|
|
|
|
|
|
|
}
|
30
|
|
|
|
|
|
|
|
31
|
3
|
50
|
33
|
|
|
21
|
if (exists $h{'val_delims'} and ref $h{'val_delims'} eq 'ARRAY') {
|
32
|
0
|
|
|
|
|
0
|
$self->{'val_delims'} = $h{'val_delims'};
|
33
|
|
|
|
|
|
|
}
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# User supplied the template from a string
|
36
|
|
|
|
|
|
|
|
37
|
3
|
100
|
|
|
|
13
|
if ( defined($h{'template_str'}) ) {
|
38
|
2
|
|
|
|
|
8
|
$self->init_template($h{'template_str'});
|
39
|
2
|
|
|
|
|
6
|
return $self;
|
40
|
|
|
|
|
|
|
}
|
41
|
|
|
|
|
|
|
|
42
|
1
|
50
|
33
|
|
|
21
|
die 'Path to template not defined or missing'
|
43
|
|
|
|
|
|
|
unless defined($h{'template_path'}) and -e $h{'template_path'};
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
5
|
$self->init_template_from_file($h{'template_path'});
|
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
|
|
3
|
return $self;
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
} # new()
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub init_template_from_file {
|
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
1
|
0
|
2
|
my ($self, $tpath) = @_;
|
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
2
|
my $s;
|
58
|
1
|
50
|
|
|
|
25
|
open my $fh, $tpath or die "Couldn't open $tpath: $!";
|
59
|
1
|
|
|
|
|
18
|
while(<$fh>) { $s .= $_; }
|
|
2
|
|
|
|
|
7
|
|
60
|
1
|
|
|
|
|
5
|
close $fh;
|
61
|
1
|
|
|
|
|
4
|
$self->init_template($s);
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
}
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Handle template passed by user as string
|
66
|
|
|
|
|
|
|
sub init_template {
|
67
|
|
|
|
|
|
|
|
68
|
3
|
|
|
3
|
0
|
5
|
my ($self, $template) = @_;
|
69
|
|
|
|
|
|
|
|
70
|
3
|
|
|
|
|
86
|
my $sec = [ split( /($self->{'secpat'})/, $template ) ];
|
71
|
|
|
|
|
|
|
|
72
|
3
|
|
|
|
|
6
|
my %h;
|
73
|
3
|
|
|
|
|
4
|
my $curr = '';
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Top-down + only one 'body' follows section, why this parse hack works
|
76
|
3
|
|
|
|
|
6
|
for (my $i=0; $i <= $#{$sec} ; $i++) {
|
|
16
|
|
|
|
|
37
|
|
77
|
13
|
|
|
|
|
17
|
my $el = $$sec[$i];
|
78
|
13
|
100
|
|
|
|
30
|
next if $el =~ /^$/;
|
79
|
10
|
100
|
|
|
|
52
|
if ($el =~ /$self->{'secpat'}/) {
|
80
|
5
|
|
|
|
|
11
|
$curr = $1;
|
81
|
5
|
|
|
|
|
11
|
$h{$curr} = '';
|
82
|
5
|
|
|
|
|
7
|
$i++; # Skip next, it's the section name (an artifact)
|
83
|
|
|
|
|
|
|
}
|
84
|
|
|
|
|
|
|
else {
|
85
|
5
|
|
|
|
|
9
|
$h{$curr} = $el;
|
86
|
|
|
|
|
|
|
}
|
87
|
|
|
|
|
|
|
}
|
88
|
|
|
|
|
|
|
|
89
|
3
|
|
|
|
|
12
|
$self->{'template_secs'} = \%h;
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
} # init_template()
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub render {
|
95
|
|
|
|
|
|
|
|
96
|
8
|
|
|
8
|
1
|
923
|
my ( $self, $section, $hash_ref ) = @_;
|
97
|
|
|
|
|
|
|
|
98
|
8
|
50
|
|
|
|
19
|
die "Error: no section to render: $section\n" if !defined($section);
|
99
|
|
|
|
|
|
|
|
100
|
8
|
50
|
|
|
|
22
|
return if !exists $self->{'template_secs'}->{$section};
|
101
|
|
|
|
|
|
|
|
102
|
8
|
|
|
|
|
42
|
return $self->SUPER::render(
|
103
|
|
|
|
|
|
|
$self->{'template_secs'}->{$section}, $hash_ref, $self->{'val_delims'});
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} # render()
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# Set trim flags
|
109
|
|
|
|
|
|
|
sub trim {
|
110
|
4
|
|
|
4
|
1
|
660
|
my ($self, $flag) = @_;
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# trim() with no params defaults to trimming both ends
|
113
|
4
|
100
|
|
|
|
10
|
if (!defined $flag) {
|
114
|
1
|
|
|
|
|
1
|
$self->{'trim'} = 'both';
|
115
|
1
|
|
|
|
|
2
|
return;
|
116
|
|
|
|
|
|
|
}
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Turn trimming off
|
119
|
3
|
100
|
|
|
|
24
|
if ($flag =~ /^(off|o)$/i) {
|
120
|
1
|
|
|
|
|
2
|
$self->{'trim'} = undef;
|
121
|
1
|
|
|
|
|
2
|
return;
|
122
|
|
|
|
|
|
|
}
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Make sure we get something valid
|
125
|
2
|
50
|
|
|
|
11
|
if ($flag !~ /^(off|left|right|both|l|r|b|o)$/i) {
|
126
|
0
|
|
|
|
|
0
|
$self->{'trim'} = undef;
|
127
|
0
|
|
|
|
|
0
|
return;
|
128
|
|
|
|
|
|
|
}
|
129
|
|
|
|
|
|
|
|
130
|
2
|
|
|
|
|
3
|
$self->{'trim'} = $flag;
|
131
|
2
|
|
|
|
|
436
|
return;
|
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
} # trim()
|
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1;
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
__END__
|