Python bindings for the syntect syntax highlighter.
Project description
pysyntect
Copyright © 2020– Spyder Project Contributors
Overview
Python bindings for the Syntect library. Pysyntect provides a lightweight, fast engine to compute syntax highlighting using Sublime Text syntax definitions and TextMate theme definitions, which are shared by many editors.
Installing
To install pysyntect, you can use both conda or pip package managers:
# Using Conda (Recommended)
conda install pysyntect -c spyder-ide
# Using pip
pip install pysyntect
Dependencies
To compile pysyntect, you will require the latest nightly release of Rust, alongside Cargo. Also, it requires a Python distribution with its corresponding development headers. Finally, this project depends on the following Cargo crates:
- PyO3: Library used to produce Python bindings from Rust code.
- Syntect: Syntax highlighting library in Rust.
- Maturin: Build system to build and publish Rust-based Python packages
Compilling locally
Besides Rust, you will require the latest version of maturin installed to compile this project locally:
pip install maturin toml
After installing those packages, it is possible to execute the following command to compile pysyntect:
maturin develop
In order to produce wheels, maturin build
can be used instead. This project supports PEP517, thus pip can be used to install this package as well:
pip install -U .
Running tests
We use pytest to run tests as it follows (after calling maturin develop
):
pytest -v syntect/tests
Package usage
Pysyntect provides utillities and functions for loading themes and syntax definitions, as well to highlight text strings. Pysyntect supports over 500 syntax language definitions across many TextMate theme definitions (not included as part of this package).
from syntect import (highlight, load_theme_folder,
load_default_syntax, load_syntax_folder,
escape_to_console)
# Load default syntax grammars
syntax = load_default_syntax()
# Load syntax grammars from a path
syntax = load_syntax_folder("path/to/a/folder/with/grammars")
# Languages supported by the syntax set
syntax.languages
# Load theme definitions from a path
themes = load_theme_folder("path/to/a/folder/with/themes")
# List of themes loaded
themes.themes
# Select a theme
theme = themes['<name_of_the_theme>']
# Load a file and syntax highlight it
with open('my_file.extension', 'r') as f:
lines = f.read()
# Returns a list of styles to apply per token
color_ranges = highlight(lines, 'extension', syntax, theme)
# Get background/foreground colors by token
style, token = color_ranges[0]
background = style.background
foreground = style.foreground
components = ('r', 'g', 'b', 'a')
bg_red, bg_green, bg_blue, bg_alpha = [getattr(background, c)
for c in components]
fg_red, fg_green, fg_blue, fg_alpha = [getattr(foreground, c)
for c in components]
# Preview syntax highlighing result to console
escape_to_console(color_ranges, display_bg=True)
Changelog
Please see our CHANGELOG file to learn more about our new features and improvements.
Contribution guidelines
We follow PEP8 and PEP257 for pure python packages and Rust to compile extensions. We use MyPy type annotations for all functions and classes declared on this package. Feel free to send a PR or create an issue if you have any problem/question.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pysyntect-0.1.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcf32df721263d4c468be6203c4b3d43f793185c5910ec25eac23b330508708d |
|
MD5 | 1aeb5985765e0c454bcb41542c406193 |
|
BLAKE2b-256 | 9abf2db8f3b14a9ecf74133f7045a44fd12330e4f7d1bbd3dd3cc8f14412d7c6 |
Hashes for pysyntect-0.1.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcb36744a09fbfbf67b4ae8e5116f5c57dc8701b1aa11f63a9bee0e787660737 |
|
MD5 | e5dfd57a72122afa7bf7155a7fdd1e54 |
|
BLAKE2b-256 | 36516c8c1489307b786851fe9195555792b81f7aa0b34cf6aee306743933a779 |
Hashes for pysyntect-0.1.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10c903bfc78430c1ad9b948eeddeb334850ef742b980e789c07703ce4a981765 |
|
MD5 | 3bf3511734bb56f18bfc02258221249f |
|
BLAKE2b-256 | 18f2af9ccdd2a4adb6abf24c15bed7c152bea87c15c27c04a1084cfffee814df |
Hashes for pysyntect-0.1.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbe9fdbd92a4502d5fa166e4d7742b2896b2d2caec0c3a8b15a5964516ced53c |
|
MD5 | f6f4b2e4e7a4fbc7f1d40ae5123650b8 |
|
BLAKE2b-256 | f3d7241d442ff3b0228fb72dd85494806d5429cd8b815f51a4a01e789b6e78d6 |
Hashes for pysyntect-0.1.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eaecf681b9818260c7eb6da29ca574fc6cfd9ee397986c3ea73bd12534fecae2 |
|
MD5 | ec973f2b9bf2c6a14abbd04e77796396 |
|
BLAKE2b-256 | ba7221252f7a7e343e7b6e98d180ad50809cd158acbf5d519fb2f09da6e7f5e5 |
Hashes for pysyntect-0.1.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 108a0715a9d7db9da0b9f867b21c76a799571319282ecbb6217ee6bd890e1da9 |
|
MD5 | d4bb5db8373b3215cf3c822508408678 |
|
BLAKE2b-256 | 9324c7da5f0a79e2b3c1f461e31636da0ccdeacd26f7d6826c9d9c1ac5c3647a |
Hashes for pysyntect-0.1.1-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e971549eb56bd8f9c8d7ef3dcf4953eb98b513cd75674a06b7776ce4dea1f3e2 |
|
MD5 | e2aca4ea176af568274ca748436e62be |
|
BLAKE2b-256 | 8aed4ca91c82f352fd151f28f40f67da15d360376fb0a48761f62aaf9ce5e761 |
Hashes for pysyntect-0.1.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32b6c095cf4ceca4a847203a99e1b421cb0eacd2ca270b6e1fea2747d7de8ba3 |
|
MD5 | b0b36c4df56f616420703570de4367d7 |
|
BLAKE2b-256 | e68cb526059139f3b5f36c28e34a253e63de81f4fea4687200e49942a833d729 |
Hashes for pysyntect-0.1.1-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9537a485fd57970d39ef501f303f7c454bd5e2e5ba148e44bc9431c720f20515 |
|
MD5 | e35b4f699bbc66d0dc51f48e7bd8c5f1 |
|
BLAKE2b-256 | 486a08bd4823a258f5b542695e496bdff495db9d60ba8fa6ffcf87459ce670df |
Hashes for pysyntect-0.1.1-cp35-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37abd6e248335527b04cb97ba472c9aec017c490a3a08648549e822e1a149a97 |
|
MD5 | 3fa97ae3b7ec57051ed64e329ad78480 |
|
BLAKE2b-256 | 2ab76055ddf0d7580ba8978d1e00df40d39acc0149ba767373b34ef31bd89983 |
Hashes for pysyntect-0.1.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e24cbcfdd89a8e9d6629846a4a208ec25b10ebc09d03ca22a98a5160e1c0fb68 |
|
MD5 | 4e2bc59f4ee82a65d7a714d3410cca95 |
|
BLAKE2b-256 | 44df721b3170c06085f292c1f495826dbaaebcc1995e995bbf4d0b67dfe2a9cc |
Hashes for pysyntect-0.1.1-cp35-cp35m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b2396f6e4fed85b351df6f7cb9461ea1cce7a6aeae74480c14adce9c67740ea |
|
MD5 | 9838951cf9706cbdb19bdc41c70462c8 |
|
BLAKE2b-256 | 7bb5e73244d1768c3b4642f5a70fdcd5bf424ad960b493cae3b5d3d858171f9f |