From f938a52e47c9870e3c236bad1fbd9cb40ef8230f Mon Sep 17 00:00:00 2001 From: Fridolyn SOMERS Date: Wed, 27 Mar 2013 16:37:36 +0100 Subject: [PATCH] Bug 9579 - Incorrect display in truncated facets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a facet label is truncated and ends with a non-ASCII character, this label shows a (broken character). This patch moves facet label truncation from C4 to templates. It adds a new template filter TruncateByWord, see http://search.cpan.org/~kawabata/Template-Plugin-TruncateByWord-0.11/ Note that existing TT filter "truncate()" has the problem with unicode. Test plan : - Perform a search - Look for a facet label with a diacritic. For example "Médiathèque" - Set syspref FacetLabelTruncationLength so that facet label ends on a diacritic, in our example '8' - Look at facettes in intranet and opac => Facet label is correct. In our example : "Médiathè..." --- C4/Search.pm | 10 -- Koha/Template/Plugin/TruncateByWord.pm | 100 ++++++++++++++++++++ catalogue/search.pl | 1 + .../intranet-tmpl/prog/en/includes/facets.inc | 4 +- .../opac-tmpl/prog/en/includes/opac-facets.inc | 4 +- opac/opac-search.pl | 1 + 6 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 Koha/Template/Plugin/TruncateByWord.pm diff --git a/C4/Search.pm b/C4/Search.pm index d3d3ce1..370ff1e 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -573,17 +573,7 @@ sub getRecords { my $facet_link_value = $one_facet; $facet_link_value =~ s/[()!?¡¿؟]/ /g; - # fix the length that will display in the label, my $facet_label_value = $one_facet; - my $facet_max_length = C4::Context->preference( - 'FacetLabelTruncationLength') - || 20; - $facet_label_value = - substr( $one_facet, 0, $facet_max_length ) - . "..." - if length($facet_label_value) > - $facet_max_length; - # if it's a branch, label by the name, not the code, if ( $link_value =~ /branch/ ) { if ( defined $branches diff --git a/Koha/Template/Plugin/TruncateByWord.pm b/Koha/Template/Plugin/TruncateByWord.pm new file mode 100644 index 0000000..5799260 --- /dev/null +++ b/Koha/Template/Plugin/TruncateByWord.pm @@ -0,0 +1,100 @@ +# +# $Id: TruncateByWord.pm,v 1.3 2008/06/20 06:17:12 oneroad Exp $ +# +package Koha::Template::Plugin::TruncateByWord; + +use strict; +use warnings; + +our $VERSION = '0.11'; + +use Template::Plugin::Filter; +use base 'Template::Plugin::Filter'; + +use Encode; + +our $FILTER_NAME_DEFAULT = 'truncate_by_word'; +our $ORG_ENC_DEFAULT = 'utf8'; + +sub init { + my $self = shift; + $self->{_DYNAMIC} = 1; + $self->install_filter($self->{_CONFIG}->{name}||$FILTER_NAME_DEFAULT); + $self->{_CONFIG}->{enc} ||= $self->{_ARGS}->[0] || $ORG_ENC_DEFAULT; + return $self; +} + +sub filter { + my($self, $string, $args, $conf) = @_; + + return '' unless $string; + + # decode + my $org_enc; + unless ( utf8::is_utf8($string) ) { + $org_enc = $self->{_CONFIG}->{enc}; + $string = Encode::decode($org_enc, $string); + } + + my $org_length = CORE::length($string); + my $length = $args->[0] || $org_length; + return if $length =~ /\D/; + $string = CORE::substr($string, 0, $length); + + my $suffix = $args->[1]||''; + # revive encode + $string = Encode::encode($org_enc, $string) if $org_enc; + return $org_length > $length ? $string.$suffix : $string ; +} + +1; +__END__ + +=head1 NAME + +Template::Plugin::TruncateByWord - A Template Toolkit filter to truncate not the number of bytes but characters + +=head1 SYNOPSIS + + # result is 'ab' + [% USE TruncateByWord %] + [% 'abcdefg' | truncate_by_word(2) %] + + # result is 'abc....' + [% USE TruncateByWord %] + [% FILTER truncate_by_word(3,'....') %] + abcdefg + [% END %] + + # default charset = 'utf8'. you can change this. + # result is 'abcd' + [% USE TruncateByWord 'euc-jp' %] + [% FILTER truncate_by_word(4) %] + abcdefg + [% END %] + +=head1 DESCRIPTION + +Template::Plugin::TruncateByWord is a filter plugin for Template Toolkit which truncate text not the number of bytes but the number of characters. + +=head1 BUGS + +If found, please Email me. I tested utf8, euc-jp, shiftjis, 7bit-jis, big5, and euc-kr. Please send me more test cases. + +=head1 SEE ALSO + +L