katex-parser

    A MoonBit parser for KaTeX, lexing and parsing LaTeX math expressions with macro expansion

    katex
    latex
    math
    parser
    tex
    Download zip
    Author
    Version
    0.2.7
    License
    AGPL-3.0
    Last updated
    2 months ago
    Downloads
    185

    No dependencies

    #KaTeX Parser

    A port of the KaTeX parser, lexing LaTeX math expressions into a typed AST with macro expansion support.

    StrictHandler

    type StrictHandler = (String, String, SourceLocation?) -> StrictResponse raise ParseFailure

    StrictWarningHandler

    type StrictWarningHandler = (String) -> Unit

    TrustHandler

    type TrustHandler = (TrustContext) -> Bool

    ParseFailure

    pub(all) suberror ParseFailure {
    UnexpectedCharacter(message~ : String, loc~ : SourceLocation?)
    ExpectedToken(expected~ : String, actual~ : Diagnostic)
    UndefinedControlSequence(name~ : String, loc~ : SourceLocation?)
    InvalidArgument(message~ : String, loc~ : SourceLocation?)
    DoubleSuperscript(loc~ : SourceLocation?)
    DoubleSubscript(loc~ : SourceLocation?)
    ExpectedGroupAfter(symbol~ : String, loc~ : SourceLocation?)
    FunctionNotAllowed(func_name~ : String, context~ : String, loc~ : SourceLocation?)
    MissingFunctionHandler(func_name~ : String, loc~ : SourceLocation?)
    TooManyExpansions(limit~ : Int)
    InternalInvariant(message~ : String)
    } derive(Eq, ToJson,
    Debug
    )

    ArrayColumn

    pub(all) enum ArrayColumn {
    AlignColumn(alignment~ : String, pre_gap~ : Double, post_gap~ : Double)
    SeparatorColumn(separator~ : String)
    } derive(Eq, ToJson,
    Debug
    )

    AtomFamily

    pub(all) enum AtomFamily {
    Mord
    Mop
    Mbin
    Mrel
    Mopen
    Mclose
    Mpunct
    Minner
    } derive(Eq, ToJson,
    Debug
    )

    ColumnSeparationType

    pub(all) enum ColumnSeparationType {
    AlignSeparation
    AlignAtSeparation
    GatherSeparation
    SmallSeparation
    CdSeparation
    } derive(Eq, ToJson,
    Debug
    )

    Diagnostic

    pub(all) struct Diagnostic {
    text : String
    loc : SourceLocation?
    } derive(Eq, ToJson,
    Debug
    )

    LapAlignment

    pub(all) enum LapAlignment {
    LLap
    RLap
    CLap
    } derive(Eq, ToJson,
    Debug
    )

    MacroDefinition

    pub(all) enum MacroDefinition {
    Text(String)
    Expansion(MacroExpansion)
    }

    MacroExpansion

    pub(all) struct MacroExpansion {
    tokens : Array[Token]
    num_args : Int
    delimiters : Array[Array[String]]?
    unexpandable : Bool
    }

    Macros

    pub struct Macros(Map[String, MacroDefinition])

    Macros::make

    fn Macros::make(macros? : Map[String, String]) -> Macros

    Measurement

    pub(all) struct Measurement {
    number : Double
    unit : String
    } derive(Eq, ToJson,
    Debug
    )

    Mode

    pub(all) enum Mode {
    Math
    Text
    } derive(Eq, ToJson,
    Debug
    )

    OperatorContent

    pub(all) enum OperatorContent {
    SymbolOperator(String)
    BodyOperator(Array[ParseNode])
    NamedOperator(String)
    } derive(Eq, ToJson,
    Debug
    )

    ParseNode

    pub(all) enum ParseNode {
    Internal(mode~ : Mode)
    Raw(mode~ : Mode, string~ : String)
    ColorToken(mode~ : Mode, color~ : String)
    Size(mode~ : Mode, value~ : Measurement, is_blank~ : Bool)
    Url(mode~ : Mode, url~ : String)
    Styling(mode~ : Mode, body~ : Array[ParseNode], style~ : StyleLevel, reset_font~ : Bool)
    Sqrt(mode~ : Mode, body~ : ParseNode, index~ : ParseNode?)
    Infix(mode~ : Mode, replace_with~ : String, size~ : Measurement?, loc~ : SourceLocation?)
    GenFrac(mode~ : Mode, numer~ : ParseNode, denom~ : ParseNode, continued~ : Bool, has_bar_line~ : Bool, bar_size~ : Measurement?, left_delim~ : String?, right_delim~ : String?)
    Text(mode~ : Mode, body~ : Array[ParseNode], font~ : String)
    Font(mode~ : Mode, font~ : String, body~ : ParseNode)
    MClass(mode~ : Mode, mclass~ : AtomFamily, body~ : Array[ParseNode], is_character_box~ : Bool)
    Op(mode~ : Mode, limits~ : Bool, always_handle_sup_sub~ : Bool, parent_is_sup_sub~ : Bool, suppress_base_shift~ : Bool, content~ : OperatorContent)
    OperatorName(mode~ : Mode, body~ : Array[ParseNode], always_handle_sup_sub~ : Bool, limits~ : Bool, parent_is_sup_sub~ : Bool)
    Overline(mode~ : Mode, body~ : ParseNode)
    Underline(mode~ : Mode, body~ : ParseNode)
    Smash(mode~ : Mode, body~ : ParseNode, smash_height~ : Bool, smash_depth~ : Bool)
    Phantom(mode~ : Mode, body~ : Array[ParseNode])
    VPhantom(mode~ : Mode, body~ : ParseNode)
    Pmb(mode~ : Mode, mclass~ : AtomFamily, body~ : Array[ParseNode])
    VCenter(mode~ : Mode, body~ : ParseNode)
    Rule(mode~ : Mode, shift~ : Measurement?, width~ : Measurement, height~ : Measurement)
    RaiseBox(mode~ : Mode, dy~ : Measurement, body~ : ParseNode)
    HBox(mode~ : Mode, body~ : Array[ParseNode])
    Lap(mode~ : Mode, alignment~ : LapAlignment, body~ : ParseNode)
    MathChoice(mode~ : Mode, display~ : Array[ParseNode], text~ : Array[ParseNode], script~ : Array[ParseNode], scriptscript~ : Array[ParseNode])
    Sizing(mode~ : Mode, size~ : Int, body~ : Array[ParseNode])
    HorizBrace(mode~ : Mode, label~ : String, is_over~ : Bool, base~ : ParseNode)
    XArrow(mode~ : Mode, label~ : String, body~ : ParseNode, below~ : ParseNode?)
    AccentUnder(mode~ : Mode, label~ : String, base~ : ParseNode)
    DelimSizing(mode~ : Mode, size~ : Int, mclass~ : AtomFamily, delim~ : String)
    LeftRightRight(mode~ : Mode, delim~ : String, color~ : String?)
    LeftRight(mode~ : Mode, body~ : Array[ParseNode], left~ : String, right~ : String, right_color~ : String?)
    Middle(mode~ : Mode, delim~ : String)
    Kern(mode~ : Mode, dimension~ : Measurement)
    Enclose(mode~ : Mode, body~ : ParseNode, label~ : String, background_color~ : String?, border_color~ : String?)
    Href(mode~ : Mode, href~ : String, body~ : Array[ParseNode])
    Html(mode~ : Mode, attributes~ : Map[String, String], body~ : Array[ParseNode])
    IncludeGraphics(mode~ : Mode, alt~ : String, width~ : Measurement, height~ : Measurement, totalheight~ : Measurement, src~ : String)
    Tag(mode~ : Mode, body~ : Array[ParseNode], tag~ : Array[ParseNode])
    Array(mode~ : Mode, body~ : Array[Array[ParseNode]], add_jot~ : Bool, array_stretch~ : Double, columns~ : Array[ArrayColumn]?, row_gaps~ : Array[Measurement?], hskip_before_and_after~ : Bool, hlines_before_row~ : Array[Array[Bool]], column_separation_type~ : ColumnSeparationType?, tags~ : Array[Array[ParseNode]?]?, auto_tags~ : Array[Bool]?, leqno~ : Bool)
    EnvironmentEnd(mode~ : Mode, name~ : String)
    CdLabel(mode~ : Mode, side~ : String, label~ : ParseNode)
    CdParent(mode~ : Mode, fragment~ : ParseNode)
    Cr(mode~ : Mode, new_line~ : Bool, size~ : Measurement?)
    HtmlMathML(mode~ : Mode, html~ : Array[ParseNode], mathml~ : Array[ParseNode])
    OrdGroup(mode~ : Mode, loc~ : SourceLocation?, body~ : Array[ParseNode], semisimple~ : Bool)
    SupSub(mode~ : Mode, base~ : ParseNode?, sup~ : ParseNode?, sub~ : ParseNode?)
    TextOrd(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    MathOrd(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    Spacing(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    AccentToken(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    OperatorToken(mode~ : Mode, loc~ : SourceLocation?, text~ : String)
    Accent(mode~ : Mode, loc~ : SourceLocation?, label~ : String, is_stretchy~ : Bool, is_shifty~ : Bool, base~ : ParseNode)
    Verb(mode~ : Mode, loc~ : SourceLocation?, body~ : String, star~ : Bool)
    Atom(mode~ : Mode, loc~ : SourceLocation?, family~ : AtomFamily, text~ : String)
    Color(mode~ : Mode, color~ : String, body~ : Array[ParseNode])
    } derive(Eq, ToJson,
    Debug
    )

    ParseNode::mode

    fn ParseNode::mode(self : ParseNode) -> Mode

    Settings

    pub struct Settings {
    throw_on_error : Bool
    display_mode : Bool
    leqno : Bool
    error_color : String
    color_is_text_color : Bool
    max_expand : Int
    global_group : Bool
    macros : Map[String, String]
    macro_store : Macros?
    strict : Strictness
    strict_warning_handler : (String) -> Unit?
    trust : TrustPolicy
    }

    impl Default for Settings

    Settings::make

    fn Settings::make(throw_on_error? : Bool, display_mode? : Bool, leqno? : Bool, error_color? : String, color_is_text_color? : Bool, max_expand? : Int, global_group? : Bool, macros? : Map[String, String], macro_store? : Macros, strict? : Strictness, strict_warning_handler? : (String) -> Unit, trust? : TrustPolicy) -> Settings

    SourceLocation

    pub(all) struct SourceLocation {
    input : String
    start : Int
    end : Int
    } derive(Eq, ToJson,
    Debug
    )

    SourceLocation::make

    fn SourceLocation::make(input : String, start~ : Int, end~ : Int) -> SourceLocation

    StrictResponse

    pub(all) enum StrictResponse {
    Ignore
    Warn
    Error
    }

    Strictness

    pub(all) enum Strictness {
    Ignore
    Warn
    Error
    Callback((String, String, SourceLocation?) -> StrictResponse raise ParseFailure)
    }

    StyleLevel

    pub(all) enum StyleLevel {
    DisplayStyle
    TextStyle
    ScriptStyle
    ScriptScriptStyle
    } derive(Eq, ToJson,
    Debug
    )

    Token

    pub(all) struct Token {
    text : String
    loc : SourceLocation?
    noexpand : Bool
    treat_as_relax : Bool
    }

    TrustContext

    pub(all) enum TrustContext {
    UrlTrust(command~ : String, url~ : String, protocol~ : String?)
    HtmlClass(class~ : String)
    HtmlId(id~ : String)
    HtmlStyle(style~ : String)
    HtmlData(attributes~ : Map[String, String])
    }

    TrustPolicy

    pub(all) enum TrustPolicy {
    Untrusted
    Trusted
    Callback((TrustContext) -> Bool)
    }

    UnicodeScript

    pub struct UnicodeScript {
    replacement : String
    kind : UnicodeScriptKind
    }

    UnicodeScriptKind

    pub(all) enum UnicodeScriptKind {
    UnicodeSubscript
    UnicodeSuperscript
    } derive(Eq)

    U00A0

    let U00A0 : String

    parse

    fn parse(input : String, settings? : Settings) -> Array[ParseNode] raise ParseFailure

    supported_codepoint

    fn supported_codepoint(code : Int) -> Bool

    True when the codepoint falls within one of the supported Unicode script blocks. Mirrors KaTeX's supportedCodepoint (unicodeScripts.ts).

    unicode_font_character

    fn unicode_font_character(font : String, char : String) -> String?

    Returns the Unicode mathematical alphanumeric character for a given font style and ASCII letter/digit, or None when no mapping exists.

    Font styles that match LaTeX commands:
    • mathbf / boldsymbol — Bold / Bold Italic
    • mathit — Italic
    • mathbb — Double-struck (blackboard bold)
    • mathcal — Calligraphic / Script
    • mathfrak — Fraktur
    • mathscr — Script (rsfs)
    • mathsf — Sans-serif
    • mathtt — Monospace
    • mathsfit — Sans-serif Italic
    • mathrm / mathnormal — Roman (identity, not handled here) / Italic

    unicode_script_character

    fn unicode_script_character(kind : UnicodeScriptKind, replacement : String) -> String?

    Returns the Unicode superscript or subscript character for replacement.

    The parser and renderers share unicode_scripts as the canonical table.

    unicode_symbol

    fn unicode_symbol(name : String) -> String?

    Returns a registered symbol's Unicode replacement, if it has one.