Template::Plugin::Filter::VisualTruncate - Filter Plugin for trimming text by the number of the columns of terminals and mobile phones.


Template-Plugin-Filter-VisualTruncate documentation Contained in the Template-Plugin-Filter-VisualTruncate distribution.

Index


Code Index:

NAME

Top

Template::Plugin::Filter::VisualTruncate - Filter Plugin for trimming text by the number of the columns of terminals and mobile phones.

SYNOPSIS

Top

Supported encodings on this module are UTF8, EUC-JP and system locale.

If your template was written in UTF8, then

    [% USE Filter.VisualTruncate 'utf8' %]
    [% row.comment | visual_truncate(20, '...') | html %]

or EUC-JP

    [% USE Filter.VisualTruncate 'euc-jp' %]
    [% row.comment | visual_truncate(20, '...') | html %]

or system locale

    [% USE Filter.VisualTruncate 'locale' %]
    [% row.comment | visual_truncate(20, '...') | html %]

If parameters are not specified explicitly...

    [% row.comment | visual_truncate() | html %]

default values is used.

    [% row.comment | visual_truncate(32, '...') | html %]

FUNCTIONS

Top

init

    Overrided method. See more detail Template::Plugin::Filter

filter

    Overrided method. See more detail Template::Plugin::Filter

SEE ALSO

Top

HTML::Filters, HTML::Plugin::Filter, Text::VisualWidth, Text::CharWidth

AUTHOR

Top

bokutin, <bokutin at cpan.org>

COPYRIGHT & LICENSE

Top


Template-Plugin-Filter-VisualTruncate documentation Contained in the Template-Plugin-Filter-VisualTruncate distribution.
package Template::Plugin::Filter::VisualTruncate;

use warnings;
use strict;

use base qw( Template::Plugin::Filter );

use UNIVERSAL::require;

our $VERSION = '0.05';

sub init {
    my ( $self, @args ) = @_;

    $self->{_DYNAMIC}      = 1;
    $self->install_filter('visual_truncate');

    my $name = $self->{_ARGS}->[0] || 'utf8';
    my $class;

    if ($name =~ m/^utf[-]{0,1}8$/i) {
        $class = "Template::Plugin::Filter::VisualTruncate::UTF8";
    }
    elsif ($name =~ m/^euc[-]{0,1}jp$/i) {
        $class = "Template::Plugin::Filter::VisualTruncate::EUC_JP";
    }
    elsif ($name =~ m/^locale$/i) {
        $class = "Template::Plugin::Filter::VisualTruncate::Locale";
    }
    else {
        die "such a encoding $name is unsupported.";
    }

    $class->require or die;
    $self->{obj} = $class->new;

    return $self;
}

#sub truncate_filter_factory {
#    my ($context, $len, $char) = @_;
#    $len = 32 unless defined $len;
#    $char = "..." unless defined $char;
#
#    return sub {
#        my $text = shift;
#        return $text if length $text <= $len;
#        return substr($text, 0, $len - length($char)) . $char;
#    }
#}

sub filter {
    my ($self, $text, $args, $config) = @_;

    my $utf8_bool = utf8::is_utf8($text);

    my $len  = $args->[0] || 32; # same at Template::Filters::truncate.
    my $tail = defined $args->[1] ? $args->[1] : "...";

    my $text_width = $self->{obj}->width($text);
    my $tail_width = $self->{obj}->width($tail);

    return $text if $text_width <= $len;

    return $self->{obj}->trim($tail, $len) if $tail_width >= $len;

    my $result = $self->{obj}->trim($text, $len - $tail_width) . $tail;

    if ($utf8_bool and ! utf8::is_utf8($result)) {
        utf8::decode($result);
    }

    return $result;
}

1; # End of Template::Plugin::Filter::VisualTruncate