Hi all
The truncate filter doesn't do what I would like it to do.
I would prefer if it broke on whitespace, rather than at the exact
length that I specify.
So I'd prefer : "the cat sat on the..."
to : "the cat sat on the m..."
However, I'd prefer : "I said supercalafragalisticexpial..."
to : "I said..."
I propose adding an extra parameter on the end : $min. So if it can
break on white space and still return a string longer than $min, then it
does so, and if not, then it breaks on $max.
If $min is not specified, then the behaviour will be as before. So this
change will be backwards compatible, and won't break any existing code:
sub truncate {
my ($self,$max,$suffix,$min) = @_;
return $self unless defined $max;
$suffix||='';
my $string = $self->{ text };
return $self if CORE::length $string < $max;
$max-=length($suffix);
$max = 0 if $max < 0;
unless ($min && $string=~s/^(.{$min,$max})\s.*/$1/e) {
$string = CORE::substr($string,0,$max);
}
$self-> { text } = $string.$suffix;
return $self;
}
The test output for this code:
------------------------------
foreach my $min (reverse 1..35) {
my $self = {text=>'word woooooooooooord word word'};
my $min = $max - 7;
truncate($self,$max,'...',$min);
printf ("%2d : %s\n",$max,$self->{text});
}
Is as follows:
--------------
35 : word woooooooooooord word word
34 : word woooooooooooord word word
33 : word woooooooooooord word word
32 : word woooooooooooord word word
31 : word woooooooooooord word word
30 : word woooooooooooord word...
29 : word woooooooooooord word...
28 : word woooooooooooord word...
27 : word woooooooooooord...
26 : word woooooooooooord...
25 : word woooooooooooord...
24 : word woooooooooooord...
23 : word woooooooooooord...
22 : word woooooooooooor...
21 : word woooooooooooo...
20 : word wooooooooooo...
19 : word woooooooooo...
18 : word wooooooooo...
17 : word woooooooo...
16 : word wooooooo...
15 : word woooooo...
14 : word wooooo...
13 : word woooo...
12 : word wooo...
11 : word...
10 : word...
9 : word...
8 : word...
7 : word...
6 : wor...
5 : wo...
4 : w...
3 : ...
2 : ...
1 : ...
Clint
_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates