Pandoc::Filter - process Pandoc abstract syntax tree
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] ]; };
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.
Create a new filter with one or more action functions, given as code reference(s).
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).
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;
Walks the ast and returns concatenated string content, leaving out all formatting.
Pandoc::Elements, Pandoc::Walker
To install Pandoc::Elements, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Pandoc::Elements
CPAN shell
perl -MCPAN -e shell install Pandoc::Elements
For more information on module installation, please visit the detailed CPAN module installation guide.