line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id: TruncateByWord.pm,v 1.3 2008/06/20 06:17:12 oneroad Exp $ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
package Template::Plugin::TruncateByWord; |
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
638736
|
use strict; |
|
6
|
|
|
|
|
19
|
|
|
6
|
|
|
|
|
278
|
|
7
|
6
|
|
|
6
|
|
36
|
use warnings; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
297
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
10
|
|
|
|
|
|
|
|
11
|
6
|
|
|
6
|
|
7308
|
use Template::Plugin::Filter; |
|
6
|
|
|
|
|
20157
|
|
|
6
|
|
|
|
|
208
|
|
12
|
6
|
|
|
6
|
|
45
|
use base 'Template::Plugin::Filter'; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
530
|
|
13
|
|
|
|
|
|
|
|
14
|
6
|
|
|
6
|
|
8601
|
use Encode; |
|
6
|
|
|
|
|
90724
|
|
|
6
|
|
|
|
|
2307
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $FILTER_NAME_DEFAULT = 'truncate_by_word'; |
17
|
|
|
|
|
|
|
our $ORG_ENC_DEFAULT = 'utf8'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub init { |
20
|
54
|
|
|
54
|
0
|
804919
|
my $self = shift; |
21
|
54
|
|
|
|
|
178
|
$self->{_DYNAMIC} = 1; |
22
|
54
|
|
66
|
|
|
12540
|
$self->install_filter($self->{_CONFIG}->{name}||$FILTER_NAME_DEFAULT); |
23
|
54
|
|
66
|
|
|
2649
|
$self->{_CONFIG}->{enc} ||= $self->{_ARGS}->[0] || $ORG_ENC_DEFAULT; |
|
|
|
66
|
|
|
|
|
24
|
54
|
|
|
|
|
143
|
return $self; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub filter { |
28
|
54
|
|
|
54
|
0
|
4188
|
my($self, $string, $args, $conf) = @_; |
29
|
|
|
|
|
|
|
|
30
|
54
|
50
|
|
|
|
178
|
return '' unless $string; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# decode |
33
|
54
|
|
|
|
|
84
|
my $org_enc; |
34
|
54
|
50
|
|
|
|
207
|
unless ( utf8::is_utf8($string) ) { |
35
|
54
|
|
|
|
|
124
|
$org_enc = $self->{_CONFIG}->{enc}; |
36
|
54
|
|
|
|
|
297
|
$string = Encode::decode($org_enc, $string); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
54
|
|
|
|
|
53679
|
my $org_length = CORE::length($string); |
40
|
54
|
|
66
|
|
|
213
|
my $length = $args->[0] || $org_length; |
41
|
54
|
50
|
|
|
|
216
|
return if $length =~ /\D/; |
42
|
54
|
|
|
|
|
227
|
$string = CORE::substr($string, 0, $length); |
43
|
|
|
|
|
|
|
|
44
|
54
|
|
100
|
|
|
263
|
my $suffix = $args->[1]||''; |
45
|
|
|
|
|
|
|
# revive encode |
46
|
54
|
50
|
|
|
|
253
|
$string = Encode::encode($org_enc, $string) if $org_enc; |
47
|
54
|
100
|
|
|
|
11560
|
return $org_length > $length ? $string.$suffix : $string ; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
51
|
|
|
|
|
|
|
__END__ |