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.2-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7896b6d99fd084e1bd17321762a3c8e6deb43eb569c71cbb03bd4000aaaf699 |
|
MD5 | a6fc4521c4277b87b9e9c80209fa9b07 |
|
BLAKE2b-256 | 6a192975d9640fa4ff0e21b4e5625630089a34701d15f6871a516c1394204496 |
Hashes for pysyntect-0.1.2-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46f4d9df9868842fe1d06ab9051fd472dd09a2f244801fadc6c5b6a658393c24 |
|
MD5 | 85a92780c6a5b29222463fee2d025769 |
|
BLAKE2b-256 | 3b16d07a994575e24f3aa3eeb8f86be524142e9ea561b18a75215d40e8c6734d |
Hashes for pysyntect-0.1.2-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c2ca705e9e7992bb4bafa374dc1c182c4a681f95c90cc502f2eca2db2a43691 |
|
MD5 | e69829fc9dfaf979f31f3dcfa845416c |
|
BLAKE2b-256 | 97cec657ade7afc950585a9715d663ca428460f7a03c1f4c21e336700384b261 |
Hashes for pysyntect-0.1.2-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7062a6007ea907ba69bcd2eb4324f84811ec60dbb5ac1405428295262f733614 |
|
MD5 | dcac894e65f97fc2806f7100572ad854 |
|
BLAKE2b-256 | 98da916c03d73e92c1050594d8e15d494b81a42b76c6d6557f7a6c01568bc84e |
Hashes for pysyntect-0.1.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49538d2c3adb4fc1ab06e652a67b64e3794ae77575a87b8975005eb92c00993e |
|
MD5 | a806837aad9422fade05c62dab884846 |
|
BLAKE2b-256 | 77f575963eaa0ca99c046117e275ef0254a91e4983769edd3db46236c8ef21b0 |
Hashes for pysyntect-0.1.2-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dd4a2f30b79ebba0dd13793f4f7cee8e161c679c29d74fa4a17d280c3ab77fc |
|
MD5 | 5e45809a913b191e99f1ee644d123bc7 |
|
BLAKE2b-256 | 53a41e621712fb7ab7891899792abde04269f3bdd5a3be0fe2b6310b2f72758a |
Hashes for pysyntect-0.1.2-cp36-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d14108e07356573895e5eadba4e3b7e262868fa5a85b01524eaa7d4aabfd936 |
|
MD5 | c722ffab442384fb44e7733111be2810 |
|
BLAKE2b-256 | 7ce5cd96abf9d45f0230b89a4046d2cdfe832371e652977d28bc7f968a54cfa0 |
Hashes for pysyntect-0.1.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d71f676abee900262683b02c1d3339fbe457cc8ebd11083f019d7e33f1eb0353 |
|
MD5 | 3da51d1947635f31776236d02e44f507 |
|
BLAKE2b-256 | 45d0a7d46e6827e14dc72e03cbacfaa539a8b321528d4cd545a502e758e55724 |
Hashes for pysyntect-0.1.2-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9a58de74fb0d9996b1fbb8ae9f01f90a9ac66d49bdb3c641f1ec016dc2ee476 |
|
MD5 | 0af0f358d59f9d5087c3c35c9b4478e7 |
|
BLAKE2b-256 | cf99aebfd254fd82a7350cc69c746c1cdf5c00a376e679aad13b2ab20199a42d |
Hashes for pysyntect-0.1.2-cp35-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abb364cd67d99298b635955e1826cac0ab5f2f39929b8bdbc820a0ec130772df |
|
MD5 | 770d3bb45b05b7e272f77406d43ac472 |
|
BLAKE2b-256 | 7dfce3d733b90fc12310c8f86ac0e7fa8ce6425d0249a5785cc273d9a25f1bf5 |
Hashes for pysyntect-0.1.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ea8f6352365adafdbac67c7acc6e8249f1be24f9936a27c2c1c55bd7e4bbab4 |
|
MD5 | e8344cb367e3eeed00c8f873c2e89abe |
|
BLAKE2b-256 | b28f502ea9c6c506ff64b49a40f125a049663d7e20e7bbb9a329c59a1cf8eeab |
Hashes for pysyntect-0.1.2-cp35-cp35m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84c147029649f41ad2300160cf62e49cf22a55f19ae6e9aed4b69daffd9c8f8c |
|
MD5 | 012ef0864dffb91067121eab63398d7b |
|
BLAKE2b-256 | ea2da5540b27f4dc0ff2e1c7da0dbd8188987d7f5fc0784c9e8575ec28838258 |