root/trunk/plagger/lib/Plagger/Plugin/Publish/Feed.pm

Revision 1863 (checked in by miyagawa, 14 years ago)

Publish::Feed bugs:

  • Fixed a bug where empty <category /> element is created when there's no tags.
  • Fixed a bug where author 'nobody' is added if the feed has author but entry doesn't.
Line 
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::RSS::LibXML;
9 use File::Spec;
10
11 $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
12
13 sub register {
14     my($self, $context) = @_;
15     $context->autoload_plugin({ module => 'Filter::FloatingDateTime' });
16     $context->register_hook(
17         $self,
18         'publish.feed' => \&publish_feed,
19         'plugin.init'  => \&plugin_init,
20     );
21 }
22
23 sub plugin_init {
24     my($self, $context, $args) = @_;
25
26     # check dir
27     my $dir = $self->conf->{dir};
28     unless (-e $dir && -d _) {
29         mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
30     }
31
32     unless (exists $self->conf->{full_content}) {
33         $self->conf->{full_content} = 1;
34     }
35 }
36
37 sub publish_feed {
38     my($self, $context, $args) = @_;
39
40     my $conf = $self->conf;
41     my $f = $args->{feed};
42     my $feed_format = $conf->{format} || 'Atom';
43
44     # generate feed
45     my $feed = XML::Feed->new($feed_format);
46     $feed->title($f->title);
47     $feed->link($f->link);
48     $feed->modified(Plagger::Date->now);
49     $feed->generator("Plagger/$Plagger::VERSION");
50     $feed->description($f->description || '');
51     $feed->author( $self->make_author($f->author, $feed_format) )
52         if $f->primary_author;
53
54     if ($feed_format eq 'Atom') {
55         $feed->{atom}->id("tag:plagger.org,2006:" . $f->id);
56     }
57
58     # add entry
59     for my $e ($f->entries) {
60         my $entry = XML::Feed::Entry->new($feed_format);
61         $entry->title($e->title);
62         $entry->link($e->permalink);
63         $entry->summary($e->body_text) if defined $e->body;
64
65         # hack to bypass XML::Feed Atom 0.3 crufts (type="text/html")
66         if ($self->conf->{full_content} && defined $e->body) {
67             if ($feed_format eq 'RSS') {
68                 $entry->content($e->body);
69             } else {
70                 $entry->{entry}->content($e->body->utf8);
71             }
72         }
73
74         $entry->category(join(' ', @{$e->tags})) if @{$e->tags};
75         $entry->issued($e->date)   if $e->date;
76         $entry->modified($e->date) if $e->date;
77
78         if ($feed_format eq 'RSS') {
79             my $author = 'nobody@example.com';
80             $author .= ' (' . $e->author . ')' if $e->author;
81             $entry->author($author);
82         } else {
83             unless ($feed->author) {
84                 $entry->author($e->author || 'nobody');
85             }
86         }
87
88         $entry->id("tag:plagger.org,2006:" . $e->id);
89
90         if ($e->has_enclosure) {
91             for my $enclosure (grep { defined $_->url && !$_->is_inline } $e->enclosures) {
92                 $entry->add_enclosure({
93                     url    => $enclosure->url,
94                     length => $enclosure->length,
95                     type   => $enclosure->type,
96                 });
97
98                 # RSS 2.0 by spec doesn't allow multiple enclosures
99                 last if $feed_format eq 'RSS';
100             }
101         }
102
103         $feed->add_entry($entry);
104     }
105
106     # generate file path
107     my $tmpl = '%i.' . ($feed_format eq 'RSS' ? 'rss' : 'atom');
108     my $file = Plagger::Util::filename_for($f, $self->conf->{filename} || $tmpl);
109     my $filepath = File::Spec->catfile($self->conf->{dir}, $file);
110
111     $context->log(info => "save feed for " . $f->link . " to $filepath");
112
113     my $xml = $feed->as_xml;
114     utf8::decode($xml) unless utf8::is_utf8($xml);
115     open my $output, ">:utf8", $filepath or $context->error("$filepath: $!");
116     print $output $xml;
117     close $output;
118 }
119
120 sub make_author {
121     my($self, $author, $feed_format) = @_;
122
123     if ($feed_format eq 'RSS') {
124         my $rfc822 = 'nobody@example.com';
125         $rfc822 .= ' (' . $author . ')' if $author;
126         return $rfc822;
127     } else {
128         return defined $author ? $author : 'nobody';
129     }
130 }
131
132 # XXX okay, this is a hack until XML::Feed is updated
133 *XML::Feed::Entry::Atom::add_enclosure = sub {
134     my($entry, $enclosure) = @_;
135     my $link = XML::Atom::Link->new;
136     $link->rel('enclosure');
137     $link->type($enclosure->{type});
138     $link->href($enclosure->{url});
139     $link->length($enclosure->{length});
140     $entry->{entry}->add_link($link);
141 };
142
143 *XML::Feed::Entry::RSS::add_enclosure = sub {
144     my($entry, $enclosure) = @_;
145     $entry->{entry}->{enclosure} = {
146         url    => $enclosure->{url},
147         type   => $enclosure->{type},
148         length => $enclosure->{length},
149     };
150 };
151
152
153 1;
154
155 __END__
156
157 =head1
158
159 Plagger::Plugin::Publish::Feed - republish RSS/Atom feeds
160
161 =head1 SYNOPSIS
162
163   - module: Publish::Feed
164     config:
165       format: RSS
166       dir: /home/yoshiki/plagger/feed
167       filename: my_%t.rss
168
169 =head1 CONFIG
170
171 =over 4
172
173 =item format
174
175 Specify the format of feed. C<Plagger::Plugin::Publish::Feed> supports
176 the following syndication feed formats:
177
178 =over 8
179
180 =item Atom (default)
181
182 =item RSS
183
184 =back
185
186 =item dir
187
188 Directory to save feed files in.
189
190 =item filename
191
192 Filename to be used to create feed files. It defaults to C<%i.rss> for
193 RSS and C<%i.atom> for Atom feed. It supports the following format
194 like printf():
195
196 =over 8
197
198 =item %u url
199
200 =item %l link
201
202 =item %t title
203
204 =item %i id
205
206 =back
207
208 =item full_content
209
210 Whether to publish full content feed. Defaults to 1.
211
212 =back
213
214 =head1 AUTHOR
215
216 Yoshiki Kurihara
217
218 Tatsuhiko Miyagawa
219
220 Gosuke Miyashita
221
222 =head1 SEE ALSO
223
224 L<Plagger>, L<XML::Feed>
225
226 =cut
Note: See TracBrowser for help on using the browser.