| 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 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 6 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Template::Plugin::Filter; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use base 'Template::Plugin::Filter'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 6 | our $FILTER_NAME_DEFAULT = 'truncate_by_word'; | |||||||||||||||||||||||||||||||||
| 17 | 6 | our $ORG_ENC_DEFAULT = 'utf8'; | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 54 | ▶ | sub init { | ||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 20 | 54 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 21 | 54 | $self->{_DYNAMIC} = 1; | |||||||||||||||||||||||||||||||||
| 22 | 54 | ▶ | $self->install_filter($self->{_CONFIG}->{name}||$FILTER_NAME_DEFAULT); | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'_CONFIG'}{'name'} || $Template::Plugin::TruncateByWord::FILTER_NAME_DEFAULT
MC/DC0%0/2
$self->{'_CONFIG'}{'name'} || $Template::Plugin::TruncateByWord::FILTER_NAME_DEFAULT
$self->{'_CONFIG'}{'name'}
$Template::Plugin::TruncateByWord::FILTER_NAME_DEFAULT
Truth table66%2/3
A || B
A= $self->{'_CONFIG'}{'name'}B= $Template::Plugin::TruncateByWord::FILTER_NAME_DEFAULT
| |||||||||||||||||||||||||||||||||||
| 23 | 54 | ▶ | $self->{_CONFIG}->{enc} ||= $self->{_ARGS}->[0] || $ORG_ENC_DEFAULT; | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
$self->{"_CONFIG"}{"enc"} ||= $self->{"_ARGS"}[0] || $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
$self->{'_ARGS'}[0] || $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
MC/DC0%0/3
$self->{"_CONFIG"}{"enc"} ||= $self->{"_ARGS"}[0] || $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
$self->{"_CONFIG"}{"enc"}
$self->{'_ARGS'}[0]
$Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
Truth table0%0/4
A ||= B || C
A= $self->{"_CONFIG"}{"enc"}B= $self->{'_ARGS'}[0]C= $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
| |||||||||||||||||||||||||||||||||||
| 24 | 54 | return $self; | |||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | 54 | ▶ | sub filter { | ||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 28 | 54 | my($self, $string, $args, $conf) = @_; | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||
| 30 | 54 | ▶ | return '' unless $string; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | # decode | ||||||||||||||||||||||||||||||||||
| 33 | 54 | my $org_enc; | |||||||||||||||||||||||||||||||||
| 34 | 54 | ▶ | unless ( utf8::is_utf8($string) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 35 | 54 | $org_enc = $self->{_CONFIG}->{enc}; | |||||||||||||||||||||||||||||||||
| 36 | 54 | $string = Encode::decode($org_enc, $string); | |||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 54 | my $org_length = CORE::length($string); | |||||||||||||||||||||||||||||||||
| 40 | 54 | ▶ | my $length = $args->[0] || $org_length; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args->[0] || $org_length
MC/DC0%0/2
$args->[0] || $org_length
$args->[0]
$org_length
Truth table66%2/3
A || B
A= $args->[0]B= $org_length
| |||||||||||||||||||||||||||||||||||
| 41 | 54 | ▶ | return if $length =~ /\D/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 42 | 54 | $string = CORE::substr($string, 0, $length); | |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||
| 44 | 54 | ▶ | my $suffix = $args->[1]||''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->[1] || ''
MC/DC100%1/1
$args->[1] || ''
$args->[1]
Truth table100%2/2
A
A= $args->[1]
| |||||||||||||||||||||||||||||||||||
| 45 | # revive encode | ||||||||||||||||||||||||||||||||||
| 46 | 54 | ▶ | $string = Encode::encode($org_enc, $string) if $org_enc; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 54 | ▶ | return $org_length > $length ? $string.$suffix : $string ; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | 6 | 1; | |||||||||||||||||||||||||||||||||
| 51 | __END__ | ||||||||||||||||||||||||||||||||||