/usr/share/perl5/Pandoc.pm is in libpandoc-wrapper-perl 0.6.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | package Pandoc;
use strict;
use warnings;
use 5.010;
use utf8;
=head1 NAME
Pandoc - wrapper for the mighty Pandoc document converter
=cut
our $VERSION = '0.6.1';
use Pandoc::Version;
use Carp 'croak';
use File::Which;
use IPC::Run3;
use parent 'Exporter';
our @EXPORT = qw(pandoc);
our $PANDOC;
our $PANDOC_PATH ||= $ENV{PANDOC_PATH} || 'pandoc';
sub import {
shift;
if (@_ and $_[0] =~ /^[v0-9.<>=!, ]+$/) {
$PANDOC //= Pandoc->new;
$PANDOC->require(shift);
}
$PANDOC //= Pandoc->new(@_) if @_;
Pandoc->export_to_level(1, 'pandoc');
}
sub VERSION {
shift;
$PANDOC //= Pandoc->new;
$PANDOC->require(shift) if @_;
$PANDOC->version;
}
sub new {
my $pandoc = bless { }, shift;
my $bin = (@_ and $_[0] !~ /^-./) ? shift : $PANDOC_PATH;
$pandoc->{bin} = which($bin);
$pandoc->{arguments} = [];
$pandoc->arguments(@_) if @_;
my ($in, $out);
if ($pandoc->{bin}) {
run3 [ $pandoc->{bin},'-v'], \$in, \$out, \undef,
{ return_if_system_error => 1 };
}
croak "pandoc executable not found\n" unless
$out and $out =~ /^[^ ]+ (\d+(\.\d+)+)/;
$pandoc->{version} = Pandoc::Version->new($1);
$pandoc->{data_dir} = $1 if $out =~ /^Default user data directory: (.+)$/m;
# before pandoc supported --list-highlight-languages
if ( $out =~ /^Syntax highlighting is supported/m ) {
$pandoc->{highlight_languages} = [
map { split /\s*,\s*/, $_ } ($out =~ /^ (.+)$/mg)
];
}
my %libs;
my $LIBRARY_VERSION = qr/\s+(\pL\w*(?:-\pL\w*)*)\s+(\d+(?:\.\d+)*),?/;
if ( $out =~ /^Compiled with($LIBRARY_VERSION+)/m ) {
%libs = $1 =~ /$LIBRARY_VERSION/g;
for my $name ( keys %libs ) {
$libs{$name} = Pandoc::Version->new( $libs{$name} );
}
}
$pandoc->{libs} = \%libs;
return $pandoc;
}
sub pandoc(@) { ## no critic
$PANDOC //= eval { Pandoc->new } // 0;
if (@_) {
return $PANDOC ? $PANDOC->run(@_) : -1;
} else {
return $PANDOC;
}
}
sub run {
my $pandoc = shift;
my $args = 'ARRAY' eq ref $_[0] ? \@{shift @_} : undef; # \@args [ ... ]
my $opts = 'HASH' eq ref $_[-1] ? \%{pop @_} : undef; # [ ... ] \%opts
if ( @_ ) {
if ( !$args ) { # @args
if ($_[0] =~ /^-/ or $opts) {
$args = \@_;
} else { # %opts
$opts = { @_ };
}
}
elsif ( $args and !$opts and (@_ % 2 == 0) ) { # \@args [, %opts ]
$opts = { @_ };
}
else {
# passed both the args and opts by ref,
# so other arguments don't make sense;
# or passed args by ref and an odd-length list
croak 'Too many or ambiguous arguments';
}
}
$args //= [];
$opts //= {};
for my $io ( qw(in out err) ) {
$opts->{"binmode_std$io"} //= $opts->{binmode} if $opts->{binmode};
if ( 'SCALAR' eq ref $opts->{$io} ) {
next unless utf8::is_utf8(${$opts->{$io}});
$opts->{"binmode_std$io"} //= ':encoding(UTF-8)';
}
}
$opts->{return_if_system_error} //= 1;
$args = [ $pandoc->{bin}, @{$pandoc->{arguments}}, @$args ];
run3 $args, $opts->{in}, $opts->{out}, $opts->{err}, $opts;
return $? == -1 ? -1 : $? >> 8;
}
sub convert {
my $pandoc = shift;
my $from = shift;
my $to = shift;
my $in = shift;
my $out = "";
my $err = "";
my $utf8 = utf8::is_utf8($in);
my %opts = (in => \$in, out => \$out, err => \$err);
my @args = (@_, '-f' => $from, '-t' => $to, '-o' => '-');
my $status = $pandoc->run( \@args, \%opts );
croak($err || "pandoc failed with exit code $status") if $status;
utf8::decode($out) if $utf8;
chomp $out;
return $out;
}
sub parse {
my $pandoc = shift;
my $format = shift;
my $json = "";
if ($format eq 'json') {
$json = shift;
} else {
$pandoc->require('1.12.1');
$json = $pandoc->convert( $format => 'json', @_ );
}
require Pandoc::Elements;
Pandoc::Elements::pandoc_json($json);
}
sub file {
my $pandoc = shift;
$pandoc->require('1.12.1');
my ($json, $err);
my @args = (@_, '-t' => 'json', '-o' => '-');
my $status = $pandoc->run( \@args, out => \$json, err => \$err );
croak($err || "pandoc failed with exit code $status") if $status;
#utf8::decode($out) if $utf8;
require Pandoc::Elements;
Pandoc::Elements::pandoc_json($json);
}
sub require {
my $pandoc = shift;
$pandoc = do { $PANDOC //= Pandoc->new } if $pandoc eq 'Pandoc';
unless ($pandoc->version(@_)) {
croak "pandoc $_[0] required, only found ".$pandoc->{version}."\n"
}
return $pandoc;
}
sub version {
my $pandoc = shift or return;
my $version = $pandoc->{version} or return;
# compare against given version
return if @_ and not $version->fulfills(@_);
return $version;
}
sub data_dir {
$_[0]->{data_dir};
}
sub bin {
my $pandoc = shift;
if (@_) {
my $new = Pandoc->new(shift);
$pandoc->{$_} = $new->{$_} for (qw(version bin data_dir));
}
$pandoc->{bin};
}
sub arguments {
my $pandoc = shift;
if (@_) {
my $args = 'ARRAY' eq ref $_[0] ? shift : \@_;
croak "first default argument must be an -option"
if @$args and $args->[0] !~ /^-./;
$pandoc->{arguments} = $args;
}
@{$pandoc->{arguments}};
}
sub _list {
my ($pandoc, $which) = @_;
if (!$pandoc->{$which}) {
if ($pandoc->version('1.18')) {
my $list = "";
my $command = $which;
$command =~ s/_/-/g;
$pandoc->run("--list-$command", { out => \$list });
$pandoc->{$which} = [ split /\n/, $list ];
} elsif (!defined $pandoc->{help}) {
my $help;
$pandoc->run('--help', { out => \$help });
for my $inout (qw(Input Output)) {
$help =~ /^$inout formats:\s+([a-z_0-9,\+\s*]+)/m or next;
$pandoc->{lc($inout).'_formats'} =
[ split /\*?,\s+|\*?\s+/, $1 ];
}
$pandoc->{help} = $help;
}
}
@{$pandoc->{$which} // []};
}
sub input_formats {
$_[0]->_list('input_formats');
}
sub output_formats {
$_[0]->_list('output_formats');
}
sub highlight_languages {
$_[0]->_list('highlight_languages');
}
sub libs {
$_[0]->{libs};
}
1;
__END__
=encoding utf-8
=begin markdown
# STATUS
[![Build Status](https://travis-ci.org/nichtich/Pandoc-Wrapper.svg)](https://travis-ci.org/nichtich/Pandoc-Wrapper)
[![Coverage Status](https://coveralls.io/repos/nichtich/Pandoc-Wrapper/badge.svg)](https://coveralls.io/r/nichtich/Pandoc-Wrapper)
[![Kwalitee Score](http://cpants.cpanauthors.org/dist/Pandoc.png)](http://cpants.cpanauthors.org/dist/Pandoc)
=end markdown
=head1 SYNOPSIS
use Pandoc; # check at first use
use Pandoc 1.12; # check at compile time
Pandoc->require(1.12); # check at run time
# execute pandoc
pandoc 'input.md', -o => 'output.html';
pandoc -f => 'html', -t => 'markdown', { in => \$html, out => \$md };
# alternative syntaxes
pandoc->run('input.md', -o => 'output.html');
pandoc [ -f => 'html', -t => 'markdown' ], in => \$html, out => \$md;
pandoc [ -f => 'html', -t => 'markdown' ], { in => \$html, out => \$md };
# check executable
pandoc or die "pandoc executable not found";
# check minimum version
pandoc->version > 1.12 or die "pandoc >= 1.12 required";
# access properties
say pandoc->bin." ".pandoc->version;
say "Default user data directory: ".pandoc->data_dir;
say "Compiled with: ".join(", ", keys %{ pandoc->libs });
say pandoc->libs->{'highlighting-kate'};
# create a new instance with default arguments
my $md2latex = Pandoc->new(qw(-f markdown -t latex --smart));
$md2latex->run({ in => \$markdown, out => \$latex });
# set default arguments on compile time
use Pandoc qw(-t latex);
use Pandoc qw(/usr/bin/pandoc --smart);
use Pandoc qw(1.16 --smart);
# utility method to convert from string
$latex = pandoc->convert( 'markdown' => 'latex', '*hello*' );
# utility methods to parse abstract syntax tree (requires Pandoc::Elements)
$doc = pandoc->parse( markdown => '*hello* **world!**' );
$doc = pandoc->file( 'example.md' );
$doc = pandoc->file; # read Markdown from STDIN
=head1 DESCRIPTION
This module provides a Perl wrapper for John MacFarlane's
L<Pandoc|http://pandoc.org> document converter.
=head2 IMPORTING
The utility function L<pandoc|/pandoc> is exported, unless the module is
imported with an empty list (C<use Pandoc ();>).
Importing this module with a version number or a more complex version
requirenment (e.g. C<use Pandoc 1.13;> or C<< use Pandoc '>= 1.6, !=1.7 >>)
will check version number of pandoc executable instead of version number of
this module (see C<$Pandoc::VERSION> for the latter). Additional import
arguments can be passed to set the executable location and default arguments of
the global Pandoc instance used by function pandoc.
=head1 FUNCTIONS
=head2 pandoc
If called without parameters, this function returns a global instance of class
Pandoc to execute L<methods|/METHODS>, or C<undef> if no pandoc executable was
found. The location and/or name of pandoc executable can be set with
environment variable C<PANDOC_PATH> (set to the string C<pandoc> by default).
=head2 pandoc( ... )
If called with parameters, this functions runs the pandoc executable configured
at the global instance of class Pandoc (C<< pandoc->bin >>). Arguments are
passed as command line arguments and options control input, output, and error
stream as described below. Returns C<0> on success. Otherwise returns the the
exit code of pandoc executable or C<-1> if execution failed. Arguments and
options can be passed as plain array/hash or as (possibly empty) reference in
the following ways:
pandoc @arguments, \%options; # ok
pandoc \@arguments, %options; # ok
pandoc \@arguments, \%options; # ok
pandoc @arguments; # ok, if first of @arguments starts with '-'
pandoc %options; # ok, if %options is not empty
pandoc @arguments, %options; # not ok!
=head3 Options
=over
=item in / out / err
These options correspond to arguments C<$stdin>, C<$stdout>, and
C<$stderr> of L<IPC::Run3>, see there for details.
=item binmode_stdin / binmode_stdout / binmode_stderr
These options correspond to the like-named options to L<IPC::Run3>, see
there for details.
=item binmode
If defined any binmode_stdin/binmode_stdout/binmode_stderr option which
is undefined will be set to this value.
=item return_if_system_error
Set to true by default to return the exit code of pandoc executable.
=back
For convenience the C<pandoc> function (I<after> checking the C<binmode>
option) checks the contents of any scalar references passed to the
in/out/err options with
L<< utf8::is_utf8()|utf8/"* C<$flag = utf8::is_utf8($string)>" >>
and sets the binmode_stdin/binmode_stdout/binmode_stderr options to
C<:encoding(UTF-8)> if the corresponding scalar is marked as UTF-8 and
the respective option is undefined. Since all pandoc executable
input/output must be UTF-8 encoded this is convenient if you run with
L<use utf8|utf8>, as you then don't need to set the binmode options at
all (L<encode nor decode|Encode>) when passing input/output scalar
references.
=head1 METHODS
=head2 new( [ $executable ] [, @arguments ] )
Create a new instance of class Pandoc or throw an exception if no pandoc
executable was found. The first argument, if given and not starting with C<->,
can be used to set the pandoc executable (C<pandoc> by default). Additional
arguments are passed to the executable on each run.
Repeated use of this constructor with same arguments is not recommended because
C<pandoc --version> is called for every new instance.
=head2 run( ... )
Execute the pandoc executable with default arguments and optional additional
arguments and options. See L<function pandoc|/FUNCTIONS> for usage.
=head2 convert( $from => $to, $input [, @arguments ] )
Convert a string in format C<$from> to format C<$to>. Additional pandoc options
such as C<--smart> and C<--standalone> can be passed. The result is returned
in same utf8 mode (C<utf8::is_unicode>) as the input. To convert from file to
string use method C<pandoc>/C<run> like this and set input/output format via
standard pandoc arguments C<-f> and C<-t>:
pandoc->run( $filename, @arguments, { out => \$string } );
=head2 parse( $from => $input [, @arguments ] )
Parse a string in format C<$from> to a L<Pandoc::Document> object. Additional
pandoc options such as C<--smart> and C<--normalize> can be passed. This method
requires at least pandoc version 1.12.1 and the Perl module L<Pandoc::Elements>.
The reverse action is possible with method C<to_pandoc> of L<Pandoc::Document>.
Additional shortcut methods such as C<to_html> are available:
$html = pandoc->parse( 'markdown' => '# A *section*' )->to_html;
Method C<convert> should be preferred for simple conversions unless you want to
modify or inspect the parsed document in between.
=head2 file( [ $filename [, @arguments ] ] )
Parse from a file (or STDIN) to a L<Pandoc::Document> object. Additional pandoc
options can be passed, for instance use HTML input format (C<@arguments = qw(-f
html)>) instead of default markdown. This method requires at least pandoc
version 1.12.1 and the Perl module L<Pandoc::Elements>.
=head2 require( $version_requirement )
Return the Pandoc instance if its version number fulfills a given version
requirement. Throw an error otherwise. Can also be called as constructor:
C<< Pandoc->require(...) >> is equivalent to C<< pandoc->require >> but
throws a more meaningful error message if no pandoc executable was found.
=head2 version( [ $version_requirement ] )
Return the pandoc version as L<Pandoc::Version> object. If a version
requirement is given, the method returns undef if the pandoc version does not
fulfill this requirement. To check whether pandoc is available with a given
minimal version use one of:
Pandoc->require( $minimum_version) # true or die
pandoc and pandoc->version( $minimum_version ) # true or false
=head2 bin( [ $executable ] )
Return or set the pandoc executable. Setting an new executable also updates
version and data_dir by calling C<pandoc --version>.
=head2 arguments( [ @arguments | \@arguments )
Return or set a list of default arguments.
=head2 data_dir
Return the default data directory (only available since Pandoc 1.11).
=head2 input_formats
Return a list of supported input formats.
=head2 output_formats
Return a list of supported output formats.
=head2 highlight_languages
Return a list of programming languages which syntax highlighting is supported
for (via Haskell library highlighting-kate).
=head2 libs
Return a hash mapping the names of Haskell libraries compiled into the
pandoc executable to L<Pandoc::Version> objects.
=head1 SEE ALSO
See L<Pandoc::Elements> for a Perl interface to the abstract syntax tree of
Pandoc documents for more elaborate document processing.
See L<Pandoc wrappers and interfaces|https://github.com/jgm/pandoc/wiki/Pandoc-wrappers-and-interfaces>
in the Pandoc GitHub Wiki for a list of wrappers in other programming
languages.
Other Pandoc related but outdated modules at CPAN include
L<Orze::Sources::Pandoc> and L<App::PDoc>.
=head1 AUTHOR
Jakob Voß
=head1 CONTRIBUTORS
Benct Philip Jonsson
=head1 LICENSE
GNU General Public License, Version 2
=cut
|