App-org2wp

 view release on metacpan or  search on metacpan

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

package App::org2wp;

use 5.010001;
use strict;
use warnings;
use Log::ger;

use Perinci::Object qw(envresmulti);
use POSIX qw(strftime);

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2022-05-02'; # DATE
our $DIST = 'App-org2wp'; # DIST
our $VERSION = '0.013'; # VERSION

our %SPEC;

sub _fmt_timestamp_org {
    my $time = shift;

    strftime("%Y-%m-%d %a %H:%M", localtime($time));
}

$SPEC{'org2wp'} = {
    v => 1.1,
    summary => 'Publish Org document (or heading) to WordPress as blog post',
    description => <<'_',

This is originally a quick hack because I couldn't make
[org2blog](https://github.com/punchagan/org2blog) on my Emacs installation to
work after some update. `org2wp` uses the same format as `org2blog`, but instead
of being an Emacs package, it is a CLI script written in Perl.

First, create `~/org2wp.conf` containing the API credentials, e.g.:

    ; use INI (IOD) format for this file
    proxy=https://YOURBLOGNAME.wordpress.com/xmlrpc.php
    username=YOURUSERNAME
    password=YOURPASSWORD

Note that `proxy` is the endpoint URL of your WordPress instance's XML-RPC
server, which can be hosted on `wordpress.com` or on other server, including
your own. It has nothing to do with HTTP/HTTPS proxy; the term "proxy" is used
by the <pm:XMLRPC::Lite> and <pm:SOAP::Lite> Perl libraries and `org2wp` simply
uses the same terminology.

You can also put multiple credentials in the configuration file using profile
sections, e.g.:

    ; use INI (IOD) format for this file
    [profile=blog1]
    proxy=https://YOURBLOG1NAME.wordpress.com/xmlrpc.php
    username=YOURUSERNAME
    password=YOURPASSWORD

    [profile=blog2]
    proxy=https://YOURBLOG2NAME.wordpress.com/xmlrpc.php
    username=YOURUSERNAME
    password=YOURPASSWORD

and specify which profile you want using command-line option e.g.
`--config-profile blog1`.

### Document mode

You can use the whole Org document file as a blog post (document mode) or a
single heading as a blog post (heading mode). The default is document mode. To
create a blog post, write your Org document (e.g. in `post1.org`) using this
format:

    #+TITLE: Blog post title
    #+CATEGORY: cat1, cat2
    #+TAGS: tag1,tag2,tag3

    Text of your post ...
    ...

then:

    % org2wp post1.org

this will create a draft post. To publish directly:

    % org2wp --publish post1.org

Note that this will also modify your Org file and insert this setting line at
the top:

    #+POSTID: 1234
    #+POSTTIME: [2020-09-16 Wed 11:51]

where 1234 is the post ID retrieved from the server when creating the post, and
post time will be set to the current local time.

After the post is created, you can update using the same command:

    % org2wp post1.org

You can use `--publish` to publish the post, or `--no-publish` to revert it to
draft.

To set more attributes:

    % org2wp post1.org --comment-status open \
        --extra-attr ping_status=closed --extra-attr sticky=1

Another example, to schedule a post in the future:

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

            $file_content =~ s/^#\+POSTTIME:.*/#+POSTTIME: $posts_times[0]/m or
                $file_content =~ s/^/#+POSTTIME: $posts_times[0]\n/;

            my $orig_post_id = $orig_posts_ids[0];
            unless ($orig_post_id) {
                my $new_post_id = $call->result;
                log_info("Inserting #+POSTID (%d) ...", $new_post_id);
                $file_content =~ s/^/#+POSTID: $new_post_id\n/;
            }
        }

        if (!$do_update_file) {
            log_info("Not updating %s because there are no changes", $filename);
        } else {
            log_trace("Updated file content: <<<%s>>>", $file_content);
            if ($dry_run) {
                log_info("[DRY-RUN] Writing %s", $filename);
            } else {
                log_info("Updating file %s ...", $filename);
                File::Slurper::write_text($filename, $file_content);
            }
        }
    } # L6_INSERT_POST_IDS

    [$dry_run ? 304:200, "OK"];
}

1;
# ABSTRACT: Publish Org document (or heading) to WordPress as blog post

__END__

=pod

=encoding UTF-8

=head1 NAME

App::org2wp - Publish Org document (or heading) to WordPress as blog post

=head1 VERSION

This document describes version 0.013 of App::org2wp (from Perl distribution App-org2wp), released on 2022-05-02.

=head1 FUNCTIONS


=head2 org2wp

Usage:

 org2wp(%args) -> [$status_code, $reason, $payload, \%result_meta]

Publish Org document (or heading) to WordPress as blog post.

This is originally a quick hack because I couldn't make
LL<https://github.com/punchagan/org2blog> on my Emacs installation to
work after some update. C<org2wp> uses the same format as C<org2blog>, but instead
of being an Emacs package, it is a CLI script written in Perl.

First, create C<~/org2wp.conf> containing the API credentials, e.g.:

 ; use INI (IOD) format for this file
 proxy=https://YOURBLOGNAME.wordpress.com/xmlrpc.php
 username=YOURUSERNAME
 password=YOURPASSWORD

Note that C<proxy> is the endpoint URL of your WordPress instance's XML-RPC
server, which can be hosted on C<wordpress.com> or on other server, including
your own. It has nothing to do with HTTP/HTTPS proxy; the term "proxy" is used
by the L<XMLRPC::Lite> and L<SOAP::Lite> Perl libraries and C<org2wp> simply
uses the same terminology.

You can also put multiple credentials in the configuration file using profile
sections, e.g.:

 ; use INI (IOD) format for this file
 [profile=blog1]
 proxy=https://YOURBLOG1NAME.wordpress.com/xmlrpc.php
 username=YOURUSERNAME
 password=YOURPASSWORD
 
 [profile=blog2]
 proxy=https://YOURBLOG2NAME.wordpress.com/xmlrpc.php
 username=YOURUSERNAME
 password=YOURPASSWORD

and specify which profile you want using command-line option e.g.
C<--config-profile blog1>.

=head3 Document mode

You can use the whole Org document file as a blog post (document mode) or a
single heading as a blog post (heading mode). The default is document mode. To
create a blog post, write your Org document (e.g. in C<post1.org>) using this
format:

 #+TITLE: Blog post title
 #+CATEGORY: cat1, cat2
 #+TAGS: tag1,tag2,tag3
 
 Text of your post ...
 ...

then:

 % org2wp post1.org

this will create a draft post. To publish directly:

 % org2wp --publish post1.org

Note that this will also modify your Org file and insert this setting line at
the top:

 #+POSTID: 1234
 #+POSTTIME: [2020-09-16 Wed 11:51]

where 1234 is the post ID retrieved from the server when creating the post, and
post time will be set to the current local time.

After the post is created, you can update using the same command:

 % org2wp post1.org

You can use C<--publish> to publish the post, or C<--no-publish> to revert it to
draft.

To set more attributes:

 % org2wp post1.org --comment-status open \
     --extra-attr ping_status=closed --extra-attr sticky=1

Another example, to schedule a post in the future:



( run in 0.460 second using v1.01-cache-2.11-cpan-71847e10f99 )