Publish::Simple

PlaggerConferenceだけじゃなく、その後のHackathonにも参加できないので、反省の意味も含めて一人寂しくHackathonの真似事をしてみました*1


これも現在は使い道がほとんど思いつきません。が、CostomFeed::PlaintextかCustomFeed::Filesysが実装されれば、Filter::HatenaFormatやFilter::Markdownとあわせてbloxsomみたいなことが出来そう*2

内容はただFeedのタイトルのディレクトリをもつEntryタイトルのファイルを作成し、bodyの中身を吐き出すだけです。

    package Plagger::Plugin::Publish::Simple;
use strict;
use warnings;
use base qw ( Plagger::Plugin );

our $VERSION = 0.01;

use Encode;
use File::Spec;

sub register {
my ($self, $context) = @_;
$context->register_hook(
$self,
'publish.init' => \&initialize,
'publish.entry.fixup' => \&store_entry,
);
}

sub initialize {
my ($self, $context, $args) = @_;
$self->{store_dir} = $self->conf->{dir} || "plagger/Simple";
$self->my_mkdir($context, $self->{store_dir});

}

sub store_entry {
my ($self, $context, $args) = @_;
my $cfg = $self->conf;
my $dir = $self->{store_dir} . "/" . $args->{feed}->title;
$self->my_mkdir($context, $dir);
my $entry = $args->{entry};
my $file_name = $self->convert($entry->title);
my $path = File::Spec->catfile($self->convert($dir), $file_name);
open(FILE, ">$path");
print(FILE $self->convert($entry->body));
close(FILE);
$context->log(info => "Publishing to " . $path);
}

sub my_mkdir {
my ($self, $context, $dir) = @_;
my $permission = $self->conf->{permission} || 0755;
$dir = $self->convert($dir);
unless (-e $dir && -d _) {
mkdir $dir, $permission or die $context->error("mkdir $dir: $!");
$context->log(info => "Create new folder ($dir)");
}
}

使い方は以下の通り、現状ではファイル名とファイル内のエンコードは一緒になります。

    - module: Publish::Simple
config:
encoding: euc-jp
dir: /tmp/plagger

名前は変更した方がいいかな?

*1:勤務中ですが

*2:Plaggerdができればそれだけで可能ですが。