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

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

more fix to Publish::Feed to deal with atom:author

Line 
1 package Plagger::Plugin::Publish::Feed;
2
3 use strict;
4 use base qw( Plagger::Plugin );
5
6 our $VERSION = 0.01;
7
8 use XML::Feed;
9 use XML::Feed::Entry;
10 use XML::RSS::LibXML;
11 use File::Spec;
12
13 $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
14
15 sub register {
16     my($self, $context) = @_;
17     $context->register_hook(
18         $self,
19         'publish.feed' => \&publish_feed,
20     );
21     $self->init_feed($context);
22 }
23
24 sub init_feed {
25     my($self, $context) = @_;
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     local $XML::Atom::DefaultVersion = "1.0";
46
47     # generate feed
48     my $feed = XML::Feed->new($feed_format);
49     $feed->title($f->title);
50     $feed->link($f->link);
51     $feed->modified(Plagger::Date->now);
52     $feed->generator("Plagger/$Plagger::VERSION");
53     $feed->description($f->description || '');
54     $feed->author($f->author) if $f->primary_author;
55
56     if ($feed_format eq 'Atom') {
57         $feed->{atom}->id("tag:plagger.org,2006:" . $f->id);
58     }
59
60     # add entry
61     for my $e ($f->entries) {
62         my $entry = XML::Feed::Entry->new($feed_format);
63         $entry->title($e->title);
64         $entry->link($e->link);
65         $entry->summary($e->body_text) if defined $e->body;
66
67         # hack to bypass XML::Feed Atom 0.3 crufts (type="text/html")
68         if ($self->conf->{full_content} && defined $e->body) {
69             if ($feed_format eq 'RSS') {
70                 $entry->content($e->body);
71             } else {
72                 $entry->{entry}->content($e->body);
73             }
74         }
75
76         $entry->category(join(' ', @{$e->tags}));
77         $entry->issued($e->date)   if $e->date;
78         $entry->modified($e->date) if $e->date;
79
80         if ($feed_format eq 'RSS') {
81             my $author = 'nobody@example.com';
82             $author .= ' (' . $e->author . ')' if $e->author;
83             $entry->author($author);
84         } else {
85             unless ($feed->author) {
86                 $entry->author($e->author || 'nobody');
87             }
88         }
89
90         $entry->id("tag:plagger.org,2006:" . $e->id);
91
92         if ($e->has_enclosure) {
93             for my $enclosure (grep { defined $_->url && !$_->is_inline } $e->enclosures) {
94                 $entry->add_enclosure({
95                     url    => $enclosure->url,
96                     length => $enclosure->length,
97                     type   => $enclosure->type,
98                 });
99
100                 # RSS 2.0 by spec doesn't allow multiple enclosures
101                 last if $feed_format eq 'RSS';
102             }
103         }
104
105         $feed->add_entry($entry);
106     }
107
108     # generate file path
109     my $filepath = File::Spec->catfile($self->conf->{dir}, $self->gen_filename($f));
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 my %formats = (
121     'u' => sub { my $s = $_[0]->url;  $s =~ s!^https?://!!; $s },
122     'l' => sub { my $s = $_[0]->link; $s =~ s!^https?://!!; $s },
123     't' => sub { $_[0]->title },
124     'i' => sub { $_[0]->id },
125 );
126
127 my $format_re = qr/%(u|l|t|i)/;
128
129 sub gen_filename {
130     my($self, $feed) = @_;
131
132     my $file = $self->conf->{filename} ||
133         '%i.' . ($self->conf->{format} eq 'RSS' ? 'rss' : 'atom');
134     $file =~ s{$format_re}{
135         $self->safe_filename($formats{$1}->($feed))
136     }egx;
137     $file;
138 }
139
140 sub safe_filename {
141     my($self, $path) = @_;
142     $path =~ s![^\w\s]+!_!g;
143     $path =~ s!\s+!_!g;
144     $path;
145 }
146
147 # XXX okay, this is a hack until XML::Feed is updated
148 *XML::Feed::Entry::Atom::add_enclosure = sub {
149     my($entry, $enclosure) = @_;
150     my $link = XML::Atom::Link->new;
151     $link->rel('enclosure');
152     $link->type($enclosure->{type});
153     $link->href($enclosure->{url});
154     $link->length($enclosure->{length});
155     $entry->{entry}->add_link($link);
156 };
157
158 *XML::Feed::Entry::RSS::add_enclosure = sub {
159     my($entry, $enclosure) = @_;
160     $entry->{entry}->{enclosure} = {
161         url    => $enclosure->{url},
162         type   => $enclosure->{type},
163         length => $enclosure->{length},
164     };
165 };
166
167
168 1;
169
170 __END__
171
172 =head1
173
174 Plagger::Plugin::Publish::Feed - republish RSS/Atom feeds
175
176 =head1 SYNOPSYS
177
178   - module: Publish::Feed
179     config:
180       format: RSS
181       dir: /home/yoshiki/plagger/feed
182       filename: my_%t.rss
183
184 =head1 CONFIG
185
186 =over 4
187
188 =item format
189
190 Specify the format of feed. C<Plagger::Plugin::Publish::Feed> supports
191 the following syndication feed formats:
192
193 =over 8
194
195 =item Atom (default)
196
197 =item RSS
198
199 =back
200
201 =item dir
202
203 Directory to save feed files in.
204
205 =item filename
206
207 Filename to be used to create feed files. It defaults to C<%i.rss> for
208 RSS and C<%i.atom> for Atom feed. It supports the following format
209 like printf():
210
211 =over 8
212
213 =item %u url
214
215 =item %l link
216
217 =item %t title
218
219 =item %i id
220
221 =back
222
223 =item full_content
224
225 Whether to publish full content feed. Defaults to 1.
226
227 =back
228
229 =head1 AUTHOR
230
231 Yoshiki KURIHARA
232
233 Tatsuhiko Miyagawa
234
235 Gosuke Miyashita
236
237 =head1 SEE ALSO
238
239 L<Plagger>, L<XML::Feed>
240
241 =cut
Note: See TracBrowser for help on using the browser.