Plagger/Plugin/Publish/Feed.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 205 CC 21 SCAR 49.4
CC
21
Cov
0%
publish_feed
272.0
make_author
20.0
plugin_init
12.0
CRAP 140.2
1 package Plagger::Plugin::Publish::Feed;
2  
3 use strict;
4 use base qw( Plagger::Plugin );
5  
6 use XML::Feed;
7 use XML::Feed::Entry;
8 use XML::Feed::RSS; # load explicitly to force LibXML
9 use XML::RSS::LibXML;
10 use File::Spec;
11  
12 $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
13  
14 sub register {
15 my($self, $context) = @_;
16 $context->autoload_plugin({ module => 'Filter::FloatingDateTime' });
17 $context->register_hook(
18 $self,
19 'publish.feed' => \&publish_feed,
20 'plugin.init' => \&plugin_init,
21 );
22 }
23  
24 sub plugin_init {
25 my($self, $context, $args) = @_;
26  
27 # check dir
28 my $dir = $self->conf->{dir};
29 unless (-e $dir && -d _) {
30 mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
31 }
32  
33 unless (exists $self->conf->{full_content}) {
34 $self->conf->{full_content} = 1;
35 }
36 }
37  
38 sub publish_feed {
39 my($self, $context, $args) = @_;
40  
41 my $conf = $self->conf;
42 my $f = $args->{feed};
43 my $feed_format = $conf->{format} || 'Atom';
44  
45 # generate feed
46 my $feed = XML::Feed->new($feed_format);
47 $feed->title($f->title);
48 $feed->link($f->link);
49 $feed->modified(Plagger::Date->now);
50 $feed->generator("Plagger/$Plagger::VERSION");
51 $feed->description($f->description || '');
52 $feed->author( $self->make_author($f->author, $feed_format) )
53 if $f->primary_author;
54  
55 my $taguri_base = $self->conf->{taguri_base} || do {
56 require Sys::Hostname;
57 Sys::Hostname::hostname();
58 };
59  
60 if ($feed_format eq 'Atom') {
61 $feed->{atom}->id("tag:$taguri_base,2006:" . $f->id); # XXX what if id is empty?
62 }
63  
64 # add entry
65 for my $e ($f->entries) {
66 my $entry = XML::Feed::Entry->new($feed_format);
67 $entry->title($e->title);
68 $entry->link($e->permalink);
69 $entry->summary($e->body_text) if defined $e->body;
70  
71 # hack to bypass XML::Feed Atom 0.3 crufts (type="text/html")
72 if ($self->conf->{full_content} && defined $e->body) {
73 if ($feed_format eq 'RSS') {
74 $entry->content($e->body);
75 } else {
76 $entry->{entry}->content($e->body->utf8);
77 }
78 }
79  
80 $entry->category(join(' ', @{$e->tags})) if @{$e->tags};
81 $entry->issued($e->date) if $e->date;
82 $entry->modified($e->date) if $e->date;
83  
84 if ($feed_format eq 'RSS') {
85 my $author = 'nobody@example.com';
86 $author .= ' (' . $e->author . ')' if $e->author;
87 $entry->author($author);
88 } else {
89 unless ($feed->author) {
90 $entry->author($e->author || 'nobody');
91 }
92 }
93  
94 $entry->id("tag:$taguri_base,2006:" . $e->id);
95  
96 if ($e->has_enclosure) {
97 for my $enclosure (grep { defined $_->url && !$_->is_inline } $e->enclosures) {
98 $entry->add_enclosure({
99 url => $enclosure->url,
100 length => $enclosure->length,
101 type => $enclosure->type,
102 });
103  
104 # RSS 2.0 by spec doesn't allow multiple enclosures
105 last if $feed_format eq 'RSS';
106 }
107 }
108  
109 $feed->add_entry($entry);
110 }
111  
112 # generate file path
113 my $tmpl = '%i.' . ($feed_format eq 'RSS' ? 'rss' : 'atom');
114 my $file = Plagger::Util::filename_for($f, $self->conf->{filename} || $tmpl);
115 my $filepath = File::Spec->catfile($self->conf->{dir}, $file);
116  
117 $context->log(info => "save feed for " . $f->link . " to $filepath");
118  
119 my $xml = $feed->as_xml;
120 utf8::decode($xml) unless utf8::is_utf8($xml);
121 open my $output, ">:utf8", $filepath or $context->error("$filepath: $!");
122 print $output $xml;
123 close $output;
124 }
125  
126 sub make_author {
127 my($self, $author, $feed_format) = @_;
128  
129 if ($feed_format eq 'RSS') {
130 my $rfc822 = 'nobody@example.com';
131 $rfc822 .= ' (' . $author . ')' if $author;
132 return $rfc822;
133 } else {
134 return defined $author ? $author : 'nobody';
135 }
136 }
137  
138 # XXX okay, this is a hack until XML::Feed is updated
139 *XML::Feed::Entry::Atom::add_enclosure = sub {
140 my($entry, $enclosure) = @_;
141 my $link = XML::Atom::Link->new;
142 $link->rel('enclosure');
143 $link->type($enclosure->{type});
144 $link->href($enclosure->{url});
145 $link->length($enclosure->{length});
146 $entry->{entry}->add_link($link);
147 };
148  
149 *XML::Feed::Entry::RSS::add_enclosure = sub {
150 my($entry, $enclosure) = @_;
151 $entry->{entry}->{enclosure} = {
152 url => $enclosure->{url},
153 type => $enclosure->{type},
154 length => $enclosure->{length},
155 };
156 };
157  
158  
159 1;
160  
161 __END__