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+
Pygments (optional)
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
from prompt_toolkit.styles import Style
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)
style = Style([('int', '#528f50')])
pph.print('1, 2, 3', style=style)
This prints out the following to the terminal:
The following code generates HTML:
pph.highlight_html('1, 2, 3')
The output is:
<pre class="highlight"><span class="int">1</span>, <span class="int">2</span>, <span class="int">3</span></pre>
There is also a lower-level API—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.
PPHighlighter can also be passed to a prompt_toolkit.PromptSession as the lexer argument, which will perform syntax highlighting as you type. For examples of this, see examples/calc.py, examples/json_pph.py, examples/repr.py, and examples/sexp.py. The examples can be run by (from the project root directory):
python3 -m examples.calc
python3 -m examples.json_pph
python3 -m examples.repr
python3 -m examples.sexp
Testing
(From the project root directory):
To run the unit tests:
python3 -m unittest
To run the regression benchmark:
python3 -m tests.benchmark
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.2.6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37c6a4484137c2904c0cd0bcf0ee7d7fdfcc658e4d50075eff1e538ddc024acd |
|
MD5 | b44946bc57a49afff31b2f13ce97d103 |
|
BLAKE2b-256 | 08ccb5b1ab1fb1569099c0f01f400a787a17bca3f2f5df81035c664602eebdcc |
Hashes for pyparsing_highlighting-0.2.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 219ee72eea51f3f1f41719e0b6212753c2cd51c38ef1e4a245ea300dbfa8a3cf |
|
MD5 | 17370b1428b6123acaafa4a155cb4aa2 |
|
BLAKE2b-256 | 73afd3b9fb5716a4261052cb4b79d208f0cef487b90fb44ac2bc779dbe07fe9a |