Filters

The package contains the following filters:

Filter Name

Description

Filter Class

tokenreplace

A configurable token replacer: replace a given token type by another given token type in a stream

TokenReplaceFilter

errortogenericerror

Replace all Error tokens in a stream by Generic.Error tokens

ErrorToGenericErrorTokenFilter

The AlgPseudocode lexer by default yields an error token for the code block below because it encounters an unknown command. When used within Sphinx it warns about this and—as a consequence—suppresses highlighting for this code block completely (see also this note):

\nonexisting{TEST}

This may be changed by using a custom AlgPseudocode lexer that has the prohibit_raiseonerror_filter lexer option enabled. Then the the output in Sphinx is as follows:

\nonexisting{TEST}

Alternatively—with the “errortogenericerror” filter applied the very same block is highlighted as:

\nonexisting{TEST}

The above custom lexer is to be defined in Sphinx using:

import functools
from pygments_lexer_pseudocode2.filters import ErrorToGenericErrorTokenFilter
from pygments_lexer_pseudocode2.lexers.algpseudocode import AlgPseudocodeLexer

def setup(app):
    app.add_lexer(
        "genericerror-algpseudocode",
        functools.partial(
            AlgPseudocodeLexer,
            filters=[ErrorToGenericErrorTokenFilter]))

TokenReplaceFilter

Name:

tokenreplace

Filter Options:
replacements

Type: dict[str | pygments.token._TokenType, str | pygments.token._TokenType]

A map from tokens to their replacements.

token_from

Type: str or pygments.token._TokenType

The name of a token type (like Error) or a token object (like pygments.token.Token.Error).

If given the token_to options is required and replacements will be augmented with their respective values.

token_to

Type: str or pygments.token._TokenType

The name of a token type (like Generic.Error) or a token object (like pygments.token.Token.Generic.Error).

This option is required if token_from is given.

Replace all token types given as replacements keys or in token_from with the token types given in replacements values or in token_to.

The values in the token stream are retained.

All str types in any of the filter options are converted to real tokens using Pygments function pygments.token.string_to_tokentype().

ErrorToGenericErrorTokenFilter

Name:

errortogenericerror

Filter Options:

none

Replace all pygments.token.Token.Error tokens in a stream by pygments.token.Token.Generic.Error tokens.

The filter is implemented as an application of the TokenReplaceFilter.