Plagger::Plugin::Publish::Text

とりあえず自分のデバッグ用に。もちろんPPP::Feedのパクリ。ほんとは ">:shift_jis" とかですませたかったんですが、うちのWinだとなぜかうまくいかなかったので明示的にEncode。なぜかもなにも、思いっきり勘違い。perlio+perlio::encodingをきちんと読んでおけ、ということですな。(→コメント欄)

package Plagger::Plugin::Publish::Text;

use strict;
use base qw( Plagger::Plugin );

our $VERSION = 0.01;

use Encode;
use File::Spec;

sub register {
    my($self, $context) = @_;
    $context->register_hook(
        $self,
        'publish.feed' => \&publish_feed,
    );
    $self->init_feed($context);
}

sub init_feed {
    my($self, $context) = @_;

    # check dir
    my $dir = $self->conf->{dir};
    unless (-e $dir && -d _) {
        require File::Path;
        eval { File::Path::mkpath($dir, 0, 0755) };
        $context->error("mkdir $dir: $@") if $@;
    }
}

sub publish_feed {
    my($self, $context, $args) = @_;

    my $conf = $self->conf;
    my $f = $args->{feed};

    # generate file path
    my $filepath = File::Spec->catfile($self->conf->{dir}, $self->gen_filename($f));

    $context->log(info => "save feed for " . $f->url . " to $filepath");

    open my $output, '>', $filepath or $context->error("$filepath: $!");
    $self->println( $output, (
         title    => $f->title,
         link     => $f->link,
         modified => Plagger::Date->now,
         generator => "Plagger/$Plagger::VERSION",
    ));
    print $output "\n";

    for my $e ($f->entries) {
        $self->println( $output, (
            title    => $e->title, 
            link     => $e->link,
            summary  => $e->body_text,
            content  => $e->body,
            category => join(' ', @{$e->tags}),
            issued   => $e->date,
            author   => $e->author,
        ));
        print $output "\n";
    }
    close $output;
}

sub println {
    my ($self, $fh, @args) = @_;

    my $ocode = $self->conf->{ocode};
    while( my ($key, $str) = splice @args, 0, 2 ) {
        $str = encode($ocode, $str) if $ocode && !ref $str;
        print $fh "$key: ", $str, "\n" if $str;
    }
}

my %formats = (
    'u' => sub { my $s = $_[0]->url;  $s =~ s!^https?://!!; $s },
    'l' => sub { my $s = $_[0]->link; $s =~ s!^https?://!!; $s },
    't' => sub { $_[0]->title },
    'i' => sub { $_[0]->id },
);

my $format_re = qr/%(u|l|t|i)/;

sub gen_filename {
    my($self, $feed) = @_;

    my $file = $self->conf->{filename} || '%i.txt';
    $file =~ s{$format_re}{
        $self->safe_filename($formats{$1}->($feed))
    }egx;
    $file;
}

sub safe_filename {
    my($self, $path) = @_;
    $path =~ s![^\w\s]+!_!g;
    $path =~ s!\s+!_!g;
    $path;
}

1;

__END__

=head1

Plagger::Plugin::Publish::Text - just for debugging?

=head1 SYNOPSYS

  - module: Publish::Text
    config:
      dir: /home/ishigaki/plagger/feed
      filename: my_%t.txt
      ocode: shift_jis

=head1 DESCRIPTION

OK, this plugin is just for debugging, based on (i.e. stolen from)
L<Plagger::Plugin::Publish::Feed>.

=head1 CONFIG

=head2 dir

Directory to save feed files in.

=head2 filename

Filename to be used to create feed files. It defaults to C<%i.txt>.
It supports the following format like printf():

=over 4

=item * %u url

=item * %l link

=item * %t title

=item * %i id

=back

=head2 ocode

Encoding of the output file.

=head1 AUTHOR

Kenichi Ishigaki

=head1 SEE ALSO

C<Plagger>, C<Plagger::Plugin::Publish::Feed>

=cut