| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Template::Plugin::Filter::VisualTruncate; |
|
2
|
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
46405
|
use warnings; |
|
|
5
|
|
|
|
|
11
|
|
|
|
5
|
|
|
|
|
203
|
|
|
4
|
5
|
|
|
5
|
|
31
|
use strict; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
216
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
29
|
use base qw( Template::Plugin::Filter ); |
|
|
5
|
|
|
|
|
17
|
|
|
|
5
|
|
|
|
|
1106
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
58405
|
use UNIVERSAL::require; |
|
|
5
|
|
|
|
|
9798
|
|
|
|
5
|
|
|
|
|
58
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Template::Plugin::Filter::VisualTruncate - Filter Plugin for trimming text by the number of the columns of terminals and mobile phones. |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Supported encodings on this module are UTF8, EUC-JP and system locale. |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
If your template was written in UTF8, then |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
[% USE Filter.VisualTruncate 'utf8' %] |
|
25
|
|
|
|
|
|
|
[% row.comment | visual_truncate(20, '...') | html %] |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
or EUC-JP |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
[% USE Filter.VisualTruncate 'euc-jp' %] |
|
30
|
|
|
|
|
|
|
[% row.comment | visual_truncate(20, '...') | html %] |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
or system locale |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
[% USE Filter.VisualTruncate 'locale' %] |
|
35
|
|
|
|
|
|
|
[% row.comment | visual_truncate(20, '...') | html %] |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
If parameters are not specified explicitly... |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
[% row.comment | visual_truncate() | html %] |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
default values is used. |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
[% row.comment | visual_truncate(32, '...') | html %] |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 init |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Overrided method. See more detail Template::Plugin::Filter |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub init { |
|
54
|
4
|
|
|
4
|
1
|
324270
|
my ( $self, @args ) = @_; |
|
55
|
|
|
|
|
|
|
|
|
56
|
4
|
|
|
|
|
224
|
$self->{_DYNAMIC} = 1; |
|
57
|
4
|
|
|
|
|
1255
|
$self->install_filter('visual_truncate'); |
|
58
|
|
|
|
|
|
|
|
|
59
|
4
|
|
100
|
|
|
238
|
my $name = $self->{_ARGS}->[0] || 'utf8'; |
|
60
|
4
|
|
|
|
|
8
|
my $class; |
|
61
|
|
|
|
|
|
|
|
|
62
|
4
|
100
|
|
|
|
44
|
if ($name =~ m/^utf[-]{0,1}8$/i) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
6
|
$class = "Template::Plugin::Filter::VisualTruncate::UTF8"; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
elsif ($name =~ m/^euc[-]{0,1}jp$/i) { |
|
66
|
1
|
|
|
|
|
3
|
$class = "Template::Plugin::Filter::VisualTruncate::EUC_JP"; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
elsif ($name =~ m/^locale$/i) { |
|
69
|
1
|
|
|
|
|
3
|
$class = "Template::Plugin::Filter::VisualTruncate::Locale"; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
else { |
|
72
|
0
|
|
|
|
|
0
|
die "such a encoding $name is unsupported."; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
4
|
50
|
|
|
|
59
|
$class->require or die; |
|
76
|
4
|
|
|
|
|
213
|
$self->{obj} = $class->new; |
|
77
|
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
24
|
return $self; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
#sub truncate_filter_factory { |
|
82
|
|
|
|
|
|
|
# my ($context, $len, $char) = @_; |
|
83
|
|
|
|
|
|
|
# $len = 32 unless defined $len; |
|
84
|
|
|
|
|
|
|
# $char = "..." unless defined $char; |
|
85
|
|
|
|
|
|
|
# |
|
86
|
|
|
|
|
|
|
# return sub { |
|
87
|
|
|
|
|
|
|
# my $text = shift; |
|
88
|
|
|
|
|
|
|
# return $text if length $text <= $len; |
|
89
|
|
|
|
|
|
|
# return substr($text, 0, $len - length($char)) . $char; |
|
90
|
|
|
|
|
|
|
# } |
|
91
|
|
|
|
|
|
|
#} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 filter |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Overrided method. See more detail Template::Plugin::Filter |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=cut |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub filter { |
|
100
|
36
|
|
|
36
|
1
|
313731
|
my ($self, $text, $args, $config) = @_; |
|
101
|
|
|
|
|
|
|
|
|
102
|
36
|
|
|
|
|
183
|
my $utf8_bool = utf8::is_utf8($text); |
|
103
|
|
|
|
|
|
|
|
|
104
|
36
|
|
100
|
|
|
205
|
my $len = $args->[0] || 32; # same at Template::Filters::truncate. |
|
105
|
36
|
100
|
|
|
|
148
|
my $tail = defined $args->[1] ? $args->[1] : "..."; |
|
106
|
|
|
|
|
|
|
|
|
107
|
36
|
|
|
|
|
281
|
my $text_width = $self->{obj}->width($text); |
|
108
|
36
|
|
|
|
|
413
|
my $tail_width = $self->{obj}->width($tail); |
|
109
|
|
|
|
|
|
|
|
|
110
|
36
|
100
|
|
|
|
252
|
return $text if $text_width <= $len; |
|
111
|
|
|
|
|
|
|
|
|
112
|
33
|
100
|
|
|
|
119
|
return $self->{obj}->trim($tail, $len) if $tail_width >= $len; |
|
113
|
|
|
|
|
|
|
|
|
114
|
27
|
|
|
|
|
156
|
my $result = $self->{obj}->trim($text, $len - $tail_width) . $tail; |
|
115
|
|
|
|
|
|
|
|
|
116
|
27
|
50
|
66
|
|
|
660
|
if ($utf8_bool and ! utf8::is_utf8($result)) { |
|
117
|
0
|
|
|
|
|
0
|
utf8::decode($result); |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
|
|
120
|
27
|
|
|
|
|
189
|
return $result; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
L, L, L, L |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 AUTHOR |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
bokutin, C<< >> |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Copyright 2007 bokutin, all rights reserved. |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
136
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
1; # End of Template::Plugin::Filter::VisualTruncate |