line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Handlebars; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:DOY'; |
3
|
|
|
|
|
|
|
$Text::Handlebars::VERSION = '0.05'; |
4
|
14
|
|
|
14
|
|
365888
|
use strict; |
|
14
|
|
|
|
|
31
|
|
|
14
|
|
|
|
|
362
|
|
5
|
14
|
|
|
14
|
|
73
|
use warnings; |
|
14
|
|
|
|
|
26
|
|
|
14
|
|
|
|
|
411
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: http://handlebarsjs.com/ for Text::Xslate |
7
|
|
|
|
|
|
|
|
8
|
14
|
|
|
14
|
|
11610
|
use Text::Xslate 2.0000; |
|
14
|
|
|
|
|
158887
|
|
|
14
|
|
|
|
|
779
|
|
9
|
14
|
|
|
14
|
|
103
|
use base 'Text::Xslate'; |
|
14
|
|
|
|
|
27
|
|
|
14
|
|
|
|
|
1517
|
|
10
|
|
|
|
|
|
|
|
11
|
14
|
|
|
14
|
|
76
|
use Scalar::Util 'weaken'; |
|
14
|
|
|
|
|
27
|
|
|
14
|
|
|
|
|
1282
|
|
12
|
14
|
|
|
14
|
|
75
|
use Try::Tiny; |
|
14
|
|
|
|
|
23
|
|
|
14
|
|
|
|
|
13222
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub default_helpers { |
16
|
162
|
|
|
162
|
0
|
237
|
my $class = shift; |
17
|
|
|
|
|
|
|
return { |
18
|
|
|
|
|
|
|
with => sub { |
19
|
3
|
|
|
3
|
|
5
|
my ($context, $new_context, $options) = @_; |
20
|
3
|
|
|
|
|
10
|
return $options->{fn}->($new_context); |
21
|
|
|
|
|
|
|
}, |
22
|
|
|
|
|
|
|
each => sub { |
23
|
14
|
|
|
14
|
|
24
|
my ($context, $list, $options) = @_; |
24
|
14
|
|
|
|
|
26
|
return join '', map { $options->{fn}->($_) } @$list; |
|
31
|
|
|
|
|
28844
|
|
25
|
|
|
|
|
|
|
}, |
26
|
|
|
|
|
|
|
if => sub { |
27
|
8
|
|
|
8
|
|
16
|
my ($context, $conditional, $options) = @_; |
28
|
|
|
|
|
|
|
return $conditional |
29
|
|
|
|
|
|
|
? $options->{fn}->($context) |
30
|
8
|
100
|
|
|
|
32
|
: $options->{inverse}->($context); |
31
|
|
|
|
|
|
|
}, |
32
|
|
|
|
|
|
|
unless => sub { |
33
|
2
|
|
|
2
|
|
5
|
my ($context, $conditional, $options) = @_; |
34
|
|
|
|
|
|
|
return $conditional |
35
|
|
|
|
|
|
|
? $options->{inverse}->($context) |
36
|
2
|
100
|
|
|
|
7
|
: $options->{fn}->($context); |
37
|
|
|
|
|
|
|
}, |
38
|
162
|
|
|
|
|
2434
|
}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub default_functions { |
42
|
81
|
|
|
81
|
0
|
384
|
my $class = shift; |
43
|
|
|
|
|
|
|
return { |
44
|
81
|
|
|
|
|
314
|
%{ $class->SUPER::default_functions(@_) }, |
45
|
81
|
|
|
|
|
121
|
%{ $class->default_helpers }, |
|
81
|
|
|
|
|
403
|
|
46
|
|
|
|
|
|
|
}; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub options { |
50
|
81
|
|
|
81
|
0
|
851178
|
my $class = shift; |
51
|
|
|
|
|
|
|
|
52
|
81
|
|
|
|
|
404
|
my $options = $class->SUPER::options(@_); |
53
|
|
|
|
|
|
|
|
54
|
81
|
|
|
|
|
2519
|
$options->{compiler} = 'Text::Handlebars::Compiler'; |
55
|
81
|
|
|
|
|
191
|
$options->{helpers} = {}; |
56
|
|
|
|
|
|
|
|
57
|
81
|
|
|
|
|
240
|
return $options; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _register_builtin_methods { |
61
|
81
|
|
|
81
|
|
9435
|
my $self = shift; |
62
|
81
|
|
|
|
|
142
|
my ($funcs) = @_; |
63
|
|
|
|
|
|
|
|
64
|
81
|
|
|
|
|
264
|
weaken(my $weakself = $self); |
65
|
|
|
|
|
|
|
$funcs->{'(render_string)'} = sub { |
66
|
3
|
|
|
3
|
|
35943
|
my ($to_render, $vars) = @_; |
67
|
3
|
|
|
|
|
15
|
return $weakself->render_string($to_render, $vars); |
68
|
81
|
|
|
|
|
349
|
}; |
69
|
|
|
|
|
|
|
$funcs->{'(make_block_helper)'} = sub { |
70
|
34
|
|
|
34
|
|
77011
|
my ($vars, $code, $raw_text, $else_raw_text, $hash) = @_; |
71
|
|
|
|
|
|
|
|
72
|
34
|
|
|
|
|
51
|
my $options = {}; |
73
|
|
|
|
|
|
|
$options->{fn} = sub { |
74
|
49
|
|
|
|
|
7987
|
my ($new_vars) = @_; |
75
|
|
|
|
|
|
|
$new_vars = { |
76
|
49
|
|
|
|
|
66
|
%{ canonicalize_vars($new_vars) }, |
|
49
|
|
|
|
|
102
|
|
77
|
|
|
|
|
|
|
'..' => $vars, |
78
|
|
|
|
|
|
|
}; |
79
|
49
|
|
|
|
|
170
|
return $weakself->render_string($raw_text, $new_vars); |
80
|
34
|
|
|
|
|
199
|
}; |
81
|
|
|
|
|
|
|
$options->{inverse} = sub { |
82
|
7
|
|
|
|
|
24
|
my ($new_vars) = @_; |
83
|
|
|
|
|
|
|
$new_vars = { |
84
|
7
|
|
|
|
|
11
|
%{ canonicalize_vars($new_vars) }, |
|
7
|
|
|
|
|
17
|
|
85
|
|
|
|
|
|
|
'..' => $vars, |
86
|
|
|
|
|
|
|
}; |
87
|
7
|
|
|
|
|
24
|
return $weakself->render_string($else_raw_text, $new_vars); |
88
|
34
|
|
|
|
|
136
|
}; |
89
|
34
|
|
|
|
|
77
|
$options->{hash} = $hash; |
90
|
|
|
|
|
|
|
|
91
|
34
|
|
|
|
|
156
|
return sub { $code->(@_, $options); }; |
|
34
|
|
|
|
|
102
|
|
92
|
81
|
|
|
|
|
332
|
}; |
93
|
|
|
|
|
|
|
|
94
|
81
|
|
|
|
|
140
|
for my $helper (keys %{ $self->{helpers} }) { |
|
81
|
|
|
|
|
477
|
|
95
|
19
|
|
|
|
|
73
|
$funcs->{$helper} = $self->{helpers}{$helper}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub _compiler { |
100
|
145
|
|
|
145
|
|
30499
|
my $self = shift; |
101
|
|
|
|
|
|
|
|
102
|
145
|
100
|
|
|
|
388
|
if (!ref($self->{compiler})) { |
103
|
81
|
|
|
|
|
325
|
my $compiler = $self->SUPER::_compiler(@_); |
104
|
81
|
|
|
|
|
577
|
$compiler->define_helper(keys %{ $self->{helpers} }); |
|
81
|
|
|
|
|
408
|
|
105
|
81
|
|
|
|
|
124
|
$compiler->define_helper(keys %{ $self->default_helpers }); |
|
81
|
|
|
|
|
214
|
|
106
|
81
|
|
|
|
|
1242
|
return $compiler; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
64
|
|
|
|
|
207
|
return $self->SUPER::_compiler(@_); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub render_string { |
114
|
139
|
|
|
139
|
1
|
5329
|
my $self = shift; |
115
|
139
|
|
|
|
|
240
|
my ($string, $vars) = @_; |
116
|
|
|
|
|
|
|
|
117
|
139
|
|
|
|
|
327
|
return $self->SUPER::render_string($string, canonicalize_vars($vars)); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub render { |
121
|
176
|
|
|
176
|
1
|
42954
|
my $self = shift; |
122
|
176
|
|
|
|
|
284
|
my ($name, $vars) = @_; |
123
|
|
|
|
|
|
|
|
124
|
176
|
|
|
|
|
333
|
return $self->SUPER::render($name, canonicalize_vars($vars)); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub canonicalize_vars { |
128
|
371
|
|
|
371
|
0
|
526
|
my ($vars) = @_; |
129
|
371
|
100
|
66
|
|
|
1504
|
if (ref($vars) && ref($vars) eq 'HASH') { |
130
|
174
|
|
|
|
|
1145
|
return $vars; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
else { |
133
|
197
|
|
|
|
|
1903
|
return { '.' => $vars }; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |