File Coverage

blib/lib/MojoMojo/Formatter/RSS.pm
Criterion Covered Total %
statement 12 28 42.8
branch 2 12 16.6
condition 0 6 0.0
subroutine 5 6 83.3
pod 4 4 100.0
total 23 56 41.0


line stmt bran cond sub pod time code
1             package MojoMojo::Formatter::RSS;
2              
3 26     26   17108 use strict;
  26         64  
  26         764  
4 26     26   451 use parent 'MojoMojo::Formatter';
  26         257  
  26         135  
5              
6             eval {require LWP::Simple; require URI::Fetch; require XML::Feed};
7             my $dependencies_installed = !$@;
8              
9             =head2 module_loaded
10              
11             Return true if the module is loaded.
12              
13             =cut
14              
15 125     125 1 48610 sub module_loaded { $dependencies_installed }
16              
17             our $VERSION = '0.01';
18              
19             =head1 NAME
20              
21             MojoMojo::Formatter::RSS - Include RSS feeds on your page.
22              
23             =head1 DESCRIPTION
24              
25             This formatter takes a feed in the format {{feed <url>}}, and
26             passes it through L<XML::Feed> to get a formatted feed suitable
27             for inclusion in your page. It also caches them in the chosen
28             Catalyst cache. By default it will render the first element in
29             the feed, but it can take a numeric parameter to choose number of
30             elements.
31              
32             =head1 METHODS
33              
34             =head2 format_content_order
35              
36             Format order can be 1-99. The RSS formatter runs on 6, after the
37             L<Include|MojoMojo::Formatter::Include>), so that transcluding
38             a page from the wiki that brings in a feed, will display the feed
39             in the transcluded section as well.
40              
41             =cut
42              
43              
44 620     620 1 1826 sub format_content_order { 6 }
45              
46             =head2 format_content
47              
48             Calls the formatter. Takes a ref to the content as well as the
49             context object.
50              
51             =cut
52              
53             sub format_content {
54 124     124 1 943 my ( $class, $content, $c ) = @_;
55 124 50       549 return unless $class->module_loaded;
56             # Regexp::Common::URI is overkill
57 124         722 my $re = $class->gen_re(qr(
58             feed \s+ (\S+) # feed URL
59             (?: \s+ (\d+))? # optional maximum number of entries
60             )x);
61 124 50       1199 if ( $$content =~ s/$re/$class->include_rss( $c, $1, $2 )/eg ) {
  0            
62             # we don't want to precompile a page with comments so turn it off
63 0           $c->stash->{precompile_off} = 1;
64             }
65             }
66              
67             =head2 include_rss <c> <url> [<entries>]
68              
69             Returns HTML-formatted feed content for inclusion, up to a specified
70             number of entries. Will store a cached version in C<< $c->cache >>.
71              
72             =cut
73              
74             sub include_rss {
75 0     0 1   my ($class, $c, $url, $entries) = @_;
76 0   0       $entries ||= 1;
77 0           $url = URI->new($url);
78 0 0         return $c->loc('x is not a valid URL', $url) unless $url;
79            
80 0           my $result = URI::Fetch->fetch( $url, Cache => $c->cache );
81 0 0         return $c->loc('Could not retrieve x', $url)
82             if not defined $result;
83 0 0         my $feed = XML::Feed->parse(\$result->content) or
84             return $c->loc('Could not parse feed x', $url);
85            
86 0           my $count = 0;
87 0           my $content = '';
88 0           foreach my $entry ($feed->entries) {
89 0           $count++;
90 0   0       $content .= '<div class="feed">'
      0        
91             . '<h3><a href="'.$entry->link.'">'
92             . ($entry->title||"no title").'</a></h3>'
93             . ($entry->content->body||$entry->summary->body||"")."</div>\n";
94 0 0         return $content if $count==$entries;
95             }
96 0           return $content;
97             }
98              
99             =head1 SEE ALSO
100              
101             L<MojoMojo>, L<Module::Pluggable::Ordered>, L<XML::Feed>, L<URI::Fetch>
102              
103             =head1 AUTHORS
104              
105             Marcus Ramberg <mramberg@cpan.org>
106              
107             =head1 LICENSE
108              
109             This library is free software. You can redistribute it and/or modify
110             it under the same terms as Perl itself.
111              
112             =cut
113              
114             1;