mathml

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT, UNKNOWN not legal advice Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const MaxParseDepth = 64

MaxParseDepth is the maximum recursion depth of the TeX parser. Deeper expressions fail to convert.

Variables

View Source
var (
	ErrTokenBufferEnd    = errors.New("end of TokenBuffer")
	ErrTokenBufferExpr   = errors.New("expected token, got expression")
	ErrTokenBufferSingle = errors.New("expected expression, got token")
)

Functions

func GetNextExpr

func GetNextExpr(tokens []Token, idx int) ([]Token, int, ExprKind)

Get the next single token or expression enclosed in brackets. Return the index immediately after the end of the returned expression. Example:

\frac{a^2+b^2}{c+d}
     │╰──┬──╯╰─ final position returned
     │   ╰───── slice of tokens returned
     ╰───────── idx (initial position)

func StringifyTokens

func StringifyTokens(toks []Token) string

Stringify the tokens

Types

type CommandSpec

type CommandSpec struct {
	F func(*MathMLConverter, string, bool, parseContext, []*TokenBuffer, *TokenBuffer) *MMLNode
	// contains filtered or unexported fields
}

type ExprKind

type ExprKind int

type MMLNode

type MMLNode struct {
	Tok        Token             // the token from which this node was created
	Text       string            // the <tag>text</tag> enclosed in the Tag.
	Tag        string            // the value of the MathML tag, e.g. <mrow>, <msqrt>, <mo>....
	Option     string            // container for any options that may be passed and processed for a tex command
	Properties NodeProperties    // bitfield of NodeProperties
	Attrib     map[string]string // key value pairs of XML attributes; nil until one is set
	Children   []*MMLNode        // ordered list of child MathML elements
}

An MMLNode is the representation of a MathML tag or tree.

func NewMMLNode

func NewMMLNode(opt ...string) *MMLNode

NewMMLNode allocates a new MathML node. The first optional argument sets the value of Tag. The second optional argument sets the value of Text.

func (*MMLNode) AddClass

func (n *MMLNode) AddClass(class string) *MMLNode

AddClass adds class to the classes of n and returns n.

func (*MMLNode) AppendChild

func (n *MMLNode) AppendChild(child ...*MMLNode) *MMLNode

AppendChild appends the child (or children) provided to the children of n.

func (*MMLNode) AppendNew

func (n *MMLNode) AppendNew(opt ...string) *MMLNode

AppendNew creates a new MMLNode and appends it to the children of n. The newly created MMLNode is returned.

func (*MMLNode) SetAttr

func (n *MMLNode) SetAttr(name, value string) *MMLNode

SetAttr sets the attribute name to "value" and returns the same MMLNode.

func (*MMLNode) SetFalse

func (n *MMLNode) SetFalse(name string) *MMLNode

set the attribute name to "false"

func (*MMLNode) SetProps

func (n *MMLNode) SetProps(p NodeProperties) *MMLNode

func (*MMLNode) SetTrue

func (n *MMLNode) SetTrue(name string) *MMLNode

set the attribute name to "true"

func (*MMLNode) Write

func (n *MMLNode) Write(w *strings.Builder)

Write the MMLNode to the strings.Builder w, on one line.

type MathMLConverter

type MathMLConverter struct {
	// contains filtered or unexported fields
}

MathMLConverter manages LaTeX to MathML conversion state

func NewMathMLConverter

func NewMathMLConverter() *MathMLConverter

NewMathMLConverter returns a converter. It keeps per-expression state, so it must not be shared across concurrent conversions.

func (*MathMLConverter) ConvertDisplay

func (converter *MathMLConverter) ConvertDisplay(tex string) (string, error)

ConvertDisplay converts LaTeX to display MathML

func (*MathMLConverter) ConvertInline

func (converter *MathMLConverter) ConvertInline(tex string) (string, error)

ConvertInline converts LaTeX to inline MathML

func (*MathMLConverter) DisplayStyle

func (converter *MathMLConverter) DisplayStyle(tex string) (string, error)

ConvertToDisplay converts LaTeX to display MathML

func (*MathMLConverter) OriginalString

func (converter *MathMLConverter) OriginalString(b *TokenBuffer) string

func (*MathMLConverter) ParseTex

func (converter *MathMLConverter) ParseTex(b *TokenBuffer, context parseContext, parent ...*MMLNode) *MMLNode

Parse a list of TeX tokens into a MathML node tree

func (*MathMLConverter) ProcessCommand

func (converter *MathMLConverter) ProcessCommand(context parseContext, tok Token, b *TokenBuffer) *MMLNode

ProcessCommand sets the value of n and returns the next index of tokens to be processed.

func (*MathMLConverter) TextStyle

func (converter *MathMLConverter) TextStyle(tex string) (string, error)

ConvertToInline converts LaTeX to inline MathML

type MismatchedBraceError

type MismatchedBraceError struct {
	// contains filtered or unexported fields
}

MismatchedBraceError is an error that occurs when a brace is mismatched

func (MismatchedBraceError) Error

func (e MismatchedBraceError) Error() string

Error the MismatchedBraceError

type NodeClass

type NodeClass uint64

type NodeProperties

type NodeProperties uint64

type Token

type Token struct {
	Kind        TokenKind
	MatchOffset int // offset from current index to matching paren, brace, etc.
	Value       string
	// contains filtered or unexported fields
}

type TokenBuffer

type TokenBuffer struct {
	Expr []Token // The current sub-expression
	// contains filtered or unexported fields
}

func NewTokenBuffer

func NewTokenBuffer(t []Token) *TokenBuffer

Create a new TokenBuffer

func (*TokenBuffer) Advance

func (b *TokenBuffer) Advance()

Advance the TokenBuffer by one token

func (*TokenBuffer) Empty

func (b *TokenBuffer) Empty() bool

Check if the TokenBuffer is empty

func (*TokenBuffer) GetNextExpr

func (b *TokenBuffer) GetNextExpr() (*TokenBuffer, error)

Get the next expression enclosed in {curly braces}

func (*TokenBuffer) GetNextN

func (b *TokenBuffer) GetNextN(n int, skipWhitespace ...bool) (*TokenBuffer, error)

Get the next n tokens. Past the end of the buffer, it returns the rest of the buffer with ErrTokenBufferEnd, and consumes it all the same: a caller that ungets after the error rewinds to where it was, not before it. With skipWhitespace, the whitespace and comments before the tokens are skipped; without it, n counts every token, as a MatchOffset does.

Sub-buffers are cut with their capacity set to their length, here and in GetNextExpr, GetOptions and GetUntil, so that no offset read inside one can reach the tokens of the buffer around it.

func (*TokenBuffer) GetNextToken

func (b *TokenBuffer) GetNextToken(skipWhitespace ...bool) (Token, error)

func (*TokenBuffer) GetOptions

func (b *TokenBuffer) GetOptions(skipWhitespace ...bool) (*TokenBuffer, error)

Extract the tokens strictly between [square brackets]. skipWhitespace is true by default.

func (*TokenBuffer) GetUntil

func (b *TokenBuffer) GetUntil(f func(Token) bool) *TokenBuffer

Get tokens until (but not including) the condition f evaluates as true, or the end of the token buffer is reached

func (*TokenBuffer) Unget

func (b *TokenBuffer) Unget()

Unget the last n tokens

type TokenBufferError

type TokenBufferError struct {
	// contains filtered or unexported fields
}

func (*TokenBufferError) Error

func (e *TokenBufferError) Error() string

Error the TokenBufferError

func (*TokenBufferError) Unwrap

func (e *TokenBufferError) Unwrap() error

Unwrap the TokenBufferError

type TokenKind

type TokenKind int

Jump to

Keyboard shortcuts

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