lexer¶
Build our lexer in addition to utilizing already built ones.
Pygments, IPython, prompt_toolkit, Jinja2 and Sphinx all come with their own concepts of lexers which doesn’t include the built-in modules.:
So it’d be tough to say we’re at a lack of tools!
Configurable Lexers¶
The lexers defined here were originally created to stand in as objects able to merge the seemingly disjoint APIs of IPython, traitlets, prompt_toolkit and pygments.
Todo
The IPython.terminal.lexer.IPythonPTLexer also should have a few
of these attributes as well.
And here’s how you join the bridge.
-
PygmentsLexer(Lexer): Lexer that calls a pygments lexer.
Example:
from pygments.lexers.html import HtmlLexer lexer = PygmentsLexer(HtmlLexer)
Note: Don’t forget to also load a Pygments compatible style. E.g.:
from prompt_toolkit.styles.from_pygments import style_from_pygments_cls from pygments.styles import get_style_by_name style = style_from_pygments_cls(get_style_by_name(‘monokai’))
- Parameters
pygments_lexer_cls – A
Lexerfrom Pygments.sync_from_start – Start lexing at the start of the document. This will always give the best results, but it will be slow for bigger documents. (When the last part of the document is display, then the whole document will be lexed by Pygments on every key stroke.) It is recommended to disable this for inputs that are expected to be more than 1,000 lines.
syntax_sync –
SyntaxSyncobject.
Lexer Autogenerated Docs¶
-
class
IPythonConfigurableLexer(**kwargs)[source][source]¶ Bases:
traitlets.config.configurable.LoggingConfigurable
-
class
Colorizer(pylexer=None, formatter=None)[source][source]¶ Bases:
objectMake the pygments function ‘highlight’ even easier to work with.
Additionally utilize
__slots__to conserve memory.-
formatter[source]¶ Format tokens with ANSI color sequences, for output in a true-color terminal or console. Like in
TerminalFormattercolor sequences are terminated at newlines, so that paging the output works correctly.New in version 2.1.
Options accepted:
styleThe style to use, can be a string or a Style subclass (default:
'default').
-