App-pod2wp

 view release on metacpan or  search on metacpan

lib/App/pod2wp.pm  view on Meta::CPAN

            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/],
    },
    deps => {
        prog => 'pod2html',
    },
    features => {
        dry_run => 1,
    },
    links => [
        {url=>'prog:org2wp'},
        {url=>'prog:html2wp'},
        {url=>'prog:wp-xmlrpc'},
    ],
};
sub pod2wp {
    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 $pod = File::Slurper::read_text($filename);

    log_info("Converting POD to HTML ...");
    my $html = _pod2html($filename);

    my $title;
    if ($pod =~ /^=for pod2wp TITLE:\s*(.+)\R\R/m) {
        $title = $1;
        log_trace("Extracted title from POD document: %s", $title);
    } else {
        $title = "(No title)";
    }

    my $post_tags;
    if ($pod =~ /^=for pod2wp TAGS?:\s*(.+)\R\R/m) {
        $post_tags = [split /\s*,\s*/, $1];
        log_trace("Extracted tags from POD document: %s", $post_tags);
    } else {
        $post_tags = [];
    }

    my $post_cats;
    if ($pod =~ /^=for pod2wp CATEGOR(?:Y|IES):\s*(.+)\R\R/m) {
        $post_cats = [split /\s*,\s*/, $1];
        log_trace("Extracted categories from POD document: %s", $post_cats);
    } else {
        $post_cats = [];
    }

    my $postid;
    if ($pod =~ /^=for pod2wp POSTID:\s*(\d+)\R\R/m) {
        $postid = $1;
        log_trace("POD 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;
            }



( run in 2.538 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )