Documentation

Lean.Data.Markdown.Parser.Emphasis

Emphasis Matching (CommonMark §6.2) #

Implements CommonMark's delimiter stack algorithm.

The entry point is processEmphasis, which consumes a flat sequence of InlineToks. Each is either a fully-finalized non-emphasis Inline or a raw */_ delimiter sequence. It returns the final Array Inline with all matched emphasis spans wrapped as .bold or .italic. Tokens of the input are produced by the tokenizer in Lean.Data.Markdown.Parser.Inline; this module is the second half of the inline element parsing pass.

With the latexMath extension, the tokens also include $ delimiter sequences.

A sequence of * or _ delimiter characters (a delimiter run, in the specification's terms), classified by the left/right-flanking rules.

With the latexMath extension, delimiters may also be a sequence of one or two $.

  • startPos : s.Pos

    Position of the first delimiter character.

  • ch : Char

    The delimiter character (*, _, or $).

  • origLen : Nat

    Number of consecutive delimiter characters.

  • canOpen : Bool

    Whether this sequence can open emphasis.

  • canClose : Bool

    Whether this sequence can close emphasis.

Instances For

    An intermediate token, which is either a finalized non-emphasis inline or a raw emphasis delimiter sequence that awaits matching.

    Instances For
      def Lean.Markdown.pushTokText {s : String.Slice} (out : Array (InlineTok s)) (textStart pos : s.Pos) :

      Appends a text inline as an InlineTok for [textStart:pos) to out, if non-empty.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For

        Delimiter Matching #

        CommonMark §6.2 specifies emphasis matching as an algorithm over a delimiter stack. Each */_ sequence that could open emphasis is pushed on the stack while non-delimiter inlines are added to the inline output, and for each closing sequence the stack is searched downward for a compatible opener. The spec describes the algorithm imperatively over two pieces of state, the delimiter stack and the inline output, and those are maintained in a single DelimiterMatchState structure.

        As in the specification, an opener's delimiter characters are a text inline in the output, and its frame records that inline's index. The content of a matched emphasis is the output after the index, abandoning an opener leaves its text in place, and openersBottom bounds each search for an opener below the point where the last search for the same kind of closer failed.

        Processes the delimiter sequences in toks, producing a final flat inline array.

        Implements CommonMark §6.2's delimiter-matching algorithm for emphasis (including the rule about lengths that are a multiple of 3), as well as the LaTeX math extension if enabled. Unmatched delimiters fall back to literal text.

        Equations
        • One or more equations did not get rendered due to their size.
        Instances For