[Jifty-commit] r6290 - in jifty/trunk: . plugins/NewsFeed plugins/NewsFeed/lib plugins/NewsFeed/lib/Jifty plugins/NewsFeed/lib/Jifty/Plugin/NewsFeed
Jifty commits
jifty-commit at lists.jifty.org
Tue Feb 3 08:11:50 EST 2009
Author: c9s
Date: Tue Feb 3 08:11:49 2009
New Revision: 6290
Added:
jifty/trunk/plugins/NewsFeed/
jifty/trunk/plugins/NewsFeed/Makefile.PL
jifty/trunk/plugins/NewsFeed/lib/
jifty/trunk/plugins/NewsFeed/lib/Jifty/
jifty/trunk/plugins/NewsFeed/lib/Jifty/Plugin/
jifty/trunk/plugins/NewsFeed/lib/Jifty/Plugin/NewsFeed/
jifty/trunk/plugins/NewsFeed/lib/Jifty/Plugin/NewsFeed.pm
jifty/trunk/plugins/NewsFeed/lib/Jifty/Plugin/NewsFeed/View.pm
Modified:
jifty/trunk/ (props changed)
Log:
r7074 at Oulixeus: c9s | 2009-02-03 21:01:48 +0800
- move NewsFeed plugin to /plugins
Added: jifty/trunk/plugins/NewsFeed/Makefile.PL
==============================================================================
--- (empty file)
+++ jifty/trunk/plugins/NewsFeed/Makefile.PL Tue Feb 3 08:11:49 2009
@@ -0,0 +1,9 @@
+use inc::Module::Install 0.46;
+name('Jifty-Plugin-NewsFeed');
+version_from('lib/Jifty/Plugin/NewsFeed.pm');
+requires ( 'URI' => 1.31 );
+requires ( 'App::Cache' => 0 );
+requires ( 'XML::Feed' => 0 );
+requires ( 'Encode' => 0 );
+&auto_install();
+WriteAll;
Added: jifty/trunk/plugins/NewsFeed/lib/Jifty/Plugin/NewsFeed.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/plugins/NewsFeed/lib/Jifty/Plugin/NewsFeed.pm Tue Feb 3 08:11:49 2009
@@ -0,0 +1,24 @@
+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 templates to include site news feeds in your Jifty app. See L<Jifty::Plugin::NewsFeed::View>
+
+=cut
+
+1;
Added: jifty/trunk/plugins/NewsFeed/lib/Jifty/Plugin/NewsFeed/View.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/plugins/NewsFeed/lib/Jifty/Plugin/NewsFeed/View.pm Tue Feb 3 08:11:49 2009
@@ -0,0 +1,180 @@
+use warnings;
+use strict;
+
+package Jifty::Plugin::NewsFeed::View;
+use Jifty::View::Declare -base;
+
+=head1 NAME
+
+Jifty::Plugin::NewsFeed::View - template for feed
+
+=head1 DESCRIPTION
+
+The templates for L<Jifty::Plugin::NewsFeed>
+
+=head1 SYNOPSIS
+
+ div { { class is 'feed-wrapper' };
+ show 'display_feed' , 'http://example.com/feed/default' , { before => '7days' };
+ };
+
+=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'
+
+=item style => I<string> I<(optional)>
+
+style could be 'list', 'p' , 'none' , default is 'list'.
+
+=cut
+
+template 'display_feed' => sub {
+ my $self = shift;
+ my $feed_url = shift;
+ my $options = shift || {};
+
+ return unless ( $feed_url and $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 = $options->{title} || decode_utf8 ( $feed->title );
+
+ my @entries = $feed->entries;
+
+ return unless ( @entries );
+
+ # filter entry
+ 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 );
+ }
+ }
+
+ @entries = splice @entries,0,$options->{max_items}
+ if( defined $options->{max_items} );
+
+ @entries = reverse @entries
+ if( defined $options->{order} and $options->{order} eq 'ASC' );
+
+ my $style = $options->{style} || 'list';
+
+ h2 { { class is 'feed-title' } ; $feed_title }
+ unless ( defined $options->{hide_title} );
+
+ if( $style eq 'list' ) {
+ show 'feed_list_style',\@entries;
+ }
+ elsif( $style eq 'p' ) {
+ show 'feed_p_style',\@entries;
+ }
+ elsif( $style eq 'none' ) {
+ show 'feed_none_style',\@entries;
+ }
+ else {
+ show 'feed_list_style',\@entries;
+ }
+
+};
+
+template 'feed_list_style' => sub {
+ my $self = shift;
+ my @entries = @{ +shift };
+ 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 );
+ }
+ };
+};
+
+template 'feed_p_style' => sub {
+ my $self = shift;
+ my @entries = @{ +shift };
+ for my $entry ( @entries ) {
+ my $issued = $entry->issued;
+ my $title = decode_utf8 ( $entry->title );
+ my $summary = decode_utf8( $entry->summary );
+ my $link = $entry->link;
+
+ p { { class is 'feed-entry' };
+ outs_raw (qq|<a class="feed-link" href="$link">$title</a>|); }
+ if ( $title );
+ }
+};
+
+template 'feed_none_style' => sub {
+ my $self = shift;
+ my @entries = @{ +shift };
+ for my $entry ( @entries ) {
+ my $issued = $entry->issued;
+ my $title = decode_utf8 ( $entry->title );
+ my $summary = decode_utf8( $entry->summary );
+ my $link = $entry->link;
+
+ span { { 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