> I will make changes to the filter as well, and post proper diffs for
> both of them.
> 

OK - here is a diff with my suggested changes made to both
Template::Filter::truncate_filter_factory and
Template::Plugin::String::truncate;

It doesn't break or alter the previous behaviour, but instead turns on
the new functionality if you add a third parameter:

> 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.
> 


Re Nic Gibson:

> Whilst it won't break any existing code it modifies the semantics of  
> the existing filter.
> What I mean is that you have changed the meaning of the existing  
> parameter when the
> new parameter is supplied because whitespace has nothing to do with  
> the truncate filter
> at present.  When $min is supplied, $max means something different.
> 
> I don't think that overloading the semantics in this way is a good  
> idea at all.
> 
> How about truncate_words as a new filter that does what you are  
> suggesting?
> 
I see what you're saying, but I'm not quite in agreement. 
1) For me, truncate should ALWAYS have done this, so (again, for me) it
isn't a change in functionality
2) The meaning of max doesn't change.  Max is still max.  The only
difference is whether it is allowed to break on whitespace or not.

I do agree that specifying a minimum would not be the obvious way to
trigger this functionality.  I misused it because I needed to flag
"break on whitespace", and also needed a minimum length so that it
wouldn't break stupidly right at the beginning if it ended with a long
word. Combining the two seemed a nice shortcut.  But maybe not as
obvious as it should be.

So perhaps you are right about using its own name.

Anyway, here's the diff:
===============================================================

diff -ruN Template/Filters.pm Template_new/Filters.pm
--- Template/Filters.pm 2006-05-26 14:21:24.000000000 +0200
+++ Template_new/Filters.pm     2007-03-28 19:21:25.000000000 +0200
@@ -483,14 +483,22 @@
 #------------------------------------------------------------------------
 
 sub truncate_filter_factory {
-    my ($context, $len, $char) = @_;
-    $len = 32 unless defined $len;
-    $char = "..." unless defined $char;
+    my ($context, $max, $suffix, $min) = @_;
+    $max = 32 unless defined $max;
+    $suffix = "..." unless defined $suffix;
 
     return sub {
         my $text = shift;
-        return $text if length $text <= $len;
-        return substr($text, 0, $len - length($char)) . $char;
+        return $text if length $text <= $max;
+
+        my $inc_max = $max - length($suffix);
+        $inc_max = $max if $max < 0;
+
+        unless ($min && $text=~s/^(.{$min,$inc_max})\s.*/$1/e) {
+            $text = substr($text,0,$inc_max);
+        }
+
+        return $text.$suffix;
     }
 }
 
diff -ruN Template/Plugin/String.pm Template_new/Plugin/String.pm
--- Template/Plugin/String.pm   2006-05-26 14:21:25.000000000 +0200
+++ Template_new/Plugin/String.pm       2007-03-28 19:21:54.000000000 +0200
@@ -309,14 +309,22 @@
 
 
 sub truncate {
-    my ($self, $length, $suffix) = @_;
-    return $self unless defined $length;
-    $suffix ||= '';
-    return $self if CORE::length $self->{ text } <= $length;
-    $self->{ text } = CORE::substr($self->{ text }, 0, 
-                            $length - CORE::length($suffix)) . $suffix;
+    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;
-}
+} 
 
 
 sub substr {

=========================================================


_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to