line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::EasyTemplate; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23594
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
665
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
7
|
|
|
|
|
|
|
my $BASE = ''; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
11
|
0
|
|
|
|
|
|
my ($file) = @_; |
12
|
0
|
|
0
|
|
|
|
my $class = ref($proto) || $proto; |
13
|
0
|
|
|
|
|
|
my $self = {}; |
14
|
0
|
|
|
|
|
|
bless($self,$class); |
15
|
0
|
|
|
|
|
|
$self->{'FILE'} = $file; |
16
|
0
|
|
|
|
|
|
$self->{'HTML'} = $self->_read_file(); |
17
|
0
|
|
|
|
|
|
return $self; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub replace { |
21
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
22
|
0
|
|
|
|
|
|
my ($hashref) = @_; |
23
|
0
|
|
|
|
|
|
my %hash; |
24
|
0
|
0
|
|
|
|
|
%hash = %$hashref if ref($hashref) eq 'HASH'; |
25
|
0
|
|
|
|
|
|
my $data = $self->{'HTML'}; |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
while ($data =~ /\[\[IF (.*?)\]\](.*?)\[\[ENDIF\]\]/s) { |
28
|
0
|
|
|
|
|
|
my $token = $1; |
29
|
0
|
|
|
|
|
|
my $else = $2; |
30
|
0
|
0
|
|
|
|
|
if ($else =~ /\[\[ELSE\]\]/) { |
31
|
0
|
0
|
|
|
|
|
if ($hash{$token}) { |
32
|
0
|
|
|
|
|
|
$data =~ s/\[\[IF $token\]\](.*?)\[\[ELSE\]\].*?\[\[ENDIF\]\]/$1/s; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
else { |
35
|
0
|
|
|
|
|
|
$data =~ s/\[\[IF $token\]\].*?\[\[ELSE\]\](.*?)\[\[ENDIF\]\]/$1/s; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
else { |
39
|
0
|
0
|
|
|
|
|
if ($hash{$token}) { |
40
|
0
|
|
|
|
|
|
$data =~ s/\[\[IF $token\]\](.*?)\[\[ENDIF\]\]/$1/s; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
else { |
43
|
0
|
|
|
|
|
|
$data =~ s/\[\[IF $token\]\].*?\[\[ENDIF\]\]//s; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
while ($data =~ /\[\[(.*?)\]\]/g) { |
49
|
0
|
|
|
|
|
|
my $token = $1; |
50
|
0
|
|
|
|
|
|
my $key = $token; |
51
|
0
|
|
|
|
|
|
my $value = $hash{$key}; |
52
|
0
|
0
|
|
|
|
|
if (defined $value) { |
53
|
0
|
|
|
|
|
|
$data =~ s/\[\[$token\]\]/$value/mg; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
0
|
|
|
|
|
|
$data =~ s/\[\[$token\]\]//mg; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
return $data; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _read_file { |
64
|
0
|
|
|
0
|
|
|
my $self = shift; |
65
|
0
|
|
|
|
|
|
my $file = $self->{'FILE'}; |
66
|
0
|
|
|
|
|
|
my $CHUNK_SIZE = 4096; |
67
|
0
|
|
|
|
|
|
my ($chunk, $data); |
68
|
|
|
|
|
|
|
|
69
|
0
|
0
|
|
|
|
|
open(FILE, "$file") || return $self->_template_not_found($!,$file); |
70
|
0
|
0
|
|
|
|
|
binmode(FILE) || return $self->_template_not_found($!); |
71
|
0
|
|
|
|
|
|
$data = ''; |
72
|
0
|
|
|
|
|
|
while (read(FILE, $chunk, $CHUNK_SIZE)) { |
73
|
0
|
|
|
|
|
|
$data .= $chunk; |
74
|
|
|
|
|
|
|
} |
75
|
0
|
0
|
|
|
|
|
close(FILE) || return $self->_template_not_found($!); |
76
|
0
|
|
|
|
|
|
return $data; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _template_not_found { |
80
|
0
|
|
|
0
|
|
|
my $self = shift; |
81
|
0
|
|
|
|
|
|
my ($error) = @_; |
82
|
0
|
|
|
|
|
|
return qq|Error: template not found!\n\t$error ($self->{'FILE'})\n|; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
__END__ |