ast

package
v0.0.0-...-f7e15b2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2024 License: BSD-2-Clause Imports: 5 Imported by: 230

Documentation

Overview

Package ast defines tree representation of a parsed markdown document.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendChild

func AppendChild(parent Node, child Node)

AppendChild appends child to children of parent It panics if either node is nil.

func Print

func Print(dst io.Writer, doc Node)

Print is for debugging. It prints a string representation of parsed markdown doc (result of parser.Parse()) to dst.

To make output readable, it shortens text output.

func PrintWithPrefix

func PrintWithPrefix(w io.Writer, doc Node, prefix string)

PrintWithPrefix is like Print but allows customizing prefix used for indentation. By default it's 2 spaces. You can change it to e.g. tab by passing "\t"

func RemoveFromTree

func RemoveFromTree(n Node)

RemoveFromTree removes this node from tree

func ToString

func ToString(doc Node) string

ToString is like Dump but returns result as a string

func WalkFunc

func WalkFunc(n Node, f NodeVisitorFunc)

WalkFunc is like Walk but accepts just a callback function

Types

type Aside

type Aside struct {
	Container
}

Aside represents an markdown aside node.

type Attribute

type Attribute struct {
	ID      []byte
	Classes [][]byte
	Attrs   map[string][]byte
}

An attribute can be attached to block elements. They are specified as {#id .classs key="value"} where quotes for values are mandatory, multiple key/value pairs are separated by whitespace.

type BlockQuote

type BlockQuote struct {
	Container
}

BlockQuote represents markdown block quote node

type Callout

type Callout struct {
	Leaf

	ID []byte // number of this callout
}

Callout is a node that can exist both in text (where it is an actual node) and in a code block.

type CanContain

type CanContain interface {
	CanContain(Node) bool
}

return true if can contain children of a given node type used by custom nodes to over-ride logic in canNodeContain

type Caption

type Caption struct {
	Container
}

Caption represents a figure, code or quote caption

type CaptionFigure

type CaptionFigure struct {
	Container

	HeadingID string // This might hold heading ID, if present
}

CaptionFigure is a node (blockquote or codeblock) that has a caption

type CellAlignFlags

type CellAlignFlags int

CellAlignFlags holds a type of alignment in a table cell.

const (
	TableAlignmentLeft CellAlignFlags = 1 << iota
	TableAlignmentRight
	TableAlignmentCenter = (TableAlignmentLeft | TableAlignmentRight)
)

These are the possible flag values for the table cell renderer. Only a single one of these values will be used; they are not ORed together. These are mostly of interest if you are writing a new output format.

func (CellAlignFlags) String

func (a CellAlignFlags) String() string

type Citation

type Citation struct {
	Leaf

	Destination [][]byte        // Destination is where the citation points to. Multiple ones are allowed.
	Type        []CitationTypes // 1:1 mapping of destination and citation type
	Suffix      [][]byte        // Potential citation suffix, i.e. [@!RFC1035, p. 144]
}

Citation is a citation node.

type CitationTypes

type CitationTypes int

CitationTypes holds the type of a citation, informative, normative or suppressed

const (
	CitationTypeNone CitationTypes = iota
	CitationTypeSuppressed
	CitationTypeInformative
	CitationTypeNormative
)

type Code

type Code struct {
	Leaf
}

Code represents markdown code node

type CodeBlock

type CodeBlock struct {
	Leaf

	IsFenced    bool   // Specifies whether it's a fenced code block or an indented one
	Info        []byte // This holds the info string
	FenceChar   byte
	FenceLength int
	FenceOffset int
}

CodeBlock represents markdown code block node

type Container

type Container struct {
	Parent   Node
	Children []Node

	Literal []byte // Text contents of the leaf nodes
	Content []byte // Markdown content of the block nodes

	*Attribute // Block level attribute
}

Container is a type of node that can contain children

func (*Container) AsContainer

func (c *Container) AsContainer() *Container

AsContainer returns itself as *Container

func (*Container) AsLeaf

func (c *Container) AsLeaf() *Leaf

AsLeaf returns nil

func (*Container) GetChildren

func (c *Container) GetChildren() []Node

GetChildren returns children nodes

func (*Container) GetParent

func (c *Container) GetParent() Node

GetParent returns parent node

func (*Container) SetChildren

func (c *Container) SetChildren(newChildren []Node)

SetChildren sets children node

func (*Container) SetParent

func (c *Container) SetParent(newParent Node)

SetParent sets the parent node

type CrossReference

type CrossReference struct {
	Container

	Destination []byte // Destination is where the reference points to
	Suffix      []byte // Potential citation suffix, i.e. (#myid, text)
}

CrossReference is a reference node.

type Del

type Del struct {
	Container
}

Del represents markdown del node

type Document

type Document struct {
	Container
}

Document represents markdown document node, a root of ast

type DocumentMatter

type DocumentMatter struct {
	Container

	Matter DocumentMatters
}

DocumentMatter represents markdown node that signals a document division: frontmatter, mainmatter or backmatter.

type DocumentMatters

type DocumentMatters int

DocumentMatters holds the type of a {front,main,back}matter in the document

const (
	DocumentMatterNone DocumentMatters = iota
	DocumentMatterFront
	DocumentMatterMain
	DocumentMatterBack
)

These are all possible Document divisions.

type Emph

type Emph struct {
	Container
}

Emph represents markdown emphasis node

type Footnotes

type Footnotes struct {
	Container
}

Footnotes is a node that contains all footnotes

type HTMLBlock

type HTMLBlock struct {
	Leaf
}

HTMLBlock represents markdown html node

type HTMLSpan

type HTMLSpan struct {
	Leaf
}

HTMLSpan represents markdown html span node

type Hardbreak

type Hardbreak struct {
	Leaf
}

Hardbreak represents markdown hard break node

type Heading

type Heading struct {
	Container

	Level        int    // This holds the heading level number
	HeadingID    string // This might hold heading ID, if present
	IsTitleblock bool   // Specifies whether it's a title block
	IsSpecial    bool   // We are a special heading (starts with .#)
}

Heading represents markdown heading node

type HorizontalRule

type HorizontalRule struct {
	Leaf
}

HorizontalRule represents markdown horizontal rule node

type Image

type Image struct {
	Container

	Destination []byte // Destination is what goes into a href
	Title       []byte // Title is the tooltip thing that goes in a title attribute
}

Image represents markdown image node

type Index

type Index struct {
	Leaf

	Primary bool
	Item    []byte
	Subitem []byte
	ID      string // ID of the index
}

Index is a node that contains an Index item and an optional, subitem.

type Leaf

type Leaf struct {
	Parent Node

	Literal []byte // Text contents of the leaf nodes
	Content []byte // Markdown content of the block nodes

	*Attribute // Block level attribute
}

Leaf is a type of node that cannot have children

func (*Leaf) AsContainer

func (l *Leaf) AsContainer() *Container

AsContainer returns nil

func (*Leaf) AsLeaf

func (l *Leaf) AsLeaf() *Leaf

AsLeaf returns itself as *Leaf

func (*Leaf) GetChildren

func (l *Leaf) GetChildren() []Node

GetChildren returns nil because Leaf cannot have children

func (*Leaf) GetParent

func (l *Leaf) GetParent() Node

GetParent returns parent node

func (*Leaf) SetChildren

func (l *Leaf) SetChildren(newChildren []Node)

SetChildren will panic if trying to set non-empty children because Leaf cannot have children

func (*Leaf) SetParent

func (l *Leaf) SetParent(newParent Node)

SetParent sets the parent nodd

type Link struct {
	Container

	Destination          []byte   // Destination is what goes into a href
	Title                []byte   // Title is the tooltip thing that goes in a title attribute
	NoteID               int      // NoteID contains a serial number of a footnote, zero if it's not a footnote
	Footnote             Node     // If it's a footnote, this is a direct link to the footnote Node. Otherwise nil.
	DeferredID           []byte   // If a deferred link this holds the original ID.
	AdditionalAttributes []string // Defines additional attributes to use during rendering.
}

Link represents markdown link node

type List

type List struct {
	Container

	ListFlags       ListType
	Tight           bool   // Skip <p>s around list item data if true
	BulletChar      byte   // '*', '+' or '-' in bullet lists
	Delimiter       byte   // '.' or ')' after the number in ordered lists
	Start           int    // for ordered lists this indicates the starting number if > 0
	RefLink         []byte // If not nil, turns this list item into a footnote item and triggers different rendering
	IsFootnotesList bool   // This is a list of footnotes
}

List represents markdown list node

type ListItem

type ListItem struct {
	Container

	ListFlags       ListType
	Tight           bool   // Skip <p>s around list item data if true
	BulletChar      byte   // '*', '+' or '-' in bullet lists
	Delimiter       byte   // '.' or ')' after the number in ordered lists
	RefLink         []byte // If not nil, turns this list item into a footnote item and triggers different rendering
	IsFootnotesList bool   // This is a list of footnotes
}

ListItem represents markdown list item node

type ListType

type ListType int

ListType contains bitwise or'ed flags for list and list item objects.

const (
	ListTypeOrdered ListType = 1 << iota
	ListTypeDefinition
	ListTypeTerm

	ListItemContainsBlock
	ListItemBeginningOfList // TODO: figure out if this is of any use now
	ListItemEndOfList
)

These are the possible flag values for the ListItem renderer. Multiple flag values may be ORed together. These are mostly of interest if you are writing a new output format.

type Math

type Math struct {
	Leaf
}

Math represents markdown MathAjax inline node

type MathBlock

type MathBlock struct {
	Container
}

MathBlock represents markdown MathAjax block node

type Node

type Node interface {
	AsContainer() *Container
	AsLeaf() *Leaf
	GetParent() Node
	SetParent(newParent Node)
	GetChildren() []Node
	SetChildren(newChildren []Node)
}

Node defines an ast node

func GetFirstChild

func GetFirstChild(n Node) Node

GetFirstChild returns first child of node n It's implemented as stand-alone function to keep Node interface small

func GetLastChild

func GetLastChild(n Node) Node

GetLastChild returns last child of node n It's implemented as stand-alone function to keep Node interface small

func GetNextNode

func GetNextNode(n Node) Node

GetNextNode returns next sibling of node n (node after n) We can't make it part of Container or Leaf because we loose Node identity

func GetPrevNode

func GetPrevNode(n Node) Node

GetPrevNode returns previous sibling of node n (node before n) We can't make it part of Container or Leaf because we loose Node identity

type NodeVisitor

type NodeVisitor interface {
	Visit(node Node, entering bool) WalkStatus
}

NodeVisitor is a callback to be called when traversing the syntax tree. Called twice for every node: once with entering=true when the branch is first visited, then with entering=false after all the children are done.

type NodeVisitorFunc

type NodeVisitorFunc func(node Node, entering bool) WalkStatus

NodeVisitorFunc casts a function to match NodeVisitor interface

func (NodeVisitorFunc) Visit

func (f NodeVisitorFunc) Visit(node Node, entering bool) WalkStatus

Visit calls visitor function

type NonBlockingSpace

type NonBlockingSpace struct {
	Leaf
}

NonBlockingSpace represents markdown non-blocking space node

type Paragraph

type Paragraph struct {
	Container
}

Paragraph represents markdown paragraph node

type Softbreak

type Softbreak struct {
	Leaf
}

Softbreak represents markdown softbreak node Note: not used currently

type Strong

type Strong struct {
	Container
}

Strong represents markdown strong node

type Subscript

type Subscript struct {
	Leaf
}

Subscript is a subscript node

type Superscript

type Superscript struct {
	Leaf
}

Subscript is a superscript node

type Table

type Table struct {
	Container
}

Table represents markdown table node

type TableBody

type TableBody struct {
	Container
}

TableBody represents markdown table body node

type TableCell

type TableCell struct {
	Container

	IsHeader bool           // This tells if it's under the header row
	Align    CellAlignFlags // This holds the value for align attribute
	ColSpan  int            // How many columns to span
}

TableCell represents markdown table cell node

type TableFooter

type TableFooter struct {
	Container
}

TableFooter represents markdown table foot node

type TableHeader

type TableHeader struct {
	Container
}

TableHeader represents markdown table head node

type TableRow

type TableRow struct {
	Container
}

TableRow represents markdown table row node

type Text

type Text struct {
	Leaf
}

Text represents markdown text node

type WalkStatus

type WalkStatus int

WalkStatus allows NodeVisitor to have some control over the tree traversal. It is returned from NodeVisitor and different values allow Node.Walk to decide which node to go to next.

const (
	// GoToNext is the default traversal of every node.
	GoToNext WalkStatus = iota
	// SkipChildren tells walker to skip all children of current node.
	SkipChildren
	// Terminate tells walker to terminate the traversal.
	Terminate
)

func Walk

func Walk(n Node, visitor NodeVisitor) WalkStatus

Walk traverses tree recursively

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL