NAME
Pandoc::Walker - utility functions to traverse Pandoc documents
SYNOPSIS
use Pandoc::Walker;
use JSON;
my $ast = decode_json(<>);
# extract all links
my $links = query $ast, sub {
my ($name, $value) = ($_[0]->name, $_[0]->value);
return unless ($name eq 'Link' or $name eq 'Image');
return $value->[1][0];
};
# print all links
walk $ast, sub {
my ($name, $value) = ($_[0]->name, $_[0]->value);
return unless ($key eq 'Link' or $key eq 'Image');
print $value->[1][0];
};
# 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->value->[0], Str ">");
};
DESCRIPTION
This module provides to helper functions to traverse the abstract syntax tree of a pandoc document.
FUNCTIONS
walk( $ast, $action [, @arguments ] )
Walks an abstract syntax tree and calls an action on every element. Additional arguments are also passed to the action.
query( $ast, $query [, @arguments ] )
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.
transform( $ast, $action [, @arguments ] )
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).
AUTHOR
Jakob Voß <jakob.voss@gbv.de>
COPYRIGHT AND LICENSE
Copyright 2014- Jakob Voß
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
Pandoc::Elements for utility functions to build abstract syntax trees of pandoc documents.
Pandoc::Filter for a higher level application.
Haskell module Text.Pandoc.Walk for the original.