すらいむがあらわれた

こまんど >  たたかう  にげる

こんな感じに

先日書いたMT形式出力のPlaggerプラグインです。
コードが汚くて恥ずかしいですが載せてみました。
ベースはPublish::CSV.pmです。(ほとんどそのままです)


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

our $VERSION = 0.01;

use Encode;
use File::Spec;
use IO::File;

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

sub feed {
my ($self, $context, $args) = @_;
my $append = ($self->conf->{mode} && $self->conf->{mode} eq 'append');
my $dir = $self->conf->{dir};
unless (-e $dir && -d _) {
mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
}

my $status = 'Draft';
if($self->conf->{status} && $self->conf->{status} eq 'publish'){
$status = 'Publish';
}

my $allow_comments = $self->check_flag($self->conf->{allow_comments});
my $convert_breaks = $self->check_flag($self->conf->{convert_breaks});
my $allow_pings = $self->check_flag($self->conf->{allow_pings});

my $primary_category = $self->conf->{primary_category};
my $categories = $self->conf->{category};

my $file = $self->gen_filename($args->{feed}) || $args->{feed}->id . ".txt";
my $path = File::Spec->catfile($dir, $file);
my $io = IO::File->new($append ? ">> $path" : "> $path");

for my $entry ($args->{feed}->entries) {
$io->printf("AUTHOR: %s\n", $self->convert($entry->author));
$io->printf("TITLE: %s\n", $self->convert($entry->title));
$io->printf("STATUS: %s\n", $status);
$io->printf("ALLOW COMMENTS: %s\n", $allow_comments);
$io->printf("CONVERT BREAKS: %s\n", $convert_breaks);
$io->printf("ALLOW PINGS: %s\n", $allow_pings);
$io->print("DATE: ".$self->datetime_tostring($entry->date)."\n");
if( $primary_category ){
$io->printf("PRIMARY CATEGORY: %s\n", $primary_category);
}
if( $categories ){
foreach my $category (@$categories){
$io->printf("CATEGORY: %s\n", $category);
}
}
$io->print("-----\n");
$io->print("BODY:\n");
$io->print($self->convert($entry->body));
$io->print("\n");
$io->print("-----\n");
$io->print("EXTENDED BODY:\n");
$io->print("\n");
$io->print("-----\n");
$io->print("EXCERPT:\n");
$io->print("\n");
$io->print("-----\n");
$io->print("KEYWORDS:\n");
$io->print("\n");
$io->print("-----\n");
$io->print("\n");
$io->print("\n");
$io->print("--------\n");
}

$context->log(
info => sprintf(
"%s to %s: %d entries",
$append ? 'Append' : 'Write',
$path,
$args->{feed}->count
)
);
}

sub convert {
my ($self, $str) = @_;
utf8::decode($str) unless utf8::is_utf8($str);
return encode($self->conf->{encoding} || 'utf8', $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} || '';
$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;
}

sub check_flag{
my($self, $flag) = @_;
if($flag && $flag ne '0' && $flag ne '1')
{
$flag = '0';
}elsif(!defined($flag)){
$flag = '0';
}
$flag;
}

sub datetime_tostring{
my ($self, $dt) = @_;
my $date = $dt ? $dt->mdy('/') : '01/01/0001';
my $time = $dt ? $dt->hms : '00:00:00';

return $date.' '.$time;
}

1;

時間ができたら出力をテンプレートにしてキレイにしたいところです。


config.yamlは以下のような感じで。
必須なのはdirだけで、あとはデフォルトでいちおう値が設定されます。filenameはつけたほうがいいかも。


- module: Publish::MTExport
config:
dir: /var/web/mtexport
encoding: utf-8
filename: my_%t.txt
mode: append
status: Publish
allow_comments: 0
convert_breaks: 1
allow_pings: 0
primary_category: test
category:
- aaaa
- bbbb