Pandoc::Elements - create and process Pandoc documents
The output of this script hello.pl
hello.pl
use Pandoc::Elements; use JSON; print Document({ title => MetaInlines [ Str "Greeting" ] }, [ Header( 1, attributes { id => 'top' }, [ Str 'Hello' ] ), Para [ Str 'Hello, world!' ], ])->to_json;
can be converted for instance to HTML with via
./hello.pl | pandoc -f json -t html5 --standalone
an equivalent Pandoc Markdown document would be
% Greeting # Gruß {.de} Hello, world!
See module Pandoc::Filter and Pandoc::Walker for processing the abstract syntax tree of pandoc documents in Perl.
Pandoc::Elements provides utility functions to create abstract syntax trees (AST) of Pandoc documents. The resulting data structure can be converted by Pandoc to many other document formats, such as HTML, LaTeX, ODT, and ePUB.
In addition to constructor functions for each document element, the following functions are exported.
Parse a JSON string, as emitted by pandoc JSON format.
Maps a hash reference into an attributes list with id, classes, and ordered key-value pairs. The special keys id and classes are recognized but setting multi-value attributes or controlled order is not supported with this function. You can always manually create an attributes structure:
id
classes
[ $id, [ @classes ], [ key => $value, ... ] ]
Elements with attributes (element accessor method attr) also provide the accessor method id, classes, and class. See Hash::MultiValue for easy access to key-value-pairs.
attr
class
Create a Pandoc document element. This function is only exported on request.
AST elements are encoded as Perl data structures equivalent to the JSON structure, emitted with pandoc output format json. All elements are blessed objects that provide the following element methods and additional accessor methods specific to each element.
json
Return the element as JSON encoded string. The following are equivalent:
$element->to_json; JSON->new->utf8->convert_blessed->encode($element);
Return the name of the element, e.g. "Para" for a paragraph element.
Return the element content. For most elements (Para, Emph, Str...) the content is an array reference with child elements. Other elements consist of multiple parts; for instance the Link element has a link text (content) and a link target (target) with url and title.
content
target
url
title
True if the element is a Block element
True if the element is an inline Inline element
True if the element is a Metadata element
True if the element is a Document element
Walk the element tree with Pandoc::Walker
Query the element to extract results with Pandoc::Walker
Transform the element tree with Pandoc::Walker
Block quote, consisting of a list of blocks (content)
Unnumbered list of items (content=items), each a list of blocks
items
Code block (literal string content) with attributes (attr)
Definition list, consisting of a list of pairs (content=items), each a term (term, a list of inlines) and one or more definitions (definitions, a list of blocks).
term
definitions
Generic container of blocks (content) with attributes (attr).
Header with level (integer), attributes (attr), and text (content, a list of inlines).
level
Horizontal rule
Nothing
Definition list of items/content, each a pair consisting of a term (a list of inlines) and one or more definitions (each a list of blocks).
Paragraph, consisting of a list of Inline elements (content).
Plain text, not a paragraph, consisting of a list of Inline elements (content).
Raw block with format and content string.
format
Table, with caption, column alignments, relative column widths (0 = default), column headers (each a list of blocks), and rows (each a list of lists of blocks).
caption
alignments
widths
headers
rows
Citation, a list of inlines (content) and a list of citations.
citations
Inline code, a literal string (content) with attributes (attr)
Code attributes { %attr }, $content
Emphasized text, a list of inlines (content).
Emph [ @inlines ]
Image with alt text (content, a list of inlines) and target (list of url and title).
Image [ @inlines ], [ $url, $title ]
Hard line break
LineBreak
Hyperlink with link text (content, a list of inlines) and target (list of url and title).
Link [ @inlines ], [ $url, $title ]
TeX math, given as literal string (content) with type (one of DisplayMath and InlineMath).
type
DisplayMath
InlineMath
Math $type, $content
Footnote or Endnote, a list of blocks (content).
Note [ @blocks ]
Quoted text with quote type (one of SingleQuote and DoubleQuote) and a list of inlines) (content).
SingleQuote
DoubleQuote
Quoted $type, [ $inlines ]
Raw inline with format (a string) and content (a string).
RawInline $format, $content
Small caps text, a list of inlines) (content).
SmallCaps [ @inlines ]
Inter-word space
Space
Generic container of inlines (content) with attributes (attr).
Span attributes { %attr }, [ @inlines ]
Plain text, a string (content).
Str $text
Strikeout text, a list of inlines) (content).
Strikeout [ @inlines ]
Strongly emphasized text, a list of inlines) (content).
Strong [ @inlines ]
Subscripted text, a list of inlines) (content).
Supscript [ @inlines ]
Superscripted text, a list of inlines) (content).
Superscript [ @inlines ]
Root element, consisting of metadata hash (meta) and document element array (content).
meta
The following elements are used as types only:
DefaultDelim, Period, OneParen, TwoParens, SingleQuote, DoubleQuote, DisplayMath, InlineMath, AuthorInText, SuppressAuthor, NormalCitation, AlignLeft, AlignRight, AlignCenter, AlignDefault, DefaultStyle, Example, Decimal, LowerRoman, UpperRoman, LowerAlpha, UpperAlpha
DefaultDelim
Period
OneParen
TwoParens
AuthorInText
SuppressAuthor
NormalCitation
AlignLeft
AlignRight
AlignCenter
AlignDefault
DefaultStyle
Example
Decimal
LowerRoman
UpperRoman
LowerAlpha
UpperAlpha
Pandoc implements a wrapper around the pandoc executable.
Text.Pandoc.Definition contains the original definition of Pandoc document data structure in Haskell. This module version was last aligned with pandoc-types-1.12.4.1.
Jakob Voß <jakob.voss@gbv.de>
Copyright 2014- Jakob Voß
GNU General Public License, Version 2
This module is heavily based on Pandoc by John MacFarlane.
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.