Syntax highlighting for prompt_toolkit and HTML with pyparsing.
Project description
Syntax highlighting with pyparsing, supporting both HTML output and prompt_toolkit–style terminal output. The PPHighlighter class can also be used as a lexer for syntax highlighting as you type in prompt_toolkit. It is compatible with existing Pygments styles.
Read the documentation on readthedocs.
Requirements
Python 3.5+
prompt_toolkit 2.0+
Installation
pip3 install -U pyparsing-highlighting
Or, after cloning the repository on GitHub:
python3 setup.py install
Examples
The following code demonstrates the use of PPHighlighter:
from pp_highlighting import PPHighlighter
import pyparsing as pp
from pyparsing import pyparsing_common as ppc
def parser_factory(styler):
a = styler('class:int', ppc.integer)
return pp.delimitedList(a)
pph = PPHighlighter(parser_factory)
pph.highlight('1, 2, 3')
pph.highlight('1, 2, 3') returns the following:
FormattedText([('class:int', '1'), ('', ', '), ('class:int', '2'), ('', ', '), ('class:int', '3')])
A FormattedText instance can be passed to prompt_toolkit.print_formatted_text(), along with a Style mapping the class names to colors, for display on the terminal. For convenience, PPHighlighter defines a print() method that highlights and then prints a string. PPHighlighter also has a highlight_html() method which returns the generated HTML as a string.
PPHighlighter can also be passed to a prompt_toolkit.PromptSession as the lexer argument, which will perform syntax highlighting as you type. For an example of this, see pp_highlighting/examples/calc.py and pp_highlighting/examples/repl.py.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pyparsing-highlighting-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b684c6fdc40d5b92ae46c9b39ed73f53edd02859e100a24bb078be9c964a194f |
|
MD5 | cc8266b2e7f6eceee592c16272f06118 |
|
BLAKE2b-256 | c503080d0fa8f61e7940bf79d651e64135fc88b92a8ebbadc379e5d46d161c00 |
Hashes for pyparsing_highlighting-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 676a5342d32e3e744c911352e001b93a0320ef548c245a4f0cba81e37d777ad6 |
|
MD5 | e318ee1fbd1969bfd379c408861fd812 |
|
BLAKE2b-256 | a79e4a65370673cce075b09e34f0d3f7cc39b77ecd1967e23fde0ea65589f9cf |