|
#!/usr/bin/env perl |
|
# concat-wxr.pl *.txt > wxr.xml |
|
|
|
use strict; |
|
use warnings; |
|
use utf8; |
|
|
|
use Data::Dumper; |
|
use Data::Section::Simple 'get_data_section'; |
|
use Date::Format 'time2str'; |
|
use Date::Parse 'str2time'; |
|
use Text::Xslate; |
|
|
|
use constant DEBUG => $ENV{DEBUG}; |
|
|
|
binmode STDOUT, ':utf8'; |
|
|
|
my @files = @ARGV; # 事前に作成した txt ファイルを期待 |
|
|
|
if (!@files) { |
|
die "$0 files.txt..."; |
|
} |
|
|
|
my $wxr_tmpl_content = get_data_section('wxr.xml'); |
|
my $tx = Text::Xslate->new( type => 'text' ); |
|
my %vars; |
|
$vars{items} = []; |
|
|
|
for my $file (@files) { |
|
my %data = read_data($file); |
|
my %item; |
|
for my $key (qw/Title Publish-Date Link Tags/) { # Tags は使わない? |
|
#warn "$key\: $data{$key}\n" if DEBUG; |
|
$item{title} = $data{Title}; |
|
$item{content} = $data{Content}; |
|
my $pub_date = $data{'Publish-Date'}; # Publish-Date: Wed Feb 1 09:05:00 2012 |
|
$item{post_date} = time2str('%Y-%m-%d %H:%M:%S', str2time($pub_date)); |
|
$item{post_date_gmt} = time2str('%Y-%m-%d %H:%M:%S', str2time($pub_date) - 3600*9); |
|
my $post_name = $data{Link}; # Link: http://post.tetsuji.jp/nml-20121 |
|
$post_name =~ s{.*/}{}; |
|
$item{post_name} = $post_name; |
|
} |
|
push @{$vars{items}}, \%item; |
|
} |
|
|
|
print $tx->render_string($wxr_tmpl_content, \%vars); |
|
|
|
|
|
# if ( DEBUG ) { |
|
# for my $item (@{$vars{items}}) { |
|
# print $item->{title} . "\n"; |
|
# } |
|
# } |
|
|
|
sub read_data { |
|
my $file = shift; |
|
open my $fh, '<:utf8', $file |
|
or die; |
|
my %data; |
|
while (<$fh>) { |
|
last if /^$/; |
|
chomp; |
|
my ($key, $value) = split /:\s*/, $_, 2; |
|
$data{$key} = $value; |
|
} |
|
# Title: NMLノート 2012年1月版 |
|
# Publish-Date: Wed Feb 1 09:05:00 2012 |
|
# Link: http://post.tetsuji.jp/nml-20121 |
|
# Tags: classical |
|
$data{Content} = do { local $/; <$fh>; }; |
|
close $fh; |
|
return wantarray ? %data : \%data; |
|
} |
|
|
|
__DATA__ |
|
@@ wxr.xml |
|
<?xml version="1.0" encoding="UTF-8"?> |
|
<rss version="2.0" |
|
xmlns:excerpt="http://wordpress.org/export/1.0/excerpt/" |
|
xmlns:content="http://purl.org/rss/1.0/modules/content/" |
|
xmlns:wfw="http://wellformedweb.org/CommentAPI/" |
|
xmlns:dc="http://purl.org/dc/elements/1.1/" |
|
xmlns:wp="http://wordpress.org/export/1.0/"> |
|
<channel> |
|
<!-- [wp:wxr_version] WXRのバージョン --> |
|
<wp:wxr_version>1.0</wp:wxr_version> |
|
|
|
<!-- [item] 投稿等(繰り返し) --> |
|
: for $items -> $item { |
|
<item> |
|
<title><: $item.title :></title><!-- <: $item.post_name :> --> |
|
<dc:creator>xtetsuji</dc:creator> |
|
|
|
<content:encoded><![CDATA[<: $item.content :>]]></content:encoded> |
|
<excerpt:encoded><![CDATA[]]></excerpt:encoded> |
|
|
|
<wp:post_date><: $item.post_date :></wp:post_date> |
|
<wp:post_date_gmt><: $item.post_date_gmt :></wp:post_date_gmt> |
|
|
|
<wp:comment_status>open</wp:comment_status> |
|
<wp:ping_status>open</wp:ping_status> |
|
|
|
<wp:post_name><: $item.post_name :></wp:post_name> |
|
<wp:status>publish</wp:status> |
|
<wp:post_type>post</wp:post_type> |
|
<wp:post_password></wp:post_password> |
|
|
|
<!-- [wp:postmeta] 投稿等のカスタムフィールド(繰り返し) --> |
|
<!-- |
|
<wp:postmeta> |
|
<wp:meta_key>カスタムフィールドのキー</wp:meta_key> |
|
<wp:meta_value>カスタムフィールドの値</wp:meta_value> |
|
</wp:postmeta> |
|
--> |
|
<!-- [/wp:postmeta] --> |
|
</item> |
|
: } |
|
<!-- [/item] --> |
|
</channel> |
|
</rss> |