App-OrgUtils

 view release on metacpan or  search on metacpan

script/org2html  view on Meta::CPAN

use Log::ger;

use Perinci::CmdLine::Any;

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2024-01-09'; # DATE
our $DIST = 'App-OrgUtils'; # DIST
our $VERSION = '0.487'; # VERSION

our %SPEC;

$SPEC{app} = {
    v => 1.1,
    summary => 'Convert Org document to HTML',
    description => <<'_',


_
    args => {
        input_filename => {
            schema => 'filename',
            default => '-',
            req => 1,
            pos => 0,
            cmdline_aliases => {i=>{}},
        },
        backend => {
            schema => ['str*', in=>['Org::To::HTML', 'emacs']],
            default => 'emacs',
            cmdline_aliases => {B=>{}},
        },
        #title => {
        #    summary => 'HTML title',
        #    schema => 'str*',
        #},
        #naked => {
        #    summary => "Don't wrap HTML with HTML/HEAD/BODY elements",
        #    schema => ['bool', is=>1],
        #},
        browser => {
            summary => 'Instead of returning the HTML, show it in browser',
            schema => ['bool*', is=>1],
            cmdline_aliases => {b=>{}},
        },
    },
};
sub app {
    my %args = @_;

    my $backend = $args{backend} // 'emacs';

    if ($backend eq 'emacs') {

        require File::Slurper;
        require File::Temp;
        require IPC::System::Options;

        my ($tempfh, $tempfname) = File::Temp::tempfile(
            "XXXXXXXX", SUFFIX=>".org", TMPDIR=>1);
        if ($args{input_filename} eq '-') {
            binmode STDIN, ":encoding(utf8)";
            print $tempfh <STDIN>;
        } else {
            print $tempfh File::Slurper::read_text($args{input_filename});
        }
        (my $temporgfname = $tempfname) =~ s/\.org\z/.html/;

        my $output;
        IPC::System::Options::system(
            {die=>1, log=>1, capture_merged=>\$output},
            "emacs", $tempfname, "--batch", "-f", "org-html-export-to-html", "--kill",
        );

        if ($args{browser}) {
            require Browser::Open;
            require URI::Escape;

            my $url = "file:" . URI::Escape::uri_escape($temporgfname);
            Browser::Open::open_browser($url);
            [200];
        } else {
            return [200, "OK", File::Slurper::read_text($temporgfname)];
        }

        # backend emacs
    } elsif ($backend eq 'Org::To::HTML') {

        my %oth_args; # args to feed to org_to_html function
        if ($args{input_filename} eq '-') {
            $oth_args{source_str} = do {
                local $/;
                binmode STDIN, ":encoding(utf8)";
                scalar <STDIN>;
            };
        } else {
            $oth_args{source_file} = $args{input_filename};
        }

        $oth_args{ignore_unknown_settings} = 1;
        #$oth_args{naked} = $args{naked};
        #$oth_args{html_title} = $args{title} if defined $args{title};

        require Org::To::HTML;
        my $res = Org::To::HTML::org_to_html(%oth_args);
        return $res unless $res->[0] == 200;

        if ($args{browser}) {
            require Browser::Open;
            require File::Temp;
            require URI::Escape;

            my ($fh, $fpath) = File::Temp::tempfile();
            print $fh $res->[2];
            close $fh;

            my $url = "file:" . URI::Escape::uri_escape($fpath);
            Browser::Open::open_browser($url);
            return [200];
        } else {
            return $res;
        }

    } # backend Org::To::HTML
}

Perinci::CmdLine::Any->new(
    url => '/main/app',
)->run;

# ABSTRACT: Convert Org document to HTML
# PODNAME: org2html

__END__

=pod

=encoding UTF-8

=head1 NAME

org2html - Convert Org document to HTML

=head1 VERSION

This document describes version 0.487 of org2html (from Perl distribution App-OrgUtils), released on 2024-01-09.

=head1 SYNOPSIS

B<org2html> B<L<--help|/"--help, -h, -?">> (or B<L<-h|/"--help, -h, -?">>, B<L<-?|/"--help, -h, -?">>)

B<org2html> B<L<--version|/"--version, -v">> (or B<L<-v|/"--version, -v">>)



( run in 0.836 second using v1.01-cache-2.11-cpan-39bf76dae61 )