App-org2wp
view release on metacpan or search on metacpan
lib/App/org2wp.pm view on Meta::CPAN
schema => ['array*', of=>'str*'],
tags => ['category:heading-mode'],
},
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.
_
},
post_password => {
summary => 'Set password for posts',
schema => 'str*',
},
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:pod2wp'},
{url=>'prog:html2wp'},
{url=>'prog:wp-xmlrpc'},
],
};
sub org2wp {
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 $file_content = File::Slurper::read_text($filename);
my $post_heading_level = $args{post_heading_level};
my $mode = 'document';
if (defined $post_heading_level) {
$mode = 'heading';
}
# 1. collect the posts information: for each post: the org source, its
# title, existing post ID (if available), as well as all categories and tags
# we want to use.
my @posts_srcs; # ("org1", "org2", ...)
my @posts_htmls; # ("html1", "html2", ...)
my @posts_titles; # ("title1", "title2", ...)
my @orig_posts_ids;# (101, undef, ...)
my @new_posts_ids;
my @posts_tags; # ([tag1_for_post1,tag2_for_post1], [tag1_for_post2,...], ...)
my @posts_cats; # ([cat1_for_post1,cat2_for_post1], [cat1_for_post2,...], ...)
my @posts_times; # ("[2020-09-17 Thu 01:55]", ...)
my @posts_headlines; # ($headline_obj1, ...) # only for heading mode
L1_COLLECT_POSTS_INFORMATION: {
require Org::To::HTML::WordPress;
if ($mode eq 'heading') {
require Org::Parser;
my $org_parser = Org::Parser->new;
my $org_doc = $org_parser->parse($file_content, {ignore_unknown_settings=>1});
my @posts_headlines0 = $org_doc->find(
sub {
my $el = shift;
$el->isa("Org::Element::Headline") && $el->level == $post_heading_level;
});
for my $headline (@posts_headlines0) {
push @posts_headlines, $headline;
push @posts_srcs, $headline->children_as_string;
push @posts_titles, $headline->title->as_string;
# find the first properties drawer
my ($properties_drawer) = $headline->find(
sub {
my $el = shift;
$el->isa("Org::Element::Drawer") && $el->name eq 'PROPERTIES';
});
my $properties = $properties_drawer ? $properties_drawer->properties : {};
push @orig_posts_ids, $properties->{POSTID};
push @posts_tags, [$headline->get_tags];
push @posts_cats, [split /\s*,\s*/, ($properties->{CATEGORY} // $properties->{CATEGORIES} // '')];
my $exclude_reason;
FILTER_POST: {
my $post_tags = $posts_tags[-1];
if (defined $args{include_heading_tags} && @{ $args{include_heading_tags} }) {
for my $tag (@{ $args{include_heading_tags} }) {
( run in 1.729 second using v1.01-cache-2.11-cpan-39bf76dae61 )