| 1 | package Plagger::Plugin::Publish::OPML; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use Plagger::Date; | ||
| 6 | use File::Spec; | ||
| 7 | |||
| 8 | sub init { | ||
| 9 | my $self = shift; | ||
| 10 | $self->SUPER::init(@_); | ||
| 11 | my $output = $self->conf->{filename} | ||
| 12 | or Plagger->context->error("filename is missing"); | ||
| 13 | } | ||
| 14 | |||
| 15 | sub register { | ||
| 16 | my($self, $context) = @_; | ||
| 17 | $context->register_hook( | ||
| 18 | $self, | ||
| 19 | 'publish.finalize' => \&finalize, | ||
| 20 | ); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub finalize { | ||
| 24 | my($self, $context, $args) = @_; | ||
| 25 | |||
| 26 | my $out = $self->templatize('opml.tt', { | ||
| 27 | feeds => [ $context->subscription->feeds ], | ||
| 28 | now => Plagger::Date->now, | ||
| 29 | conf => $self->conf, | ||
| 30 | }); | ||
| 31 | |||
| 32 | my $path = $self->conf->{filename}; | ||
| 33 | $context->log(info => "Writing OPML to $path"); | ||
| 34 | |||
| 35 | open my $fh, ">:utf8", $path or $context->error("$path: $!"); | ||
| 36 | print $fh $out; | ||
| 37 | close $fh; | ||
| 38 | } | ||
| 39 | |||
| 40 | 1; | ||
| 41 | |||
| 42 | __END__ |