| 1 | package Plagger::Plugin::Publish::Pipe; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use Encode; | ||
| 6 | |||
| 7 | sub register { | ||
| 8 | my($self, $context) = @_; | ||
| 9 | $context->register_hook( | ||
| 10 | $self, | ||
| 11 | 'publish.feed' => \&feed, | ||
| 12 | ); | ||
| 13 | } | ||
| 14 | |||
| 15 | sub feed { | ||
| 16 | my($self, $context, $args) = @_; | ||
| 17 | |||
| 18 | open my $out, "|" . $self->conf->{command} or $context->error("Can't open pipe: $!"); ## no critic | ||
| 19 | $context->log(info => "Publishing to " . $self->conf->{command}); | ||
| 20 | for my $entry ($args->{feed}->entries) { | ||
| 21 | print $out $self->convert($entry->title) . "\n"; | ||
| 22 | print $out $self->convert($entry->permalink) . "\n\n"; | ||
| 23 | } | ||
| 24 | close $out; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub convert { | ||
| 28 | my ($self, $str) = @_; | ||
| 29 | utf8::decode($str) unless utf8::is_utf8($str); | ||
| 30 | return encode($self->conf->{encoding} || 'utf8', $str); | ||
| 31 | } | ||
| 32 | |||
| 33 | 1; | ||
| 34 | |||
| 35 | __END__ |