| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Goo::Template; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
############################################################################### |
|
6
|
|
|
|
|
|
|
# Nigel Hamilton |
|
7
|
|
|
|
|
|
|
# |
|
8
|
|
|
|
|
|
|
# Copyright Nigel Hamilton 2002 |
|
9
|
|
|
|
|
|
|
# All Rights Reserved |
|
10
|
|
|
|
|
|
|
# |
|
11
|
|
|
|
|
|
|
# Author: Nigel Hamilton |
|
12
|
|
|
|
|
|
|
# Filename: Goo::Template.pm |
|
13
|
|
|
|
|
|
|
# Description: Replace tokens in a file or a string |
|
14
|
|
|
|
|
|
|
# |
|
15
|
|
|
|
|
|
|
# Date Change |
|
16
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
|
17
|
|
|
|
|
|
|
# 04/03/1999 Version 1 - Based on Michael Snell's brilliant replace.pm |
|
18
|
|
|
|
|
|
|
# "This is the future" |
|
19
|
|
|
|
|
|
|
# 10/05/2000 Version 2 - a more efficient slurping mode |
|
20
|
|
|
|
|
|
|
# 01/02/2002 Caching version - is memory consumption OK? |
|
21
|
|
|
|
|
|
|
# 24/07/2002 More OO version |
|
22
|
|
|
|
|
|
|
# 24/07/2004 Added a new dynamic include token |
|
23
|
|
|
|
|
|
|
# {{>HelloWorld::hello()}} it efficiently replaces the token with |
|
24
|
|
|
|
|
|
|
# the output of the code specified |
|
25
|
|
|
|
|
|
|
# 05/08/2004 Needed a way of evaluating new dynamic tokens - recursion? |
|
26
|
|
|
|
|
|
|
# 02/02/2005 Added a direct WebDBLite way of replacing in templates |
|
27
|
|
|
|
|
|
|
# Decided not to --- this module must stay lite. |
|
28
|
|
|
|
|
|
|
# 10/02/2005 Added slurping FileUtilities functions for file access |
|
29
|
|
|
|
|
|
|
# |
|
30
|
|
|
|
|
|
|
############################################################################### |
|
31
|
|
|
|
|
|
|
|
|
32
|
1
|
|
|
1
|
|
4074
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
26
|
|
|
33
|
1
|
|
|
1
|
|
4
|
use Goo::Object; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
16
|
|
|
34
|
1
|
|
|
1
|
|
4
|
use Goo::FileUtilities; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
549
|
|
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our @ISA = ("Goo::Object"); # used to print out the contents of the cache |
|
37
|
|
|
|
|
|
|
our $cache = {}; # a persistent hash of templates keyed on filenames |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
############################################################################### |
|
41
|
|
|
|
|
|
|
# |
|
42
|
|
|
|
|
|
|
# replace_tokens_in_string - replace tokens in a string |
|
43
|
|
|
|
|
|
|
# |
|
44
|
|
|
|
|
|
|
############################################################################### |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub replace_tokens_in_string { |
|
47
|
|
|
|
|
|
|
|
|
48
|
0
|
|
|
0
|
1
|
|
my ($string, $tokens) = @_; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# replace tokens in the string |
|
51
|
0
|
|
|
|
|
|
$string =~ s! {{([^}]*)}} ! # match {{ followed by any |
|
52
|
|
|
|
|
|
|
# non } characters followed by |
|
53
|
|
|
|
|
|
|
# two }} characters |
|
54
|
0
|
|
|
|
|
|
get_token($1, $tokens) |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
!gsex; # g - global, keep matching |
|
57
|
|
|
|
|
|
|
# e - eval the code to substitute |
|
58
|
|
|
|
|
|
|
# s - . matches newlines, needed? |
|
59
|
|
|
|
|
|
|
# x - ignore whitespace allow comments |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
return $string; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
############################################################################### |
|
68
|
|
|
|
|
|
|
# |
|
69
|
|
|
|
|
|
|
# get_token - look up the hash for the token and return the value |
|
70
|
|
|
|
|
|
|
# |
|
71
|
|
|
|
|
|
|
############################################################################### |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub get_token { |
|
74
|
|
|
|
|
|
|
|
|
75
|
0
|
|
|
0
|
1
|
|
my ($tokenstring, $tokenhash) = @_; |
|
76
|
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
if (exists $tokenhash->{$tokenstring}) { |
|
78
|
0
|
|
|
|
|
|
return $tokenhash->{$tokenstring}; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# is it a special code include token? |
|
82
|
|
|
|
|
|
|
# e.g., {{>Test::HelloWorld()}} |
|
83
|
0
|
0
|
|
|
|
|
if ($tokenstring =~ /^\>(.*?)::(.*?)$/) { |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# insert an object dynamically generated from code |
|
86
|
0
|
|
|
|
|
|
return eval <<CODE; |
|
87
|
|
|
|
|
|
|
use lib '/home/search/shared/bin'; |
|
88
|
|
|
|
|
|
|
use lib '/home/search/trexy/bin'; |
|
89
|
|
|
|
|
|
|
use lib '/home/search/trexy/bin'; |
|
90
|
|
|
|
|
|
|
use $1; |
|
91
|
|
|
|
|
|
|
$1::$2; |
|
92
|
|
|
|
|
|
|
CODE |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
#return "wanker"; |
|
96
|
|
|
|
|
|
|
} |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# maybe the token has already been set with curly brackets {{ }} |
|
99
|
|
|
|
|
|
|
# this is so the template module is backwards compatible |
|
100
|
0
|
|
|
|
|
|
$tokenstring = '{{' . $tokenstring . '}}'; |
|
101
|
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
return $tokenhash->{$tokenstring}; |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
############################################################################### |
|
108
|
|
|
|
|
|
|
# |
|
109
|
|
|
|
|
|
|
# replace_tokens_in_file - replace tokens in a file and use a cache too |
|
110
|
|
|
|
|
|
|
# |
|
111
|
|
|
|
|
|
|
############################################################################### |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub replace_tokens_in_file { |
|
114
|
|
|
|
|
|
|
|
|
115
|
0
|
|
|
0
|
1
|
|
my ($template_file, $tokens) = @_; |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# is the file in the cache? if so, don't go to disk, use the cached version |
|
118
|
|
|
|
|
|
|
# instead |
|
119
|
0
|
0
|
|
|
|
|
if (exists($cache->{$template_file})) { |
|
120
|
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
return replace_tokens_in_string($cache->{$template_file}, $tokens); |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# this file is not in the cache - grab it from disk and put into cache |
|
126
|
0
|
|
|
|
|
|
$cache->{$template_file} = Goo::FileUtilities::get_file_as_string($template_file); |
|
127
|
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
return replace_tokens_in_string($cache->{$template_file}, $tokens); |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
__END__ |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 NAME |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Goo::Template - Replace special tokens in a file or a string |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
use Goo::Template; |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 METHODS |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=over |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item replace_tokens_in_string |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
replace tokens in a string |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item get_token |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
look up the hash for the token and return the value |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item replace_tokens_in_file |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
replace tokens in a file |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=back |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 AUTHOR |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Nigel Hamilton <nigel@trexy.com> |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
171
|
|
|
|
|
|
|
|