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
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, 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.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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyparsing-highlighting-0.2.5.tar.gz.
File metadata
- Download URL: pyparsing-highlighting-0.2.5.tar.gz
- Upload date:
- Size: 6.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5a615b165250881d715385886b507f6cc0d141ae1c85a74e0a51b124e05fa0c
|
|
| MD5 |
8017481790b24a410eed23058698d3db
|
|
| BLAKE2b-256 |
e37f8f36a89c6aa9ffb29a0aaff66f6e4bd4aa5cc679fd9ada2c6c76e7bac520
|
File details
Details for the file pyparsing_highlighting-0.2.5-py3-none-any.whl.
File metadata
- Download URL: pyparsing_highlighting-0.2.5-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4f93a19afb415b34977dbd810cf8b160bc70771b72ba06ab054970ab853580b
|
|
| MD5 |
e03d5bb59d38b35f4e4c0f26753628e5
|
|
| BLAKE2b-256 |
b1709c49d55c8a4bda34c4fe08a2370e204daabccf14eabca8094ee83358eb23
|