| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Template::JavaScript; |
|
2
|
|
|
|
|
|
|
BEGIN { |
|
3
|
5
|
|
|
5
|
|
244410
|
$Template::JavaScript::AUTHORITY = 'cpan:AVAR'; |
|
4
|
|
|
|
|
|
|
} |
|
5
|
|
|
|
|
|
|
BEGIN { |
|
6
|
5
|
|
|
5
|
|
106
|
$Template::JavaScript::VERSION = '0.01'; |
|
7
|
|
|
|
|
|
|
} |
|
8
|
|
|
|
|
|
|
# vim: ft=perl ts=4 sw=4 et: |
|
9
|
|
|
|
|
|
|
|
|
10
|
5
|
|
|
5
|
|
96
|
use v5.010.1; |
|
|
5
|
|
|
|
|
21
|
|
|
|
5
|
|
|
|
|
250
|
|
|
11
|
5
|
|
|
5
|
|
3820
|
use Any::Moose; |
|
|
5
|
|
|
|
|
228630
|
|
|
|
5
|
|
|
|
|
40
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# For compiling our output |
|
14
|
5
|
|
|
5
|
|
12133
|
use JavaScript::V8; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# For generating our output |
|
17
|
|
|
|
|
|
|
use Template; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Utility functions |
|
20
|
|
|
|
|
|
|
use JavaScript::Value::Escape; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 NAME |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Template::JavaScript - A templating engine using the L module |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use Test::More qw( no_plan ); |
|
29
|
|
|
|
|
|
|
use Template::JavaScript; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $tj = Template::JavaScript->new(); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$tj->output( \my $out ); |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$tj->tmpl_string( <<'' ); |
|
36
|
|
|
|
|
|
|
before |
|
37
|
|
|
|
|
|
|
% for( var i = 3; i ; i-- ){ |
|
38
|
|
|
|
|
|
|
this is a loop |
|
39
|
|
|
|
|
|
|
% } |
|
40
|
|
|
|
|
|
|
after |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$tj->run; |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
is( $out, <<'', 'can run simple JS code (loops)' ); |
|
45
|
|
|
|
|
|
|
before |
|
46
|
|
|
|
|
|
|
this is a loop |
|
47
|
|
|
|
|
|
|
this is a loop |
|
48
|
|
|
|
|
|
|
this is a loop |
|
49
|
|
|
|
|
|
|
after |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
This is a very simple template to JavaScript compiler. We compile |
|
54
|
|
|
|
|
|
|
either templates passed in as strings or as a file with L
|
|
55
|
|
|
|
|
|
|
Toolkit|Template>, so you can do includes etc. like L |
|
56
|
|
|
|
|
|
|
normally does it. |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
Once L has run we apply our own syntax, which is that any |
|
59
|
|
|
|
|
|
|
line beginning with C<%> is JavaScript and any other line is output |
|
60
|
|
|
|
|
|
|
verbatim. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
After the compilation phase (which you can cache) we execute the |
|
63
|
|
|
|
|
|
|
template with L. So your templates will run very fast |
|
64
|
|
|
|
|
|
|
in the V8 JIT. We provide ways to pass variables and functions back & |
|
65
|
|
|
|
|
|
|
forth to L through its normal facilities. |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
has bind => ( |
|
70
|
|
|
|
|
|
|
is => 'ro', |
|
71
|
|
|
|
|
|
|
isa => 'ArrayRef[Any]', |
|
72
|
|
|
|
|
|
|
default => sub { +[] }, |
|
73
|
|
|
|
|
|
|
documentation => 'Things to bind', |
|
74
|
|
|
|
|
|
|
); |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
has template => ( |
|
77
|
|
|
|
|
|
|
is => 'rw', |
|
78
|
|
|
|
|
|
|
isa => 'Str', |
|
79
|
|
|
|
|
|
|
documentation => 'Things to bind', |
|
80
|
|
|
|
|
|
|
); |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
has include_path => ( |
|
83
|
|
|
|
|
|
|
is => 'rw', |
|
84
|
|
|
|
|
|
|
isa => 'Str|ArrayRef', |
|
85
|
|
|
|
|
|
|
documentation => 'The include path for the templates', |
|
86
|
|
|
|
|
|
|
); |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
has output => ( |
|
89
|
|
|
|
|
|
|
is => 'rw', |
|
90
|
|
|
|
|
|
|
isa => 'Any', |
|
91
|
|
|
|
|
|
|
); |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
has _context => ( |
|
94
|
|
|
|
|
|
|
is => 'ro', |
|
95
|
|
|
|
|
|
|
isa => 'JavaScript::V8::Context', |
|
96
|
|
|
|
|
|
|
lazy_build => 1, |
|
97
|
|
|
|
|
|
|
documentation => '', |
|
98
|
|
|
|
|
|
|
); |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub _build__context { |
|
101
|
|
|
|
|
|
|
JavaScript::V8::Context->new; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
has _js_code => ( |
|
105
|
|
|
|
|
|
|
is => 'rw', |
|
106
|
|
|
|
|
|
|
isa => 'Str', |
|
107
|
|
|
|
|
|
|
documentation => 'Compiled JS code', |
|
108
|
|
|
|
|
|
|
); |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
has _result => ( |
|
111
|
|
|
|
|
|
|
is => 'rw', |
|
112
|
|
|
|
|
|
|
isa => 'Str', |
|
113
|
|
|
|
|
|
|
default => '', |
|
114
|
|
|
|
|
|
|
documentation => 'Result accumulator', |
|
115
|
|
|
|
|
|
|
); |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
has _tt => ( |
|
118
|
|
|
|
|
|
|
is => 'rw', |
|
119
|
|
|
|
|
|
|
isa => 'Template', |
|
120
|
|
|
|
|
|
|
lazy_build => 1, |
|
121
|
|
|
|
|
|
|
documentation => 'Our Template Toolkit object', |
|
122
|
|
|
|
|
|
|
); |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub _build__tt { |
|
125
|
|
|
|
|
|
|
my ($self) = @_; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $tt = Template->new({ |
|
128
|
|
|
|
|
|
|
INCLUDE_PATH => $self->include_path, # or list ref |
|
129
|
|
|
|
|
|
|
INTERPOLATE => 0, # expand "$var" in plain text |
|
130
|
|
|
|
|
|
|
POST_CHOMP => 0, # cleanup whitespace |
|
131
|
|
|
|
|
|
|
EVAL_PERL => 0, # evaluate Perl code blocks |
|
132
|
|
|
|
|
|
|
ABSOLUTE => 1, # all includes are absolute |
|
133
|
|
|
|
|
|
|
}); |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
return $tt; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub BUILD { |
|
139
|
|
|
|
|
|
|
my ($self) = @_; |
|
140
|
|
|
|
|
|
|
my $context = $self->_context; |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Standard library |
|
143
|
|
|
|
|
|
|
$context->bind_function( say => sub { |
|
144
|
|
|
|
|
|
|
$self->{_result} .= $_[0]; |
|
145
|
|
|
|
|
|
|
$self->{_result} .= "\n"; |
|
146
|
|
|
|
|
|
|
}); |
|
147
|
|
|
|
|
|
|
$context->bind_function( whisper => sub { |
|
148
|
|
|
|
|
|
|
$self->{_result} .= $_[0]; |
|
149
|
|
|
|
|
|
|
}); |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
# User-supplied stuff |
|
152
|
|
|
|
|
|
|
my $bind = $self->bind; |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
for my $b (@$bind) { |
|
155
|
|
|
|
|
|
|
$context->bind(@$b); |
|
156
|
|
|
|
|
|
|
} |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
return; |
|
159
|
|
|
|
|
|
|
} |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub tmpl_string { |
|
162
|
|
|
|
|
|
|
my ($self, $string) = @_; |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
my $output; |
|
165
|
|
|
|
|
|
|
$self->_tt->process(\$string, {}, \$output) || die $self->_tt->error; |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
$self->template( $output ); |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub tmpl_fh { |
|
171
|
|
|
|
|
|
|
my ($self, $fh) = @_; |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
my $code; |
|
174
|
|
|
|
|
|
|
{ |
|
175
|
|
|
|
|
|
|
local $/; |
|
176
|
|
|
|
|
|
|
$code = < $fh >; |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
my $output; |
|
180
|
|
|
|
|
|
|
$self->_tt->process(\$code, {}, \$output) || die $self->_tt->error; |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
$self->template( $output ); |
|
183
|
|
|
|
|
|
|
} |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub tmpl_file { |
|
186
|
|
|
|
|
|
|
my ($self, $file) = @_; |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
my $output; |
|
189
|
|
|
|
|
|
|
$self->_tt->process($file, {}, \$output) || die $self->_tt->error; |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
$self->template( $output ); |
|
192
|
|
|
|
|
|
|
} |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub compile { |
|
195
|
|
|
|
|
|
|
my ($self) = @_; |
|
196
|
|
|
|
|
|
|
my $context = $self->_context; |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
my $js_code = ''; |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
for my $line (split /^/, $self->template) { |
|
201
|
|
|
|
|
|
|
chomp $line; |
|
202
|
|
|
|
|
|
|
if ( substr($line, 0, 1) ne '%' ) { |
|
203
|
|
|
|
|
|
|
my @parts; |
|
204
|
|
|
|
|
|
|
# Parse inline variables |
|
205
|
|
|
|
|
|
|
while($line =~ /(.*?)<%\s*([^%]*?)\s*%>(.*)/s) { |
|
206
|
|
|
|
|
|
|
push (@parts, ( [ 'str', $1 ], [ 'expr', $2 ] )); |
|
207
|
|
|
|
|
|
|
$line = $3; |
|
208
|
|
|
|
|
|
|
} |
|
209
|
|
|
|
|
|
|
push (@parts, ['str', $line]) if ($line ne ''); |
|
210
|
|
|
|
|
|
|
# use Data::Dumper; |
|
211
|
|
|
|
|
|
|
# say STDERR "begin"; |
|
212
|
|
|
|
|
|
|
# say STDERR Dumper \@parts; |
|
213
|
|
|
|
|
|
|
# say STDERR "end"; |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
if (@parts == 0 || @parts == 1) { |
|
216
|
|
|
|
|
|
|
my $escaped = javascript_value_escape($line); |
|
217
|
|
|
|
|
|
|
$js_code .= qq[;say('$escaped');]; |
|
218
|
|
|
|
|
|
|
} else { |
|
219
|
|
|
|
|
|
|
# join them up |
|
220
|
|
|
|
|
|
|
$js_code .= join '', map { |
|
221
|
|
|
|
|
|
|
my ($what, $value) = @$_; |
|
222
|
|
|
|
|
|
|
my $ret; |
|
223
|
|
|
|
|
|
|
if ($what eq 'str') { |
|
224
|
|
|
|
|
|
|
my $escaped = javascript_value_escape($value); |
|
225
|
|
|
|
|
|
|
$ret = qq[;whisper('$escaped');]; |
|
226
|
|
|
|
|
|
|
} elsif ($what eq 'expr') { |
|
227
|
|
|
|
|
|
|
$ret = ";whisper($value);"; |
|
228
|
|
|
|
|
|
|
} else { |
|
229
|
|
|
|
|
|
|
die; |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
} @parts; |
|
232
|
|
|
|
|
|
|
$js_code .= qq[;whisper("\\n");]; |
|
233
|
|
|
|
|
|
|
} |
|
234
|
|
|
|
|
|
|
} else { |
|
235
|
|
|
|
|
|
|
substr($line, 0, 1, ''); |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
$js_code .= $line . "\n"; |
|
238
|
|
|
|
|
|
|
} |
|
239
|
|
|
|
|
|
|
} |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# say STDERR "CODE:{$js_code}"; |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
$self->_js_code( $js_code ); |
|
244
|
|
|
|
|
|
|
} |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub run { |
|
247
|
|
|
|
|
|
|
my ($self) = @_; |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
my $js_code = ''; |
|
250
|
|
|
|
|
|
|
unless ( $js_code = $self->_js_code ){ |
|
251
|
|
|
|
|
|
|
$self->compile; |
|
252
|
|
|
|
|
|
|
$js_code = $self->_js_code; |
|
253
|
|
|
|
|
|
|
} |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
my $context = $self->_context; |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
unless ( my $retval = $context->eval($js_code) ){ |
|
258
|
|
|
|
|
|
|
$retval //= ''; |
|
259
|
|
|
|
|
|
|
$@ //= ''; |
|
260
|
|
|
|
|
|
|
die "retval:[$retval] \$\@:[$@]"; |
|
261
|
|
|
|
|
|
|
} |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
given ( ref $self->{output} ) { |
|
264
|
|
|
|
|
|
|
when ( 'SCALAR' ){ |
|
265
|
|
|
|
|
|
|
${ $self->{output} } = $self->{_result}; |
|
266
|
|
|
|
|
|
|
} |
|
267
|
|
|
|
|
|
|
when ( 'GLOB' ){ |
|
268
|
|
|
|
|
|
|
print { $self->{output} } $self->{_result}; |
|
269
|
|
|
|
|
|
|
} |
|
270
|
|
|
|
|
|
|
} |
|
271
|
|
|
|
|
|
|
} |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |