module documentation

Syntax highlighting for blocks of Python code.

Function colorize_codeblock Colorize a string containing only Python code. This method differs from colorize_doctest in that it will not search for doctest prompts when deciding how to colorize the string.
Function colorize_codeblock_body Undocumented
Function colorize_doctest Perform syntax highlighting on the given doctest string, and return the resulting HTML code.
Function colorize_doctest_body Undocumented
Function subfunc Undocumented
Constant DEFINE_FUNC_RE Undocumented
Constant DOCTEST_DIRECTIVE_RE Undocumented
Constant DOCTEST_EXAMPLE_RE Undocumented
Constant DOCTEST_RE Undocumented
Constant EXCEPT_RE Undocumented
Constant PROMPT2_RE Undocumented
Constant PROMPT_RE Undocumented
Constant _BUILTIN_GRP Undocumented
Constant _BUILTINS Undocumented
Constant _COMMENT_GRP Undocumented
Constant _DEFINE_GRP Undocumented
Constant _KEYWORD_GRP Undocumented
Constant _KEYWORDS Undocumented
Constant _PROMPT1_GRP Undocumented
Constant _PROMPT2_GRP Undocumented
Constant _STRING_GRP Undocumented
def colorize_codeblock(s: str) -> Tag: (source)

Colorize a string containing only Python code. This method differs from colorize_doctest in that it will not search for doctest prompts when deciding how to colorize the string.

This code consists of a <pre> block with class=py-doctest. Syntax highlighting is performed using the following CSS classes:

  • py-keyword -- a Python keyword (for, if, etc.)
  • py-builtin -- a Python builtin name (abs, dir, etc.)
  • py-string -- a string literal
  • py-comment -- a comment
  • py-except -- an exception traceback (up to the next >>>)
  • py-output -- the output from a doctest block.
  • py-defname -- the name of a function or class defined by a def or class statement.
def colorize_codeblock_body(s: str) -> Iterator[Tag | str]: (source)

Undocumented

def colorize_doctest(s: str) -> Tag: (source)

Perform syntax highlighting on the given doctest string, and return the resulting HTML code.

This code consists of a <pre> block with class=py-doctest. Syntax highlighting is performed using the following CSS classes:

  • py-prompt -- the Python PS1 prompt (>>>)
  • py-more -- the Python PS2 prompt (...)
  • the CSS classes output by colorize_codeblock
def colorize_doctest_body(s: str) -> Iterator[str | Tag]: (source)

Undocumented

def subfunc(match: Match[str]) -> Iterator[Tag | str]: (source)

Undocumented

DEFINE_FUNC_RE = (source)

Undocumented

Value
re.compile(r'(?P<def>\w+)(?P<space>\s+)(?P<name>\w+)')
DOCTEST_DIRECTIVE_RE = (source)

Undocumented

Value
re.compile(r'#[ \t]*doctest:.*')
DOCTEST_EXAMPLE_RE = (source)

Undocumented

Value
re.compile('''
    # Source consists of a PS1 line followed by zero or more PS2 lines.
    (?P<source>
        (?:^(?P<indent> [ ]*) >>>    .*)    # PS1 line
        (?:\\n           [ ]*  \\.\\.\\. .*)*   # PS2 lines
        \\n?)
    # Want consists of any non-blank lines that do not start with PS1.
...
DOCTEST_RE = (source)

Undocumented

Value
re.compile(f'((?P<STRING>{_STRING_GRP})|(?P<COMMENT>{_COMMENT_GRP})|(?P<DEFINE>{
_DEFINE_GRP})|(?P<KEYWORD>{_KEYWORD_GRP})|(?P<BUILTIN>{_BUILTIN_GRP})|(?P<PROMPT
1>{_PROMPT1_GRP})|(?P<PROMPT2>{_PROMPT2_GRP})|(?P<EOS>\\Z))',
           (re.MULTILINE | re.DOTALL))
EXCEPT_RE = (source)

Undocumented

Value
re.compile(r'^[ \t]*Traceback \(most recent call last\):.*',
           (re.DOTALL | re.MULTILINE))
PROMPT2_RE = (source)

Undocumented

Value
re.compile(f'({_PROMPT2_GRP})', (re.MULTILINE | re.DOTALL))
PROMPT_RE = (source)

Undocumented

Value
re.compile(f'({_PROMPT1_GRP}|{_PROMPT2_GRP})', (re.MULTILINE | re.DOTALL))
_BUILTIN_GRP = (source)

Undocumented

Value
'(?<!\\.)(?:%s)' % '|'.join((f'\\b{_BI}\\b' for _BI in _BUILTINS))
_BUILTINS = (source)

Undocumented

Value
[_BI for _BI in dir(builtins) if not _BI.startswith('__')]
_COMMENT_GRP: str = (source)

Undocumented

Value
'(#.*?$)'
_DEFINE_GRP: str = (source)

Undocumented

Value
'\\b(?:def|class)[ \\t]+\\w+'
_KEYWORD_GRP = (source)

Undocumented

Value
'|'.join((f'\\b{_KW}\\b' for _KW in _KEYWORDS))
_KEYWORDS: list[str] = (source)

Undocumented

Value
['and',
 'as',
 'assert',
 'async',
 'await',
 'break',
 'class',
...
_PROMPT1_GRP: str = (source)

Undocumented

Value
'^[ \\t]*>>>(?:[ \\t]|$)'
_PROMPT2_GRP: str = (source)

Undocumented

Value
'^[ \\t]*\\.\\.\\.(?:[ \\t]|$)'
_STRING_GRP = (source)

Undocumented

Value
'|'.join(['("""("""|.*?((?!").)"""))',
          '("("|.*?((?!").)"))',
          '(\'\'\'(\'\'\'|.*?[^\\\\\']\'\'\'))',
          '(\'(\'|.*?[^\\\\\']\'))'])