The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Pandoc::Filter - process Pandoc abstract syntax tree

SYNOPSIS

The following filter, adopted from [pandoc scripting documentation](http://johnmacfarlane.net/pandoc/scripting.html) converts level 2+ headers to regular paragraphs.

    use Pandoc::Filter;
    use Pandoc::Elements;

    pandoc_filter sub {
        return unless ($_[0]->name eq 'Header' and $_[0]->value->[0] >= 2);
        return Para [ Emph $_[0]->value->[2] ];
    };

[Signatures](http://perldoc.perl.org/perlsub.html#Signatures), as introduced in Perl 5.20, can be used for better readability:

    use Pandoc::Filter;
    use experimental 'signatures';

    pandoc_filter sub ($elem, $format, $meta) {
        return unless ($elem->name eq 'Header' and $elem->value->[0] >= 2);
        return Para [ Emph $elem->value->[2] ];
    };

DESCRIPTION

Pandoc::Filter is a port of pandocfilters from Python to modern Perl. The module provide provides functions to aid writing Perl scripts that process a Pandoc abstract syntax tree (AST) serialized as JSON. See also Pandoc::Elements and Pandoc::Walker for modules that filters are based on.

METHODS

new( @action )

Create a new filter with one or more action functions, given as code reference(s).

apply( $ast [, $format [ $metadata ] ] )

Apply all actions to a given abstract syntax tree. The syntax tree is modified in place and also returned for convenience. Additional argument format and metadata are also passed to the action function. Metadata is taken from the Document by default (if the AST is a Document root).

FUNCTIONS

pandoc_filter( @action )

Read a single line of JSON from STDIN, apply actions and print the resulting AST as single line of JSON. This function is roughly equivalent to

    my $ast = Pandoc::Elements::from_json(<>);
    Pandoc::Filter->new(@action)->apply($ast);
    say $ast->to_json;

stringify( $ast )

Walks the ast and returns concatenated string content, leaving out all formatting.

SEE ALSO

Pandoc::Elements, Pandoc::Walker