App-OrgUtils

 view release on metacpan or  search on metacpan

script/org2html  view on Meta::CPAN

_
    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">>)

B<org2html> [B<L<--backend|/"--backend=s, -B">>=I<str>|B<L<-B|/"--backend=s, -B">>=I<str>] [B<L<--browser|/"--browser, -b">>|B<L<-b|/"--browser, -b">>] [B<L<--config-path|/"--config-path=s, -c">>=I<path>|B<L<-c|/"--config-path=s, -c">>|B<L<--no-confi...

=head1 DESCRIPTION

This is a more user-friendly CLI script for L<Org::To::HTML> compared to
L<org-to-html> which is a direct CLI version of the function C<org_to_html>.

=head1 OPTIONS

C<*> marks required options.

=head2 Main options

=over

=item B<--backend>=I<s>, B<-B>

Default value:

 "emacs"

Valid values:

 ["Org::To::HTML","emacs"]



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