#!/usr/bin/env perl
use strict;
use 5.010;

our $VERSION = '0.14';

use Pod::Usage;
pod2usage(1) unless @ARGV;

my $script = pop @ARGV;
my $pipe;

if ( -f $script ) {
    $pipe = sub {
        exec($script) or die "can't exec $script\n";
    };
}
else {
    use Pandoc::Filter::Lazy;
    my $filter = Pandoc::Filter::Lazy->new($script);
    if ( $filter->error ) {
        say STDERR "Failed to compile filter code:\n";
        say STDERR $filter->code( indent => '    ' ) . "\n";
        say STDERR $filter->error;
        exit 1;
    }

    $pipe = sub {
        my $json = <STDIN>;
        exit 1 if substr($json, 0, 1) ne '[';
        my $ast = Pandoc::Elements::pandoc_json($json);
        $filter->apply($ast);
    };
}

my $pid = open( STDIN, "-|" ) // die "cannot fork: $!\n";
if ( $pid == 0 ) {
    exec( 'pandoc', @ARGV, '-t', 'json' ) or die "can't exec pandoc: $!\n";
}
else {
    binmode STDOUT, ':encoding(UTF-8)';
    $pipe->();
}

=head1 NAME

pandocwalk - parse document with pandoc and process abstract syntax tree

=head1 SYNOPSIS

  pandocwalk [ options ] script

Calls pandoc with given options to parse a document and process its abstract
syntax tree. A processing script must be given as executable file or as Perl
code to be passed to function C<pandoc_walk> from Perl module
L<Pandoc::Filter>.

=head2 EXAMPLES

Extract all URLs from a HTML file:

  pandocwalk document.html 'Link => sub{say $_->url}'

Extract table of contents from a LaTeX file:

  pandocwalk document.tex 'Header => sub{say " " x $_->level, $_->string }'

=cut
