Pandoc::Walker - utility functions to process Pandoc documents
use Pandoc::Walker; use Pandoc::Elements qw(pandoc_json); my $ast = pandoc_json(<>); # extract all links and image URLs my $links = query $ast, 'Link|Image' => sub { $_->url }; # print all links and image URLs walk $ast, 'Link|Image' => sub { say $_->url }; # remove all links transform $ast, sub { return ($_->name eq 'Link' ? [] : ()); }; # replace all links by their link text angle brackets use Pandoc::Elements 'Str'; transform $ast, Link => sub { return (Str " < ", $_->content->[0], Str " > "); };
This module provides utility functions to traverse the abstract syntax tree (AST) of a pandoc document (see Pandoc::Elements for documentation of AST elements).
Document elements are passed to action functions by reference, so don't shoot yourself in the foot by trying to directly modify the element. Traversing a single element is not reliable neither, so put the element in an array reference if needed. For instance to replace links in headers only by their link text content:
transform $ast, Header => sub { transform [ $_[0] ], Link => sub { # make an array return $_[0]->content; # is an array }; };
See also Pandoc::Filter for an object oriented interface to transformations.
Return an an action function to process document elements.
Walks an abstract syntax tree and calls an action on every element or every element of given name(s). Additional arguments are also passed to the action.
If and only if the action function returns \undef the current element is not traversed further.
\undef
See also function pandoc_walk exported by Pandoc::Filter.
pandoc_walk
Walks an abstract syntax tree and applies one or multiple query functions to extract results. The query function is expected to return a list or \undef. The combined query result is returned as array reference. For instance the string method of Pandoc::Elements is implemented as following:
string
join '', @{ query( $ast, { 'Str|Code|Math' => sub { $_->content }, 'LineBreak|Space' => sub { ' ' } } );
Walks an abstract syntax tree and applies an action on every element, or every element of given name(s), to either keep it (if the action returns undef or \undef), remove it (if it returns an empty array reference), or replace it with one or more elements (returned by array reference or as single value).
undef
See also function pandoc_filter exported by Pandoc::Filter.
pandoc_filter
Copyright 2014- Jakob Voß
GNU General Public License, Version 2
This module is heavily based on Pandoc by John MacFarlane.
Haskell module Text.Pandoc.Walk for the original.
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.