Python API

Python API

Miscellaneous

class myst_nb.ansi_lexer.AnsiColorLexer(*args, **kwds)[source]

Bases: pygments.lexer.RegexLexer

aliases = ('myst-ansi',)

Shortcuts for the lexer

property current_token
flags = 24

Flags for compiling the regular expressions. Defaults to MULTILINE.

name = 'ANSI Color'

Name of the lexer

process(match)[source]

Produce the next token and bit of text.

Interprets the ANSI code (which may be a color code or some other code), changing the lexer state and producing a new token. If it’s not a color code, we just strip it out and move on.

Some useful reference for ANSI codes:
reset_state()[source]
tokens = {'root': [('\\x1b\\[([^\\x1b]*)', <function AnsiColorLexer.process>), ('[^\\x1b]+', Token.Text)]}

At all time there is a stack of states. Initially, the stack contains a single state ‘root’. The top of the stack is called “the current state”.

Dict of {'state': [(regex, tokentype, new_state), ...], ...}

new_state can be omitted to signify no state transition. If new_state is a string, it is pushed on the stack. This ensure the new current state is new_state. If new_state is a tuple of strings, all of those strings are pushed on the stack and the current state will be the last element of the list. new_state can also be combined('state1', 'state2', ...) to signify a new, anonymous state combined from the rules of two or more existing ones. Furthermore, it can be ‘#pop’ to signify going back one step in the state stack, or ‘#push’ to push the current state on the stack again. Note that if you push while in a combined state, the combined state itself is pushed, and not only the state in which the rule is defined.

The tuple can also be replaced with include('state'), in which case the rules from the state named by the string are included in the current one.