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:
<span class="highlight"><span class="int">1</span>, <span class="int">2</span>, <span class="int">3</span></span>
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 an example of this, see examples/calc.py and 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.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a1ab649b633fa50de76edfb67c1e046f2bef7ce2d2f72f05e55f6da4be1904e |
|
MD5 | 78668eacd62a8791215e3e94479cd20d |
|
BLAKE2b-256 | bf30492c44115601618d47ce2453d4b93831a1a6a32d94f14a7582ddf0b31723 |
Hashes for pyparsing_highlighting-0.1.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b39462be2a156be8d340eb429d8dd143fc49cb3a5f0aa68d968d28b16a40ade |
|
MD5 | 2e61b690179fdf2a64cd0e82fd14f504 |
|
BLAKE2b-256 | 53a593e2f5d8a7247de9b82150a08e85e38957b84fbe934827ed7050eacee0e5 |