App-html2wp
view release on metacpan or search on metacpan
lib/App/html2wp.pm view on Meta::CPAN
filename => {
summary => 'Path to HTML document to publish',
schema => 'filename*',
req => 1,
pos => 0,
cmdline_aliases => {f=>{}},
},
publish => {
summary => 'Whether to publish post or make it a draft',
schema => 'bool*',
description => <<'_',
Equivalent to `--extra-attr post_status=published`, while `--no-publish` is
equivalent to `--extra-attr post_status=draft`.
_
},
schedule => {
summary => 'Schedule post to be published sometime in the future',
schema => 'date*',
description => <<'_',
Equivalent to `--publish --extra-attr post_date=DATE`. Note that WordPress
accepts date in the `YYYYMMDD"T"HH:MM:SS` format, but you specify this option in
regular ISO8601 format. Also note that time is in your chosen local timezone
setting.
_
},
comment_status => {
summary => 'Whether to allow comments (open) or not (closed)',
schema => ['str*', in=>['open','closed']],
default => 'closed',
},
extra_attrs => {
'x.name.is_plural' => 1,
'x.name.singular' => 'extra_attr',
summary => 'Set extra post attributes, e.g. ping_status, post_format, etc',
schema => ['hash*', of=>'str*'],
},
},
args_rels => {
choose_one => [qw/publish schedule/],
},
features => {
dry_run => 1,
},
links => [
{url=>'prog:org2wp'},
{url=>'prog:pod2wp'},
{url=>'prog:wp-xmlrpc'},
],
};
sub html2wp {
my %args = @_;
my $dry_run = $args{-dry_run};
my $filename = $args{filename};
(-f $filename) or return [404, "No such file '$filename'"];
require File::Slurper;
my $html = File::Slurper::read_text($filename);
my $title;
if ($html =~ m!<title>(.+?)</title>!i || $html =~ m!<meta\s+name=\"?title\"?\s+content=\"?(.*?)\"?>!is) {
$title = $1;
log_trace("Extracted title from HTML document: %s", $title);
} else {
$title = "(No title)";
}
my $post_tags;
if ($html =~ m!<meta\s+name=\"?tags?\"?\s+content=\"?(.*?)\"?>!is) {
$post_tags = [split /\s+|\s*[,;]\s*/, $1];
log_trace("Extracted tags from HTML document: %s", $post_tags);
} else {
$post_tags = [];
}
my $post_cats;
if ($html =~ m!<meta\s+name=\"?(?:categories|category)\"?\s+content=\"?(.*?)\"?>!is) {
$post_cats = [split /\s+|\s*[,;]\s*/, $1];
log_trace("Extracted categories from HTML document: %s", $post_cats);
} else {
$post_cats = [];
}
my $postid;
if ($html =~ m!<meta\s+name=\"?postid\"?\s+content=\"?(.*?)\"?>!is) {
$postid = $1;
log_trace("HTML document already has post ID: %s", $postid);
}
require XMLRPC::Lite;
my $call;
# create categories if necessary
my $cat_ids = {};
{
log_info("[api] Listing categories ...");
$call = XMLRPC::Lite->proxy($args{proxy})->call(
'wp.getTerms',
1, # blog id, set to 1
$args{username},
$args{password},
'category',
);
return [$call->fault->{faultCode}, "Can't list categories: ".$call->fault->{faultString}]
if $call->fault && $call->fault->{faultCode};
my $all_cats = $call->result;
for my $cat (@$post_cats) {
if (my ($cat_detail) = grep { $_->{name} eq $cat } @$all_cats) {
$cat_ids->{$cat} = $cat_detail->{term_id};
log_trace("Category %s already exists", $cat);
next;
}
if ($dry_run) {
log_info("(DRY_RUN) [api] Creating category %s ...", $cat);
next;
( run in 1.800 second using v1.01-cache-2.11-cpan-39bf76dae61 )