[Bast-commits] r9169 - in ironman/branches/mk-ii/Perlanet-IronMan: . lib/Perlanet

aCiD2 at dev.catalyst.perl.org aCiD2 at dev.catalyst.perl.org
Wed Apr 14 21:52:56 GMT 2010


Author: aCiD2
Date: 2010-04-14 22:52:56 +0100 (Wed, 14 Apr 2010)
New Revision: 9169

Modified:
   ironman/branches/mk-ii/Perlanet-IronMan/Makefile.PL
   ironman/branches/mk-ii/Perlanet-IronMan/lib/Perlanet/IronMan.pm
Log:
Use HTML::Truncate for truncation

Modified: ironman/branches/mk-ii/Perlanet-IronMan/Makefile.PL
===================================================================
--- ironman/branches/mk-ii/Perlanet-IronMan/Makefile.PL	2010-04-14 21:52:08 UTC (rev 9168)
+++ ironman/branches/mk-ii/Perlanet-IronMan/Makefile.PL	2010-04-14 21:52:56 UTC (rev 9169)
@@ -4,6 +4,7 @@
 
 requires 'IronMan::Schema';
 requires 'Perlanet';
+requires 'HTML::Truncate';
 
 auto_install;
 

Modified: ironman/branches/mk-ii/Perlanet-IronMan/lib/Perlanet/IronMan.pm
===================================================================
--- ironman/branches/mk-ii/Perlanet-IronMan/lib/Perlanet/IronMan.pm	2010-04-14 21:52:08 UTC (rev 9168)
+++ ironman/branches/mk-ii/Perlanet-IronMan/lib/Perlanet/IronMan.pm	2010-04-14 21:52:56 UTC (rev 9169)
@@ -6,6 +6,7 @@
 
 use Moose;
 use IronMan::Schema;
+use HTML::Truncator;
 use Perlanet::Feed;
 use Data::Dumper;
 use Try::Tiny;
@@ -43,6 +44,11 @@
     lazy_build => 1,
 );
 
+has 'truncator' => (
+    is         => 'rw',
+    lazy_build => 1
+);
+
 =head2 select_entries
 
 The select entries function takes an array of Perlanet::Feed objects and
@@ -93,7 +99,7 @@
                 next;
             }
 
-            # Create a Perlanet::Entry object from the XML data retrieved           
+            # Create a Perlanet::Entry object from the XML data retrieved
             my $entry = Perlanet::Entry->new(
                             _entry => $xml_entry,
                             feed   => $feed
@@ -128,11 +134,15 @@
         # This should probably be in config rather than hard coded.
         my $summary = $post->_entry->summary->body || $post->_entry->content->body;
 
-        # Limit the summary text length.  This might be better as a word regexp.
-        if(length($summary) > 250) {
-            $summary = substr($summary, 0, 250) . " [...]"
+
+        my $truncated = eval { $self->truncator->truncate($summary) };
+        if ($@) {
+            warn "Truncate failed: $@";
+            $truncated = $summary;
         }
 
+        $summary = $truncated;
+
         #print(Dumper($post));
         #exit;
 
@@ -166,7 +176,7 @@
 
     #print(Dumper($post));
 
-    } 
+    }
 };
 
 =head2 save
@@ -214,6 +224,21 @@
     );
 }
 
+=head2 _build_truncator
+
+Construct a HTML::Truncator object for truncating posts
+
+=cut
+
+sub _build_truncator {
+    my $self = shift;
+    my $html_truncate = HTML::Truncate->new(repair=>1);
+    $html_truncate->chars($ length);
+    $html_truncate->ellipsis(" [...]");
+
+    return $html_truncate;
+}
+
 =head2 _filter_entry_for_duplicate
 
 Test to see if the supplied XML::Feed::Entry passes the configured filters.
@@ -275,7 +300,7 @@
                 return 1;
             }
         }
- 
+
         # Check the title if defined
         if(defined($xml_entry->title)) {
             if(grep(/$filter/i, $xml_entry->title)) {




More information about the Bast-commits mailing list