[Jifty-commit] r6288 - in jifty/trunk: lib/Jifty/Plugin lib/Jifty/Plugin/NewsFeed

Jifty commits jifty-commit at lists.jifty.org
Tue Feb 3 08:11:13 EST 2009


Author: c9s
Date: Tue Feb  3 08:11:12 2009
New Revision: 6288

Added:
   jifty/trunk/lib/Jifty/Plugin/NewsFeed/
   jifty/trunk/lib/Jifty/Plugin/NewsFeed.pm
   jifty/trunk/lib/Jifty/Plugin/NewsFeed/View.pm
Modified:
   jifty/trunk/   (props changed)

Log:
 r7071 at Oulixeus:  c9s | 2009-02-03 18:59:51 +0800
  - Newsfeed plugin 


Added: jifty/trunk/lib/Jifty/Plugin/NewsFeed.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/NewsFeed.pm	Tue Feb  3 08:11:12 2009
@@ -0,0 +1,25 @@
+use warnings;
+use strict;
+package Jifty::Plugin::NewsFeed;
+use base qw'Jifty::Plugin';
+
+=head1 NAME
+
+Jifty::Plugin::NewsFeed - Provide site news by feeds in your app
+
+=head1 SYNOPSIS
+
+# In your jifty config.yml under the framework section:
+
+  Plugins:
+    - NewsFeed: {}
+
+
+=head1 DESCRIPTION
+
+Provides a way to include site news feeds in your Jifty app
+
+=cut
+
+
+1;

Added: jifty/trunk/lib/Jifty/Plugin/NewsFeed/View.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/NewsFeed/View.pm	Tue Feb  3 08:11:12 2009
@@ -0,0 +1,119 @@
+use warnings;
+use strict;
+
+package Jifty::Plugin::NewsFeed::View;
+use Jifty::View::Declare -base;
+
+=head1 NAME
+
+Jifty::Plugin::SiteNews::View - /newsfeed pages for your app
+
+=head1 DESCRIPTION
+
+The tempaltes for L<Jifty::Plugin::NewsFeed>
+
+=cut
+
+template 'feedpage' => page { title => "Feed" } 
+content {
+    h1 { "I am NEWS FEED PLUGIN!" } ;
+};
+
+=head1 Templates
+
+=head2 display_feed feed_url , {  title => TITLE , before => BEFORE , cache_ttl => TTL  , ... } 
+
+display syndication feed
+
+=head3 options
+
+=over 4
+
+=item title => I<string>  I<(optional)>
+
+C<TITLE> , which overrides the title of feed.
+
+=item before => I<string>  I<(optional)>
+
+C<BEFORE>, is a range before current date, display feed items which were created before C<BEFORE> ago.
+C<BEFORE> could be C<"7 days"> , C<"2 weeks"> , C<"1 months">
+
+=item cache_ttl => I<integer>   I<(optional)>
+
+C<TTL> (Time to live) for App::Cache.
+
+=item hide_title => I<boolean>  I<(optional)>
+
+hide feed title
+
+=item max_items => I<integer>  I<(optional)>
+
+max items to display.
+
+=item order => I<string>  I<(optional)>
+
+order feed items by date, 'DESC' or 'ASC'
+
+=cut
+
+template 'display_feed' => sub {
+    my ( $self , $feed_url ) = @_;
+    my $options = shift || {};
+
+    return unless ( $feed_url =~ m(^https?://) );
+
+    use XML::Feed;
+    use App::Cache;
+    use Encode qw(decode_utf8);
+
+    my $cache = App::Cache->new({ ttl =>  $options->{cache_ttl} || 60*60   });
+    my $feed_xml = $cache->get_url( $feed_url );
+
+    my $feed = XML::Feed->parse ( \$feed_xml );
+
+    unless ( $feed ) {
+      h3 { "Can't Parse Feed" };
+      div { { class is 'feed-error' };
+          outs_raw( decode_utf8(XML::Feed->errstr) );
+      };
+      return;
+    }
+
+    my $feed_title = decode_utf8 ( $feed->title );
+
+    # filter
+    my @entries = $feed->entries;
+    if( defined $options->{before} ) {
+        my $before = $options->{before};
+        my ( $num , $slice ) = ( $before =~ m/^(\d+)\s*(\w+)$/ );
+        if( $num and $slice )  {
+            my $theday = DateTime->now->subtract( $slice => $num );
+            @entries = grep {  $_->issued > $theday  } @entries 
+                                                            if ( $theday );
+        } 
+    }
+
+    splice @entries,0,scalar @entries
+            if( defined $options->{max} );
+
+    @entries = reverse @entries 
+            if( defined $options->{order} and $options->{order} eq 'ASC' );
+
+    h2 { { class is 'feed-title' } ; $feed_title } 
+            unless ( defined $options->{hide_title} );
+
+    ul { { class is 'feed-entries' } ;
+        for my $entry ( @entries ) {
+            my $issued = $entry->issued;
+            my $title = decode_utf8 ( $entry->title );
+            my $summary = decode_utf8( $entry->summary );
+            my $link  = $entry->link;
+
+            li { { class is 'feed-entry' }; 
+                outs_raw (qq|<a class="feed-link" href="$link">$title</a>|); } 
+                    if ( $title );
+        }
+    };
+};
+
+1;


More information about the Jifty-commit mailing list