Skip to main content

Python bindings for the syntect syntax highlighter.

Project description

pysyntect

Project License - MIT pypi version conda version download count Downloads PyPI status Linux build MacOS build Windows build

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.

Python example

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

pysyntect-0.1.0.tar.gz (3.3 MB view details)

Uploaded Source

Built Distributions

pysyntect-0.1.0-cp38-none-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.8 Windows x86-64

pysyntect-0.1.0-cp38-cp38-manylinux1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.8

pysyntect-0.1.0-cp38-cp38-macosx_10_7_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

pysyntect-0.1.0-cp37-none-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.7 Windows x86-64

pysyntect-0.1.0-cp37-cp37m-manylinux1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.7m

pysyntect-0.1.0-cp37-cp37m-macosx_10_7_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

pysyntect-0.1.0-cp36-none-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.6 Windows x86-64

pysyntect-0.1.0-cp36-cp36m-macosx_10_7_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.6m macOS 10.7+ x86-64

pysyntect-0.1.0-cp35-none-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.5 Windows x86-64

pysyntect-0.1.0-cp35-cp35m-manylinux1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.5m

pysyntect-0.1.0-cp35-cp35m-macosx_10_7_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.5m macOS 10.7+ x86-64

File details

Details for the file pysyntect-0.1.0.tar.gz.

File metadata

  • Download URL: pysyntect-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.8.0

File hashes

Hashes for pysyntect-0.1.0.tar.gz
Algorithm Hash digest
SHA256 61f55c4e54ca88b0c5ef115b7c815149e22921559f8d24059140ca91d62bced5
MD5 ed8907aa99b8f8dac227b18a09fd72a3
BLAKE2b-256 429b9e11a9194eae93cf403995793e6c09958339d31d84c26f794e54b70f6803

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 62a36efbfe91cb0dca9ea081829ae7376547127a68228ff905c777d920b1532a
MD5 86e51ae0129fde00c2155c3d8211d494
BLAKE2b-256 e1a31378111cc12775a676405dabfcc06ad161c392b7e39fcf324d55150e7f68

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 149ec08aec1677c1ddef537597a020167f854d854bae0cfc48b4bc06bf4c4f56
MD5 6b6009e2483e60e406ccb3fa4be7882a
BLAKE2b-256 461094dfe1049bd1be6bdedbea54be05aa19cd27df0992897a098c7c9c04c82b

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 60ce19297be243a58e053c82f384d396c7c753845df2bba19ebc9b1f8031a42a
MD5 a3950714f96eb5c8886b35facc9eb5eb
BLAKE2b-256 1bf6dbaa206bc6141bda1aaffad9998f4a7e7da464d7440f8f94b5c19a581810

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp37-none-win_amd64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 9a63b3feeafb5d9f337339abb54ce6d2190fc1dc3f789feddc5bf51b746add58
MD5 1f6d8d498227501c2659c6b38cda1dff
BLAKE2b-256 0315b25faf95b5e5b4684ad939a745b59778f9eeaf4310f9393ebcd6ab9d32cb

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a630e368298fdfa6136ae58bce79bfa9659437addb43403bc1be87b3f25309c0
MD5 adc2ce6a73dd923faab72a4c6d18f984
BLAKE2b-256 d71c849890ade20cf3c597a7e9f1c3df9759162a4ba13e4b753bd576bc2ef6fe

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 95fecde4c7dbd89933386617d856048ae5c2782363f8161e7636fa6b7cddb8bf
MD5 7112482cab55c6fa7b53fd1b802e2a1a
BLAKE2b-256 460c9fb69524e8d5622d61c8b49618697086b28885cceb75a04fec618fc92df4

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp36-none-win_amd64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 b59df79e6fc913a9b57a35104f73d9464b8b0f5dd5175f75fc166a3ca2704265
MD5 d01b60b6e2881a3146337bdd47bae466
BLAKE2b-256 700b2975a7980359f67a0f14faf99eabfe70e55d0cd5c44e4ddac6bd6d34f057

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp36-cp36m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a8e8c54d4b758e58cd5d97a70a6e0cd13a6733f4ea3fddc880a289f3e22ecc19
MD5 ae5eb278bf465cc975921fcf4302e160
BLAKE2b-256 2900be9942f3361c34c9056eb6e86abd5ab338e52c77ef50ed9ca1652e04f1a1

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp35-none-win_amd64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp35-none-win_amd64.whl
Algorithm Hash digest
SHA256 90d6f6e9b287a29abde1b25cf1245e3362b70f87a06c64fbc5af1124fa04ca15
MD5 210a54d608351f71c004a6bfea93d767
BLAKE2b-256 4a8a75e3df769a2fcbbb77d71709b24356882741688fc21c484601ff0dc5a568

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 05de26dec02a9c0bec217a0ba3b8b056718c9ff5910af87ad90321bc9169eda6
MD5 2f17264494f7c0286051fee9c96af725
BLAKE2b-256 911a01a9dc1fad40c1f2aebc48d0b4d97d31329f0a4b335be57300f3a2ed8696

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.1.0-cp35-cp35m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.1.0-cp35-cp35m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 1da2b2afd517d19eba9958fe1808e9c91589691f9222ff884707b13231bcaaa4
MD5 7993629994f305e4caf17df1626d1c54
BLAKE2b-256 9b79953c8ed6f67a93afe48a4bdf5ab8d9129f08b584e274c23a3112432e3da7

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page