Pandoc::Walker - utility functions to traverse Pandoc documents
use Pandoc::Walker; use Pandoc::Elements qw(pandoc_json); my $ast = pandoc_json(<>); # extract all links my $links = query $ast, sub { my $e = shift; return unless ($e->name eq 'Link' or $e->name eq 'Image'); return $e->url; }; # print all links walk $ast, sub { my $e = shift; return unless ($e->name eq 'Link' or $e->name eq 'Image'); print $e->url; }; # remove of all links transform $ast, sub { return ($_[0]->name eq 'Link' ? [] : ()); }; # replace all links by their link text angle brackets use Pandoc::Elements 'Str'; transform $ast, sub { my $elem = $_[0]; return unless $elem->name eq 'Link'; return (Str "<", $elem->content->[0], Str ">"); };
This module provides to helper 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, sub { my $header = shift; return unless $header->name eq 'Header'; transform [$header], sub { # make an array my $link = shift; return unless $link->name eq 'Link'; return $e->content; # is an array }; };
See also Pandoc::Filter for an object oriented interface to transformations.
Walks an abstract syntax tree and calls an action on every element. Additional arguments are also passed to the action.
Walks an abstract syntax tree and applies a query function to extract results. The query function is expected to return a list. The combined query result is returned as array reference.
Walks an abstract syntax tree and applies an action on every element to either keep it (if the action returns 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
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.