Documentation ¶
Overview ¶
Package pandoc implements the Pandoc AST as defined in Pandoc Types.
Index ¶
- Constants
- Variables
- func Clone[P Element](elt P) P
- func Filter[P any, E Element, R Element](elt E, fun func(P) ([]R, error)) (E, error)
- func Fprint(w io.Writer, e Element) error
- func Index[E Element, L Element](lst []L) (int, E)
- func Index2[E1 Element, E2 Element, L Element](lst []L) (int, E1, E2)
- func Index3[E1 Element, E2 Element, E3 Element, L Element](lst []L) (int, E1, E2, E3)
- func InlinesToIdent(inlines []Inline) string
- func Is[P any, S Element](elt S) bool
- func Match[T Element, E Element](m T, e E) (T, bool)
- func Print(e Element) error
- func Query[P any, E Element](elt E, fun func(P))
- func QueryE[P any, E Element](elt E, fun func(P) error) error
- func Sprint(e Element) string
- func StoreFile(f string, conf Conf, meta Meta, docs ...*Pandoc) error
- func StoreTo(w io.Writer, conf Conf, meta Meta, docs ...*Pandoc) error
- func StringToIdent(s string) string
- func Transformer[E Element, P any, R Element](fun func(P) ([]R, error)) func(E) (E, error)
- type Alignment
- type Attr
- func (a *Attr) Get(key string) (string, bool)
- func (a *Attr) HasClass(c string) bool
- func (a *Attr) HasOneOfClasses(c ...string) bool
- func (a *Attr) Ident() string
- func (a *Attr) SetIdent(id string)
- func (a Attr) WithClass(c string) Attr
- func (a Attr) WithIdent(id string) Attr
- func (a Attr) WithKV(key, value string) Attr
- func (a Attr) WithKVs(pairs ...string) Attr
- func (a Attr) WithoutClass(c string) Attr
- func (a Attr) WithoutKey(key string) Attr
- func (a Attr) WithoutKeys(keys ...string) Attr
- type Block
- type BlockQuote
- type BulletList
- type Caption
- type Citation
- type CitationMode
- type Cite
- type Code
- type CodeBlock
- type ColSpec
- type ColWidth
- type Conf
- type Definition
- type DefinitionList
- type Div
- type Element
- type Emph
- type Figure
- type Header
- type HorizontalRule
- type Image
- type Inline
- type KV
- type LineBlock
- type LineBreak
- type Link
- type Linkable
- type ListAttrs
- type ListNumberDelim
- type ListNumberStyle
- type Math
- type MathType
- type Meta
- type MetaBlocks
- type MetaBool
- type MetaInlines
- type MetaList
- type MetaMap
- type MetaMapEntry
- type MetaString
- type MetaValue
- type Note
- type OrderedList
- type Pandoc
- type Para
- type Plain
- type QuoteType
- type Quoted
- type RawBlock
- type RawInline
- type SmallCaps
- type SoftBreak
- type Space
- type Span
- type Str
- type Strikeout
- type Strong
- type Subscript
- type Superscript
- type Table
- type TableBody
- type TableCell
- type TableHeadFoot
- type TableRow
- type Tag
- type Tagged
- type Target
- type Underline
- type WhiteSpace
Constants ¶
const BlockQuoteTag = Tag("BlockQuote")
const BulletListTag = Tag("BulletList")
const CiteTag = Tag("Cite")
const CodeBlockTag = Tag("CodeBlock")
const CodeTag = Tag("Code")
const DefinitionListTag = Tag("DefinitionList")
const DivTag = Tag("Div")
const EmphTag = Tag("Emph")
const FigureTag = Tag("Figure")
const HeaderTag = Tag("Header")
const HorizontalRuleTag = Tag("HorizontalRule")
const ImageTag = Tag("Image")
const LineBlockTag = Tag("LineBlock")
const LineBreakTag = Tag("LineBreak")
const LinkTag = Tag("Link")
const MathTag = Tag("Math")
const MetaBlocksTag = Tag("MetaBlocks")
const MetaBoolTag = Tag("MetaBool")
const MetaInlinesTag = Tag("MetaInlines")
const MetaListTag = Tag("MetaList")
const MetaMapTag = Tag("MetaMap")
const MetaStringTag = Tag("MetaString")
const NoteTag = Tag("Note")
const OrderedListTag = Tag("OrderedList")
const ParaTag = Tag("Para")
const PlainTag = Tag("Plain")
const QuotedTag = Tag("Quoted")
const RawBlockTag = Tag("RawBlock")
const RawInlineTag = Tag("RawInline")
const SmallCapsTag = Tag("SmallCaps")
const SoftBreakTag = Tag("SoftBreak")
const SpaceTag = Tag("Space")
const SpanTag = Tag("Span")
const StrTag = Tag("Str")
const StrikeoutTag = Tag("Strikeout")
const StrongTag = Tag("Strong")
const SubscriptTag = Tag("Subscript")
const SuperscriptTag = Tag("Superscript")
const TableTag = Tag("Table")
const UnderlineTag = Tag("Underline")
const Version = "1.23.1"
Implemented Pandoc protocol version.
Variables ¶
var ( // Continue indicates that the traversal should continue, including // children of the current element. Continue = func() error { return traversalResult(0) }() // Skip indicates that the traversal should continue, but skip processing // children of the current element. Skip = func() error { return traversalResult(skipChildren) }() // Halt indicates that the traversal should stop. Halt = func() error { return traversalResult(haltTraversal) }() // ReplaceContinue indicates that the current element should be // replaced with the elements returned by the function, and the // function should be applied to the children of the returned elements. ReplaceContinue = func() error { return traversalResult(replaceElement) }() // ReplaceSkip indicates that the current element should be replaced with the // elements returned by the function. No children of the returned elements // will be processed, but the traversal will continue with the siblings of // the current element (if any). ReplaceSkip = func() error { return traversalResult(replaceElement | skipChildren) }() // ReplaceHalt indicates that the current element should be replaced with the // elements returned by the function, and the traversal should stop. // No children of the returned elements will be processed. // Might be useful to change or remove the only specific element in the AST. ReplaceHalt = func() error { return traversalResult(replaceElement | haltTraversal) }() // Returned by Filter if the function returns the wrong type. ErrUnexpectedType = errors.New("unexpected type") )
var DefaultFormat = Conf{
Format: "markdown",
}
var HR = &HorizontalRule{}
var LB = &LineBreak{}
var SB = &SoftBreak{}
var SP = &Space{}
Functions ¶
func Clone ¶
func Clone[P Element](elt P) P
Returs a shallow copy of an element. Intended for use in Filter.
func Filter ¶
Filter applies the specified function 'fun' to each child element of the provided element 'elt'. The function 'fun' is not applied to 'elt' itself, even if 'elt's type matches the parameter type of 'fun'.
The parameter type P should be the same as or implement the return type R. This relationship is not enforced by the type system. If this condition is not met, the filter operation will still execute, but the intended modifications may not be applied.
The parameter type P must be also a slice of R. In this case the function will be applied to all the lists of Inlines, or Blocks, (but not both). In latter case the order of traversal is reverted: fun will be applied to all the children and the to the current element's list.
The behavior of the filter depends on the error returned by 'fun':
- TraverseChildren: Continie traversing including children of the current element.
- SkipChildren: Continue traversing AST, but skip processing children of the current element.
- StopTraversal: Skips processing children of the current element and terminates the traversal process immediately.
- Replace: Replaces the current element with the elements returned by 'fun'.
- any other error: Terminates the traversal process immediately and returns the error.
To remove an element, 'fun' should return an empty slice of elements along with Replace.
The function returns an updated version of the 'elt' after applying the specified function 'fun' (it might be the same 'elt' if no changes were made).
Example:
import . "github.com/growler/go-pandoc/cons" doc = pandoc.Filter(doc, func (str *pandoc.Str) ([]pandoc.Inline, error) { return Inlines(Quoted(SingleQuote, Str("foo"))), Replace })
func Index ¶
Index returns index of the first element of type E in the list of elements implementing interface L (either Block or Inline), and the element itself. Returns -1, nil if []L does not contain any element of type E
Example:
Filter(doc, func(lst []Inline) ([]Inline, error) { ... if span, idx := Index[*Span](lst); idx >= 0 {
func Index2 ¶
Index2 returns index of the first sequence of elements of types E1 and E2 in the list of elements implementing interface L (either Block or Inline), and the elements themselves. Returns -1, nil, nil if []L does not contain any element of type E
Example:
Filter(doc, func(lst []Block) ([]Block, error) { ... if para, block, idx := Index2[*Plain, *CodeBlock](lst); idx >= 0 {
func Index3 ¶
Index3 returns index of the first sequence of elements of types E1, E2 and E3 in the list of elements implementing interface L (either Block or Inline), and the elements themselves. Returns -1, nil, nil, nil if []L does not contain any element of type E
Example:
Filter(doc, func(lst []Inline) ([]Inline, error) { ... if _, str, _, idx := Index3[*Space, *Str, *Space](lst); idx >= 0 {
func InlinesToIdent ¶
Converts list of inlines to identifier.
func Is ¶
A convenience function to check if an element is of a particular type.
Example:
if pandoc.Is[*pandoc.Str](elt) { ... if pandoc.Is[pandoc.Inline](elt) { ...
func Match ¶
Matches element E of type E agains template m of type T. Returns T and true if e matches. Does not modify m.
Example:
var tmpl = &Para{[]Inline{&Code{}, &Link{}}} Query(doc, func(e Block) { if para, ok := Match(tmpl, e); ok { ... // para is Para that consists of a single Code followed by Link
func Query ¶
Query works the same way as QueryE, but fun does not return errors and traverse all the AST.
func QueryE ¶
QueryE applies the specified function 'fun' to each child element of the provided element 'elt'. The function 'fun' is not applied to 'elt' itself, regardless of whether 'elt's type matches the parameter type of 'fun'.
This function is used for walking through the child elements of 'elt' and applying the function 'fun' to perform checks or actions, without altering the structure of 'elt'. It is particularly useful for operations like searching or validation where modification of the element is not required.
The function 'fun' returns an error to control the traversal process:
- Continue: Continie traversing the tree. Also nil works as well.
- Skip: Skips processing children of the current element.
- SkipAll: Skips processing children of the current element and terminates the traversal process immediately.
- Replace: Not allowed. The function should not return this error. However, works as Continue.
- any other error: Terminates the traversal process immediately and returns the error.
Unlike Filter, Query does not modify the element 'elt' or its children. It strictly performs read-only operations as defined in 'fun'.
The fun may update the elements in-place, however this must be used with caution.
Example:
var headers int pandoc.Query(doc, func (str *pandoc.Header) { headers++ }) pandoc.QueryE(doc, func (str *pandoc.Header) error { })
func Transformer ¶
Takes a filter function and returns a transformer function that can be used with element.Apply.
Example:
doc.Apply( pandoc.Transformer[*pandoc.Pandoc](func (e *pandoc.Span) ([]pandoc.Inline, error) { ... }), pandoc.Transformer[*pandoc.Pandoc](func (e *pandoc.Header) ([]pandoc.Block, error) { ... }), )
Types ¶
type Attr ¶
type Attr struct { Id string // Element ID Classes []string // Element classes KVs []KV // Element attributes' key-value pairs }
Pandoc elements attribute.
func (*Attr) HasOneOfClasses ¶
Returns true if attribute has one of the given classes.
func (*Attr) SetIdent ¶
Sets the element's ID in-place. This method is intended to quickly modify an element's ID in Query or QueryE without cloning it.
func (Attr) WithoutClass ¶
Returns a copy of attributes without the given class.
func (Attr) WithoutKey ¶
Returns a copy of attributes without the given key.
func (Attr) WithoutKeys ¶
Returns a copy of attributes without the given keys.
type BlockQuote ¶
type BlockQuote struct {
Blocks []Block
}
Block quote (list of blocks)
func (*BlockQuote) Apply ¶
func (b *BlockQuote) Apply(transformers ...func(*BlockQuote) (*BlockQuote, error)) (*BlockQuote, error)
func (*BlockQuote) Tag ¶
func (b *BlockQuote) Tag() Tag
type BulletList ¶
type BulletList struct {
Items [][]Block
}
Bullet list (list of items, each a list of blocks)
func (*BulletList) Apply ¶
func (l *BulletList) Apply(transformers ...func(*BulletList) (*BulletList, error)) (*BulletList, error)
func (*BulletList) Tag ¶
func (l *BulletList) Tag() Tag
type Citation ¶
type CitationMode ¶
type CitationMode Tag
const ( NormalCitation CitationMode = "NormalCitation" SuppressAuthor CitationMode = "SuppressAuthor" AuthorInText CitationMode = "AuthorInText" )
type ColWidth ¶
func DefaultColWidth ¶
func DefaultColWidth() ColWidth
type Conf ¶
type Conf struct { Pandoc string // Path to pandoc executable Dir string // Working directory Format string // Format to load or store. Ext []string // List of format extensions, each must start with '+' or '-' Opts []string // Additional options }
A configuration for running pandoc executable.
func (Conf) WithOpt ¶
Add an option to the configuration. Accepts:
- single-letter option, e.g. "s"
- single-letter option with value, e.g. "s", "foo"
- long option, e.g. "smart"
- long option with value, e.g. "smart", "foo"
func (Conf) WithPandoc ¶
Returns a Conf with a specified path to pandoc executable.
func (Conf) WithoutExt ¶
type Definition ¶
type DefinitionList ¶
type DefinitionList struct {
Items []Definition
}
Definition list (list of items, each a pair of inlines and a list of blocks)
func (*DefinitionList) Apply ¶
func (d *DefinitionList) Apply(transformers ...func(*DefinitionList) (*DefinitionList, error)) (*DefinitionList, error)
func (*DefinitionList) Tag ¶
func (d *DefinitionList) Tag() Tag
type Element ¶
type Element interface {
// contains filtered or unexported methods
}
Pandoc AST element interface
type Emph ¶
type Emph struct {
Inlines []Inline
}
Emphasized text (list of inlines)
type HorizontalRule ¶
type HorizontalRule struct{}
Horizontal rule
func (*HorizontalRule) Tag ¶
func (*HorizontalRule) Tag() Tag
type LineBlock ¶
type LineBlock struct {
Inlines [][]Inline
}
Multiple non-breaking lines
type ListAttrs ¶
type ListAttrs struct { Start int Style ListNumberStyle Delimiter ListNumberDelim }
type ListNumberDelim ¶
type ListNumberDelim Tag
const ( DefaultDelim ListNumberDelim = "DefaultDelim" Period ListNumberDelim = "Period" OneParen ListNumberDelim = "OneParen" TwoParens ListNumberDelim = "TwoParens" )
type ListNumberStyle ¶
type ListNumberStyle Tag
const ( DefaultStyle ListNumberStyle = "DefaultStyle" Example ListNumberStyle = "Example" Decimal ListNumberStyle = "Decimal" LowerRoman ListNumberStyle = "LowerRoman" UpperRoman ListNumberStyle = "UpperRoman" LowerAlpha ListNumberStyle = "LowerAlpha" UpperAlpha ListNumberStyle = "UpperAlpha" )
type Meta ¶
type Meta []MetaMapEntry
Pandoc's Meta
func (*Meta) SetInlines ¶
Sets a list of inlines for the given key.
type MetaBlocks ¶
type MetaBlocks struct {
Blocks []Block
}
Pandoc document metadata blocks block
func (*MetaBlocks) Tag ¶
func (m *MetaBlocks) Tag() Tag
type MetaInlines ¶
type MetaInlines struct {
Inlines []Inline
}
Pandoc document metadata inlines block
func (*MetaInlines) Tag ¶
func (m *MetaInlines) Tag() Tag
func (*MetaInlines) Text ¶
func (m *MetaInlines) Text() string
type MetaMap ¶
type MetaMap struct {
Entries Meta
}
Pandoc document metadata map
type MetaString ¶
type MetaString string
Pandoc document metadata string
func (MetaString) String ¶
func (s MetaString) String() string
func (MetaString) Tag ¶
func (MetaString) Tag() Tag
type OrderedList ¶
Ordered list (attributes and a list of items, each a list of blocks)
func (*OrderedList) Apply ¶
func (l *OrderedList) Apply(transformers ...func(*OrderedList) (*OrderedList, error)) (*OrderedList, error)
func (*OrderedList) Tag ¶
func (l *OrderedList) Tag() Tag
type Pandoc ¶
Pandoc document
type Plain ¶
type Plain struct {
Inlines []Inline
}
Plain text, not a paragraph
type SmallCaps ¶
type SmallCaps struct {
Inlines []Inline
}
Small capitals (list of inlines)
type Strikeout ¶
type Strikeout struct {
Inlines []Inline
}
Strikeout text (list of inlines)
type Strong ¶
type Strong struct {
Inlines []Inline
}
Strongly emphasized text (list of inlines)
type Subscript ¶
type Subscript struct {
Inlines []Inline
}
Subscripted text (list of inlines)
type Superscript ¶
type Superscript struct {
Inlines []Inline
}
Superscripted text (list of inlines)
func (*Superscript) Apply ¶
func (s *Superscript) Apply(transformers ...func(*Superscript) (*Superscript, error)) (*Superscript, error)
func (*Superscript) Tag ¶
func (s *Superscript) Tag() Tag
type Table ¶
type Table struct { Attr Caption Caption Aligns []ColSpec Head TableHeadFoot Bodies []*TableBody Foot TableHeadFoot }
Table, with attributes, caption, optional short caption, column alignments and widths (required), table head, table bodies, and table foot
type TableHeadFoot ¶
func (*TableHeadFoot) Apply ¶
func (t *TableHeadFoot) Apply(transformers ...func(*TableHeadFoot) (*TableHeadFoot, error)) (*TableHeadFoot, error)
type Underline ¶
type Underline struct {
Inlines []Inline
}
Underlined text (list of inlines)
type WhiteSpace ¶
type WhiteSpace interface { Inline // contains filtered or unexported methods }
Pandoc AST inline's whitespaces (Space, SoftBreak, LineBreak)