line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Xslate::Util; |
2
|
184
|
|
|
184
|
|
27179
|
use strict; |
|
184
|
|
|
|
|
211
|
|
|
184
|
|
|
|
|
5217
|
|
3
|
184
|
|
|
183
|
|
1305
|
use warnings; |
|
184
|
|
|
|
|
1184
|
|
|
184
|
|
|
|
|
5070
|
|
4
|
|
|
|
|
|
|
|
5
|
184
|
|
|
183
|
|
1910
|
use Carp (); |
|
184
|
|
|
|
|
236
|
|
|
184
|
|
|
|
|
3563
|
|
6
|
|
|
|
|
|
|
|
7
|
184
|
|
|
183
|
|
62827
|
use parent qw(Exporter); |
|
184
|
|
|
|
|
38322
|
|
|
184
|
|
|
|
|
832
|
|
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
9
|
|
|
|
|
|
|
mark_raw unmark_raw |
10
|
|
|
|
|
|
|
html_escape escaped_string |
11
|
|
|
|
|
|
|
uri_escape |
12
|
|
|
|
|
|
|
p dump |
13
|
|
|
|
|
|
|
html_builder |
14
|
|
|
|
|
|
|
hash_with_default |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
literal_to_value value_to_literal |
17
|
|
|
|
|
|
|
import_from |
18
|
|
|
|
|
|
|
neat |
19
|
|
|
|
|
|
|
is_int any_in |
20
|
|
|
|
|
|
|
read_around |
21
|
|
|
|
|
|
|
make_error |
22
|
|
|
|
|
|
|
$DEBUG |
23
|
|
|
|
|
|
|
$STRING $NUMBER |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $DEBUG; |
27
|
|
|
|
|
|
|
defined($DEBUG) or $DEBUG = $ENV{XSLATE} || ''; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $DisplayWidth = 76; |
30
|
|
|
|
|
|
|
if($DEBUG =~ /display_width=(\d+)/) { |
31
|
|
|
|
|
|
|
$DisplayWidth = $1; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# cf. http://swtch.com/~rsc/regexp/regexp1.html |
35
|
|
|
|
|
|
|
my $dquoted = qr/" [^"\\]* (?: \\. [^"\\]* )* "/xms; |
36
|
|
|
|
|
|
|
my $squoted = qr/' [^'\\]* (?: \\. [^'\\]* )* '/xms; |
37
|
|
|
|
|
|
|
our $STRING = qr/(?: $dquoted | $squoted )/xms; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our $NUMBER = qr/ (?: |
40
|
|
|
|
|
|
|
(?: [0-9][0-9_]* (?: \. [0-9_]+)? \b) # decimal |
41
|
|
|
|
|
|
|
| |
42
|
|
|
|
|
|
|
(?: 0 (?: |
43
|
|
|
|
|
|
|
(?: [0-7_]+ ) # octal |
44
|
|
|
|
|
|
|
| |
45
|
|
|
|
|
|
|
(?: x [0-9a-fA-F_]+) # hex |
46
|
|
|
|
|
|
|
| |
47
|
|
|
|
|
|
|
(?: b [01_]+ ) # binary |
48
|
|
|
|
|
|
|
)?) |
49
|
|
|
|
|
|
|
)/xms; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
require Text::Xslate; # load XS stuff |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub mark_raw; # XS |
54
|
|
|
|
|
|
|
sub unmark_raw; # XS |
55
|
|
|
|
|
|
|
sub html_escape; # XS |
56
|
|
|
|
|
|
|
sub uri_escape; # XS |
57
|
|
|
|
|
|
|
sub escaped_string; *escaped_string = \&mark_raw; |
58
|
|
|
|
|
|
|
sub merge_hash; # XS |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub html_builder (&){ |
61
|
5
|
|
|
5
|
1
|
510
|
my($code_ref) = @_; |
62
|
|
|
|
|
|
|
return sub { |
63
|
5
|
|
|
5
|
|
30
|
my $ret = $code_ref->(@_); |
64
|
|
|
|
|
|
|
return ref($ret) eq 'CODE' |
65
|
5
|
50
|
|
|
|
74
|
? html_builder(\&{$ret}) |
|
0
|
|
|
|
|
0
|
|
66
|
|
|
|
|
|
|
: mark_raw($ret); |
67
|
5
|
|
|
|
|
44
|
}; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub hash_with_default { |
71
|
5
|
|
|
5
|
1
|
799
|
my($hash_ref, $default) = @_; |
72
|
5
|
50
|
|
|
|
21
|
ref($hash_ref) eq 'HASH' |
73
|
|
|
|
|
|
|
or Carp::croak('Usage: hash_with_default(\%vars, $default)'); |
74
|
5
|
|
|
|
|
1678
|
require 'Text/Xslate/HashWithDefault.pm'; |
75
|
5
|
|
|
|
|
10
|
my %vars; |
76
|
5
|
|
|
|
|
32
|
tie %vars, 'Text::Xslate::HashWithDefault', $hash_ref, $default; |
77
|
5
|
|
|
|
|
40
|
return \%vars; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# for internals |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub neat { |
84
|
22
|
|
|
22
|
0
|
29
|
my($s) = @_; |
85
|
22
|
100
|
|
|
|
36
|
if ( defined $s ) { |
86
|
21
|
50
|
33
|
|
|
99
|
if ( ref($s) || Scalar::Util::looks_like_number($s) ) { |
87
|
0
|
|
|
|
|
0
|
return $s; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else { |
90
|
21
|
|
|
|
|
109
|
return "'$s'"; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
1
|
|
|
|
|
3
|
return 'nil'; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub is_int { |
99
|
19402
|
|
|
19402
|
0
|
14081
|
my($s) = @_; |
100
|
|
|
|
|
|
|
# XXX: '+1', '1.0', '00', must NOT be interpreted as an integer |
101
|
19402
|
|
100
|
|
|
94056
|
return defined($s) |
102
|
|
|
|
|
|
|
&& $s =~ /\A -? [0-9]+ \z/xms |
103
|
|
|
|
|
|
|
&& int($s) eq $s |
104
|
|
|
|
|
|
|
&& abs(int($s)) < 0x7FFF_FFFF; # fits int32_t |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub any_in { |
108
|
3265
|
|
|
3265
|
0
|
2745
|
my $value = shift; |
109
|
3265
|
50
|
|
|
|
4004
|
if(defined $value) { |
110
|
3265
|
50
|
|
|
|
3170
|
return scalar grep { defined($_) && $value eq $_ } @_; |
|
6656
|
|
|
|
|
23204
|
|
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else { |
113
|
0
|
|
|
|
|
0
|
return scalar grep { not defined($_) } @_; |
|
0
|
|
|
|
|
0
|
|
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my %esc2char = ( |
118
|
|
|
|
|
|
|
't' => "\t", |
119
|
|
|
|
|
|
|
'n' => "\n", |
120
|
|
|
|
|
|
|
'r' => "\r", |
121
|
|
|
|
|
|
|
); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub literal_to_value { |
124
|
17717
|
|
|
17717
|
0
|
25751
|
my($value) = @_; |
125
|
17717
|
50
|
|
|
|
26021
|
return $value if not defined $value; |
126
|
|
|
|
|
|
|
|
127
|
17717
|
100
|
|
|
|
72999
|
if($value =~ s/\A "(.*)" \z/$1/xms){ |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
128
|
16406
|
100
|
|
|
|
32874
|
$value =~ s/\\(.)/$esc2char{$1} || $1/xmseg; |
|
8600
|
|
|
|
|
28907
|
|
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
elsif($value =~ s/\A '(.*)' \z/$1/xms) { |
131
|
116
|
|
|
|
|
191
|
$value =~ s/\\(['\\])/$1/xmsg; # ' for poor editors |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
elsif($value =~ /\A [+-]? $NUMBER \z/xmso) { |
134
|
1130
|
100
|
|
|
|
2336
|
if($value =~ s/\A ([+-]?) (?= 0[0-7xb])//xms) { |
135
|
37
|
100
|
|
|
|
104
|
$value = ($1 eq '-' ? -1 : +1) |
136
|
|
|
|
|
|
|
* oct($value); # also grok hex and binary |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
else { |
139
|
1093
|
|
|
|
|
1169
|
$value =~ s/_//xmsg; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
17717
|
|
|
|
|
52081
|
return $value; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
my %char2esc = ( |
147
|
|
|
|
|
|
|
"\\" => '\\\\', |
148
|
|
|
|
|
|
|
"\n" => '\\n', |
149
|
|
|
|
|
|
|
"\r" => '\\r', |
150
|
|
|
|
|
|
|
'"' => '\\"', |
151
|
|
|
|
|
|
|
'$' => '\\$', |
152
|
|
|
|
|
|
|
'@' => '\\@', |
153
|
|
|
|
|
|
|
); |
154
|
|
|
|
|
|
|
my $value_chars = join '|', map { quotemeta } keys %char2esc; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub value_to_literal { |
157
|
54
|
|
|
54
|
0
|
11262
|
my($value) = @_; |
158
|
54
|
50
|
|
|
|
98
|
return 'undef' if not defined $value; |
159
|
|
|
|
|
|
|
|
160
|
54
|
100
|
|
|
|
62
|
if(is_int($value)){ |
161
|
16
|
|
|
|
|
32
|
return $value; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
else { |
164
|
38
|
|
|
|
|
144
|
$value =~ s/($value_chars)/$char2esc{$1}/xmsgeo; |
|
8
|
|
|
|
|
27
|
|
165
|
38
|
|
|
|
|
101
|
return qq{"$value"}; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub import_from { |
170
|
18
|
|
|
18
|
0
|
3269
|
my $code = "# Text::Xslate::Util::import_from()\n" |
171
|
|
|
|
|
|
|
. "package " . "Text::Xslate::Util::_import;\n" |
172
|
|
|
|
|
|
|
. "use warnings FATAL => 'all';\n" |
173
|
|
|
|
|
|
|
. 'my @args;' . "\n"; |
174
|
|
|
|
|
|
|
|
175
|
18
|
|
|
|
|
59
|
for(my $i = 0; $i < @_; $i++) { |
176
|
24
|
|
|
|
|
37
|
my $module = $_[$i]; |
177
|
|
|
|
|
|
|
|
178
|
24
|
100
|
|
|
|
85
|
if($module =~ /[^a-zA-Z0-9_:]/) { |
179
|
1
|
|
|
|
|
118
|
Carp::confess("Xslate: Invalid module name: $module"); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
23
|
|
|
|
|
26
|
my $commands; |
183
|
23
|
100
|
|
|
|
81
|
if(ref $_[$i+1]){ |
184
|
13
|
|
|
|
|
3229
|
require 'Data/Dumper.pm'; |
185
|
13
|
|
|
|
|
22028
|
my @args = ($_[++$i]); |
186
|
13
|
|
|
|
|
24
|
my @protos = ('*data'); |
187
|
13
|
|
|
|
|
49
|
$commands = Data::Dumper->new(\@args, \@protos)->Terse(1)->Dump(); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
23
|
100
|
|
|
|
906
|
$code .= "use $module ();\n" if !$module->can('export_into_xslate'); |
191
|
|
|
|
|
|
|
|
192
|
23
|
50
|
66
|
|
|
104
|
if(!defined($commands) or $commands ne '') { |
193
|
23
|
|
100
|
|
|
202
|
$code .= sprintf <<'END_IMPORT', $module, $commands || '()'; |
194
|
|
|
|
|
|
|
@args = %2$s; |
195
|
|
|
|
|
|
|
%1$s->can('export_into_xslate') |
196
|
|
|
|
|
|
|
? %1$s->export_into_xslate(\@funcs, @args) # bridge modules |
197
|
|
|
|
|
|
|
: %1$s->import(@args); # function-based modules |
198
|
|
|
|
|
|
|
END_IMPORT |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
17
|
|
|
|
|
53
|
local $Text::Xslate::Util::{'_import::'}; |
203
|
|
|
|
|
|
|
#print STDERR $code; |
204
|
17
|
|
|
|
|
17
|
my @funcs; |
205
|
17
|
|
|
|
|
18
|
my $e = do { |
206
|
17
|
|
|
|
|
25
|
local $@; |
207
|
17
|
|
|
|
|
1292
|
eval qq{package} |
|
8
|
|
|
|
|
736
|
|
|
7
|
|
|
|
|
7
|
|
|
7
|
|
|
|
|
241
|
|
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
72
|
|
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
62
|
|
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
69
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
208
|
|
|
|
|
|
|
. qq{ Text::Xslate::Util::_import;\n} |
209
|
|
|
|
|
|
|
. $code; |
210
|
17
|
|
|
|
|
39
|
$@; |
211
|
|
|
|
|
|
|
}; |
212
|
17
|
100
|
|
|
|
280
|
Carp::confess("Xslate: Failed to import:\n" . $e) if $e; |
213
|
|
|
|
|
|
|
push @funcs, map { |
214
|
16
|
|
|
|
|
42
|
my $entity_ref = \$Text::Xslate::Util::_import::{$_}; |
|
49
|
|
|
|
|
49
|
|
215
|
49
|
|
|
|
|
32
|
my $c; |
216
|
49
|
50
|
|
|
|
80
|
if(ref($entity_ref) eq 'GLOB') { # normal symbols |
|
|
0
|
|
|
|
|
|
217
|
49
|
|
|
|
|
34
|
$c = *{$entity_ref}{CODE}; |
|
49
|
|
|
|
|
61
|
|
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
elsif(ref($entity_ref) eq 'REF') { # special constants |
220
|
183
|
|
|
183
|
|
206586
|
no strict 'refs'; |
|
183
|
|
|
|
|
256
|
|
|
183
|
|
|
|
|
98996
|
|
221
|
0
|
|
|
|
|
0
|
$c = \&{ 'Text::Xslate::Util::_import::' . $_ }; |
|
0
|
|
|
|
|
0
|
|
222
|
|
|
|
|
|
|
} |
223
|
49
|
100
|
|
|
|
102
|
defined($c) ? ($_ => $c) : (); |
224
|
|
|
|
|
|
|
} keys %Text::Xslate::Util::_import::; |
225
|
|
|
|
|
|
|
|
226
|
16
|
|
|
|
|
226
|
return {@funcs}; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub make_error { |
230
|
177
|
|
|
177
|
0
|
2246
|
my($self, $message, $file, $line, @extra) = @_; |
231
|
177
|
50
|
|
|
|
475
|
if(ref $message eq 'SCALAR') { # re-thrown form virtual machines |
232
|
0
|
|
|
|
|
0
|
return ${$message}; |
|
0
|
|
|
|
|
0
|
|
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
177
|
|
|
|
|
482
|
my $lines = read_around($file, $line, 1, $self->input_layer); |
236
|
177
|
100
|
|
|
|
328
|
if($lines) { |
237
|
154
|
100
|
|
|
|
568
|
$lines .= "\n" if $lines !~ /\n\z/xms; |
238
|
154
|
|
|
|
|
554
|
$lines = '-' x $DisplayWidth . "\n" |
239
|
|
|
|
|
|
|
. $lines |
240
|
|
|
|
|
|
|
. '-' x $DisplayWidth . "\n"; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
177
|
|
|
|
|
248
|
local $Carp::CarpLevel = $Carp::CarpLevel + 1; |
244
|
177
|
|
33
|
|
|
355
|
my $class = ref($self) || $self; |
245
|
177
|
100
|
|
|
|
1435
|
$message =~ s/\A \Q$class: \E//xms and $message .= "\t..."; |
246
|
|
|
|
|
|
|
|
247
|
177
|
100
|
|
|
|
386
|
if(defined $file) { |
248
|
167
|
100
|
|
|
|
235
|
if(defined $line) { |
249
|
164
|
|
|
|
|
210
|
unshift @extra, $line; |
250
|
|
|
|
|
|
|
} |
251
|
167
|
100
|
|
|
|
427
|
unshift @extra, ref($file) ? '' : $file; |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
177
|
100
|
|
|
|
282
|
if(@extra) { |
255
|
167
|
|
|
|
|
26897
|
$message = Carp::shortmess(sprintf '%s (%s)', |
256
|
|
|
|
|
|
|
$message, join(':', @extra)); |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
else { |
259
|
10
|
|
|
|
|
1314
|
$message = Carp::shortmess($message); |
260
|
|
|
|
|
|
|
} |
261
|
177
|
|
|
|
|
13199
|
return sprintf "%s: %s%s", |
262
|
|
|
|
|
|
|
$class, $message, $lines; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub read_around { # for error messages |
266
|
182
|
|
|
182
|
0
|
222
|
my($file, $line, $around, $input_layer) = @_; |
267
|
|
|
|
|
|
|
|
268
|
182
|
100
|
100
|
|
|
718
|
defined($file) && defined($line) or return ''; |
269
|
|
|
|
|
|
|
|
270
|
168
|
100
|
|
|
|
309
|
if (ref $file) { # if $file is a scalar ref, it must contain text strings |
271
|
141
|
|
|
|
|
134
|
my $content = $$file; |
272
|
141
|
|
|
|
|
262
|
utf8::encode($content); |
273
|
141
|
|
|
|
|
162
|
$file = \$content; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
|
276
|
168
|
100
|
|
|
|
282
|
$around = 1 if not defined $around; |
277
|
168
|
100
|
|
|
|
258
|
$input_layer = '' if not defined $input_layer; |
278
|
|
|
|
|
|
|
|
279
|
168
|
100
|
|
29
|
|
3022
|
open my $in, '<' . $input_layer, $file or return ''; |
|
29
|
|
|
|
|
148
|
|
|
29
|
|
|
|
|
33
|
|
|
29
|
|
|
|
|
681
|
|
280
|
157
|
|
|
|
|
16160
|
local $/ = "\n"; |
281
|
157
|
|
|
|
|
273
|
local $. = 0; |
282
|
|
|
|
|
|
|
|
283
|
157
|
|
|
|
|
164
|
my $s = ''; |
284
|
157
|
|
|
|
|
760
|
while(defined(my $l = <$in>)) { |
285
|
384
|
100
|
|
|
|
615
|
if($. >= ($line - $around)) { |
286
|
244
|
|
|
|
|
321
|
$s .= $l; |
287
|
|
|
|
|
|
|
} |
288
|
384
|
100
|
|
|
|
989
|
if($. >= ($line + $around)) { |
289
|
40
|
|
|
|
|
52
|
last; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
} |
292
|
157
|
|
|
|
|
835
|
return $s; |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
sub p { # for debugging, the guts of dump() |
296
|
74
|
|
|
74
|
1
|
7637
|
require 'Data/Dumper.pm'; # we don't want to create its namespace |
297
|
74
|
|
|
|
|
35037
|
my $dd = Data::Dumper->new(\@_); |
298
|
74
|
|
|
|
|
1504
|
$dd->Indent(1); |
299
|
74
|
|
|
|
|
563
|
$dd->Sortkeys(1); |
300
|
74
|
|
|
|
|
290
|
$dd->Quotekeys(0); |
301
|
74
|
|
|
|
|
265
|
$dd->Terse(1); |
302
|
74
|
50
|
|
|
|
351
|
return $dd->Dump() if defined wantarray; |
303
|
0
|
|
|
|
|
0
|
print $dd->Dump(); |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
3
|
|
|
3
|
1
|
10
|
sub dump :method { goto &p } |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
1; |
309
|
|
|
|
|
|
|
__END__ |