line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Clevery::Modifier; |
2
|
14
|
|
|
14
|
|
83
|
use strict; |
|
14
|
|
|
|
|
26
|
|
|
14
|
|
|
|
|
547
|
|
3
|
14
|
|
|
14
|
|
71
|
use warnings; |
|
14
|
|
|
|
|
24
|
|
|
14
|
|
|
|
|
529
|
|
4
|
|
|
|
|
|
|
|
5
|
14
|
|
|
14
|
|
71
|
use parent qw(Text::Xslate::Bridge); |
|
14
|
|
|
|
|
25
|
|
|
14
|
|
|
|
|
92
|
|
6
|
|
|
|
|
|
|
|
7
|
14
|
|
|
14
|
|
2037
|
use List::Util qw(min); |
|
14
|
|
|
|
|
27
|
|
|
14
|
|
|
|
|
1876
|
|
8
|
|
|
|
|
|
|
|
9
|
14
|
|
|
|
|
1059
|
use Text::Xslate::Util qw( |
10
|
|
|
|
|
|
|
p |
11
|
|
|
|
|
|
|
mark_raw |
12
|
|
|
|
|
|
|
html_escape |
13
|
|
|
|
|
|
|
uri_escape |
14
|
14
|
|
|
14
|
|
111
|
); |
|
14
|
|
|
|
|
34
|
|
15
|
|
|
|
|
|
|
|
16
|
14
|
|
|
|
|
11506
|
use Text::Clevery::Util qw( |
17
|
|
|
|
|
|
|
safe_join |
18
|
|
|
|
|
|
|
safe_cat |
19
|
|
|
|
|
|
|
true false |
20
|
14
|
|
|
14
|
|
72
|
); |
|
14
|
|
|
|
|
22
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
require Text::Clevery; |
23
|
|
|
|
|
|
|
our $EngineClass = 'Text::Clevery'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my %modifier = map { $_ => __PACKAGE__->can($_) || die $_ } qw( |
26
|
|
|
|
|
|
|
capitalize |
27
|
|
|
|
|
|
|
cat |
28
|
|
|
|
|
|
|
count_characters |
29
|
|
|
|
|
|
|
count_paragraphs |
30
|
|
|
|
|
|
|
count_sentences |
31
|
|
|
|
|
|
|
count_words |
32
|
|
|
|
|
|
|
date_format |
33
|
|
|
|
|
|
|
default |
34
|
|
|
|
|
|
|
escape |
35
|
|
|
|
|
|
|
indent |
36
|
|
|
|
|
|
|
lower |
37
|
|
|
|
|
|
|
nl2br |
38
|
|
|
|
|
|
|
regex_replace |
39
|
|
|
|
|
|
|
replace |
40
|
|
|
|
|
|
|
spacify |
41
|
|
|
|
|
|
|
string_format |
42
|
|
|
|
|
|
|
strip |
43
|
|
|
|
|
|
|
strip_tags |
44
|
|
|
|
|
|
|
truncate |
45
|
|
|
|
|
|
|
upper |
46
|
|
|
|
|
|
|
wordwrap |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
__PACKAGE__->bridge(function => \%modifier); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub capitalize { |
51
|
3
|
|
|
3
|
1
|
2812
|
my($str, $number_as_word) = @_; |
52
|
3
|
100
|
|
|
|
15
|
my $word = $number_as_word |
53
|
|
|
|
|
|
|
? qr/\b ([[:alpha:]]\w*) \b/xms |
54
|
|
|
|
|
|
|
: qr/\b ([[:alpha:]]+) \b/xms; |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
|
|
19
|
$str =~ s/$word/ ucfirst($1) /xmseg; |
|
16
|
|
|
|
|
58
|
|
57
|
3
|
|
|
|
|
31
|
return $str; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub cat { |
61
|
4
|
|
|
4
|
1
|
4641
|
return safe_cat(@_); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub count_characters { |
65
|
2
|
|
|
2
|
1
|
1968
|
my($str, $count_whitespaces) = @_; |
66
|
2
|
100
|
|
|
|
8
|
if(!$count_whitespaces) { |
67
|
1
|
|
|
|
|
90
|
$str =~ s/\s+//g; |
68
|
|
|
|
|
|
|
} |
69
|
2
|
|
|
|
|
17
|
return length($str); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub count_paragraphs { |
73
|
1
|
|
|
1
|
1
|
2663
|
my($str) = @_; |
74
|
1
|
|
|
|
|
14
|
return scalar $str =~ s/([\r\n]+)/$1/xmsg; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub count_sentences { |
78
|
1
|
|
|
1
|
1
|
3
|
my($str) = @_; |
79
|
1
|
|
|
|
|
15
|
return scalar $str =~ s/(\S \.) (?!\w)/$1/xmsg; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub count_words { |
83
|
1
|
|
|
1
|
1
|
2
|
my($str) = @_; |
84
|
1
|
|
|
|
|
41
|
return scalar $str =~ s/(\S+)/$1/xmsg; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub date_format { |
88
|
2
|
|
|
2
|
1
|
2703
|
my($time, $format, $default) = @_; |
89
|
2
|
|
|
|
|
18
|
require Time::Piece; |
90
|
2
|
50
|
|
|
|
35
|
return $time |
91
|
|
|
|
|
|
|
? Time::Piece->new($time)->strftime($format) |
92
|
|
|
|
|
|
|
: $default; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub default { |
96
|
3
|
|
|
3
|
1
|
15856
|
my($value, $default) = @_; |
97
|
3
|
100
|
100
|
|
|
58
|
return defined($value) && length($value) |
98
|
|
|
|
|
|
|
? $value |
99
|
|
|
|
|
|
|
: $default; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# See smarty3/libs/plugins/modifier.escape.php |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub escape { |
105
|
11
|
|
|
11
|
1
|
12766
|
my($str, $format, $encoding) = @_; |
106
|
11
|
|
100
|
|
|
88
|
$format ||= 'html'; |
107
|
11
|
|
50
|
|
|
55
|
$encoding ||= 'ISO-8859-1'; |
108
|
|
|
|
|
|
|
|
109
|
11
|
100
|
|
|
|
113
|
if($format eq 'html') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
110
|
2
|
|
|
|
|
18
|
return html_escape($str); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
elsif($format eq 'htmlall') { |
113
|
1
|
|
|
|
|
1280
|
require HTML::Entities; |
114
|
1
|
|
|
|
|
5781
|
$str = HTML::Entities::encode($str); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
elsif($format =~ /\A ur [il] ( pathinfo )? \z/xms) { |
117
|
3
|
|
|
|
|
15
|
$str = uri_escape($str); |
118
|
3
|
100
|
|
|
|
14
|
if($1) { # ur[il]pathinfo |
119
|
1
|
|
|
|
|
6
|
$str =~ s{%2F}{/}g; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
elsif($format eq 'quotes') { |
123
|
|
|
|
|
|
|
# escapes single quotes and back slashes |
124
|
1
|
|
|
|
|
7
|
$str =~ s{ ( [\\'] ) }{\\$1}xmsg; # ' |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
elsif($format eq 'hex') { |
127
|
14
|
|
|
14
|
|
15339
|
use bytes; |
|
14
|
|
|
|
|
148
|
|
|
14
|
|
|
|
|
63
|
|
128
|
1
|
|
|
|
|
7
|
$str =~ s{ (.) }{ '%' . unpack('H*', $1) }xmsge; |
|
18
|
|
|
|
|
59
|
|
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
elsif($format eq 'hexentity') { |
131
|
1
|
|
|
|
|
5
|
$str =~ s{ (.) }{ '' . unpack('H*', $1) . ';' }xmsge; |
|
18
|
|
|
|
|
56
|
|
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
elsif($format eq 'decentity') { |
134
|
0
|
|
|
|
|
0
|
$str =~ s{ (.) }{ '' . ord($1) . ';' }xmsge; |
|
0
|
|
|
|
|
0
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
elsif($format eq 'javascript') { |
137
|
1
|
|
|
|
|
10
|
my %map = ( |
138
|
|
|
|
|
|
|
q{\\} => q{\\\\}, |
139
|
|
|
|
|
|
|
q{'} => q{\\'}, |
140
|
|
|
|
|
|
|
q{"} => q{\\"}, |
141
|
|
|
|
|
|
|
qq{\r} => q{\r}, |
142
|
|
|
|
|
|
|
qq{\n} => q{\n}, |
143
|
|
|
|
|
|
|
q{} => q{<\/}, |
144
|
|
|
|
|
|
|
); |
145
|
1
|
|
|
|
|
5
|
my $pat = join '|', map { quotemeta } keys %map; |
|
6
|
|
|
|
|
15
|
|
146
|
1
|
|
|
|
|
50
|
$str =~ s/($pat)/$map{$1}/xmsge; |
|
5
|
|
|
|
|
23
|
|
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
elsif($format eq 'mail') { |
149
|
1
|
|
|
|
|
7
|
$str =~ s/\@/ [AT] /g; |
150
|
1
|
|
|
|
|
5
|
$str =~ s/\./ [DOT] /g; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
elsif($format eq 'nonstd') { |
153
|
14
|
|
|
14
|
|
3735
|
use bytes; |
|
14
|
|
|
|
|
26
|
|
|
14
|
|
|
|
|
54
|
|
154
|
0
|
|
|
|
|
0
|
$str =~ s/([^\x00-\x7d])/'' . ord($1) . ';'/xmsge; |
|
0
|
|
|
|
|
0
|
|
155
|
0
|
|
|
|
|
0
|
$str = mark_raw($str); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
else { |
158
|
0
|
|
|
|
|
0
|
warnings::warnif(misc => "Unknown escape format '$format' used"); |
159
|
|
|
|
|
|
|
} |
160
|
9
|
|
|
|
|
124
|
return mark_raw($str); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub indent { |
164
|
3
|
|
|
3
|
1
|
2474
|
my($str, $count, $padding) = @_; |
165
|
3
|
100
|
|
|
|
12
|
$count = 4 if not defined $count; |
166
|
3
|
100
|
|
|
|
11
|
$padding = ' ' if not defined $padding; |
167
|
|
|
|
|
|
|
|
168
|
3
|
|
|
|
|
10
|
$padding x= $count; |
169
|
3
|
|
|
|
|
21
|
$str =~ s/^/$padding/xmsg; |
170
|
3
|
|
|
|
|
26
|
return $str; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub lower { |
174
|
2
|
|
|
2
|
1
|
2237
|
my($str) = @_; |
175
|
2
|
|
|
|
|
18
|
return lc($str); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub nl2br { |
179
|
1
|
|
|
1
|
1
|
920
|
my($str) = @_; |
180
|
1
|
|
|
|
|
15
|
return safe_join mark_raw(" "), |
181
|
|
|
|
|
|
|
split /\n/, $str, -1; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub regex_replace { |
185
|
1
|
|
|
1
|
1
|
3530
|
my($str, $pattern, $replace) = @_; |
186
|
1
|
|
|
|
|
29
|
$str =~ s/$pattern/$replace/msg; |
187
|
1
|
|
|
|
|
9
|
return $str; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub replace { |
191
|
2
|
|
|
2
|
1
|
5
|
my($str, $pattern, $replace) = @_; |
192
|
2
|
|
|
|
|
81
|
$str =~ s/\Q$pattern\E/$replace/msg; |
193
|
2
|
|
|
|
|
22
|
return $str; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub spacify { |
197
|
2
|
|
|
2
|
1
|
3581
|
my($str, $padding) = @_; |
198
|
2
|
100
|
|
|
|
9
|
$padding = ' ' if not defined $padding; |
199
|
2
|
|
|
|
|
15
|
return safe_join $padding, split //, $str; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub string_format { |
203
|
1
|
|
|
1
|
1
|
1795
|
my($str, $format) = @_; |
204
|
1
|
|
|
|
|
26
|
return sprintf $format, $str; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub strip { |
208
|
2
|
|
|
2
|
1
|
4929
|
my($str, $space) = @_; |
209
|
2
|
100
|
|
|
|
11
|
$space = ' ' if not defined $space; |
210
|
2
|
|
|
|
|
31
|
$str =~ s/\s+/$space/g; |
211
|
2
|
|
|
|
|
21
|
return $str; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub strip_tags { |
215
|
3
|
|
|
3
|
1
|
3277
|
my($str, $replace_with_space) = @_; |
216
|
3
|
100
|
|
|
|
10
|
$replace_with_space = 1 if not defined $replace_with_space; |
217
|
3
|
100
|
|
|
|
10
|
my $replace = $replace_with_space ? ' ' : ''; |
218
|
3
|
|
|
|
|
72
|
$str =~ s{ < [^>]* > }{$replace}xmsg; |
219
|
3
|
|
|
|
|
31
|
return $str; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
sub truncate { |
223
|
7
|
|
|
7
|
1
|
8122
|
my($str, $length, $etc, $break_words, $middle) = @_; |
224
|
7
|
100
|
|
|
|
22
|
$length = 80 if not defined $length; |
225
|
7
|
100
|
|
|
|
18
|
$etc = '...' if not defined $etc; |
226
|
|
|
|
|
|
|
|
227
|
7
|
100
|
|
|
|
17
|
if(length($str) <= $length) { |
228
|
1
|
|
|
|
|
8
|
return $str; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
6
|
|
|
|
|
33
|
$length -= min($length, length($etc)); |
232
|
|
|
|
|
|
|
|
233
|
6
|
100
|
|
|
|
19
|
if (!$middle) { |
234
|
5
|
100
|
|
|
|
16
|
if(!$break_words) { |
235
|
3
|
|
|
|
|
8
|
$str = substr($str, 0, $length + 1); |
236
|
3
|
|
|
|
|
30
|
$str =~ s/ \s+? (\S+)? \z//xmsg; |
237
|
|
|
|
|
|
|
} |
238
|
5
|
|
|
|
|
39
|
return substr($str, 0, $length) . $etc; |
239
|
|
|
|
|
|
|
} else { |
240
|
1
|
|
|
|
|
22
|
return substr($str, 0, $length / 2) . $etc . substr($str, - $length / 2); |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub upper { |
245
|
3
|
|
|
3
|
1
|
3254
|
my($str) = @_; |
246
|
3
|
|
|
|
|
56
|
return uc($str); |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
sub wordwrap { |
250
|
4
|
|
|
4
|
1
|
3955
|
my($str, $length, $break, $cut) = @_; |
251
|
4
|
50
|
|
|
|
12
|
$length = 80 if not defined $length; |
252
|
4
|
100
|
|
|
|
11
|
$break = "\n" if not defined $break; |
253
|
|
|
|
|
|
|
|
254
|
4
|
100
|
|
|
|
9
|
if(!$cut) { |
255
|
3
|
|
|
|
|
5
|
my @lines; |
256
|
3
|
|
|
|
|
5
|
my $line = ''; |
257
|
3
|
|
|
|
|
28
|
foreach my $word(split /(\s+)/, $str) { |
258
|
69
|
100
|
100
|
|
|
154
|
if(length($line) + length($word) > $length |
259
|
|
|
|
|
|
|
&& $word =~ /\S/) { |
260
|
7
|
|
|
|
|
23
|
$line =~ s/ \s+ \z//xms; # chomp the last spaces |
261
|
7
|
|
|
|
|
10
|
push @lines, $line; |
262
|
7
|
|
|
|
|
11
|
$line = $word; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
else { |
265
|
62
|
|
|
|
|
75
|
$line .= $word; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
3
|
50
|
|
|
|
13
|
if(length($line) > 0) { |
270
|
3
|
|
|
|
|
4
|
$line =~ s/ \s+ \z//xms; # chomp the last spaces |
271
|
3
|
|
|
|
|
6
|
push @lines, $line; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
3
|
|
|
|
|
13
|
return safe_join($break, @lines); |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
else { # force wrapping mode |
277
|
1
|
|
|
|
|
3
|
$length--; # What's it??? |
278
|
1
|
|
|
|
|
30
|
$str =~ s/(.{$length})/$1$break/xmsg; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
1
|
|
|
|
|
13
|
return $str; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
1; |
285
|
|
|
|
|
|
|
__END__ |