Attached patch fixes the problem, but the handling of the punctuation
clean might be a little nasty.
Tony
From 308b7e25190ea0abc94383fe8e065d8a0116d633 Mon Sep 17 00:00:00 2001
From: Tony Cook <tony@develop-help.com>
Date: Tue, 29 Mar 2011 20:06:52 +1100
Subject: [PATCH] sort of fix breaking final entitites
---
lib/HTML/Truncate.pm | 5 +++--
t/entity.t | 24 ++++++++++++++++++++++++
2 files changed, 27 insertions(+), 2 deletions(-)
create mode 100644 t/entity.t
diff --git a/lib/HTML/Truncate.pm b/lib/HTML/Truncate.pm
index 4a80198..4423900 100644
--- a/lib/HTML/Truncate.pm
+++ b/lib/HTML/Truncate.pm
@@ -102,7 +102,7 @@ sub new {
{
_chars => 100,
_percent => undef,
- _cleanly => qr/[\s[:punct:]]+\z/,
+ _cleanly => qr/(?!;)[\s[:punct:]]+\z/,
_on_space => undef,
_utf8_mode => undef,
_ellipsis => '…',
@@ -426,7 +426,8 @@ sub truncate {
}
else
{
- $keep = substr($txt, 0, $chars_to_keep);
+ #$keep = substr($txt, 0, $chars_to_keep);
+ ($keep) = $txt =~ /\A((?:[^&]|&[^;]+;){0,$chars_to_keep})/;
}
if ( my $cleaner = $self->cleanly )
diff --git a/t/entity.t b/t/entity.t
new file mode 100644
index 0000000..a82bfe3
--- /dev/null
+++ b/t/entity.t
@@ -0,0 +1,24 @@
+#!perl
+use strict;
+use Test::More tests => 5;
+
+use HTML::Truncate;
+
+my $text = 'test "x"';
+
+my $trunc = HTML::Truncate->new;
+
+ok($trunc, "Make truncate object");
+
+is($trunc->truncate($text, 5), "test…",
+ "truncate to 5");
+
+is($trunc->truncate($text, 6), "test "…",
+ "truncate to 6");
+
+is($trunc->truncate($text, 7), "test "x…",
+ "truncate to 7");
+
+is($trunc->truncate($text, 8), "test "x"…",
+ "truncate to 8");
+
--
1.7.2.5