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 stable/beta/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.3.0.tar.gz (3.3 MB view details)

Uploaded Source

Built Distributions

pysyntect-0.3.0-cp39-none-win_amd64.whl (2.5 MB view details)

Uploaded CPython 3.9 Windows x86-64

pysyntect-0.3.0-cp39-cp39-manylinux_2_24_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.24+ x86-64

pysyntect-0.3.0-cp39-cp39-macosx_10_7_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

pysyntect-0.3.0-cp38-none-win_amd64.whl (2.5 MB view details)

Uploaded CPython 3.8 Windows x86-64

pysyntect-0.3.0-cp38-cp38-manylinux_2_24_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.24+ x86-64

pysyntect-0.3.0-cp38-cp38-macosx_10_7_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

pysyntect-0.3.0-cp37-none-win_amd64.whl (2.5 MB view details)

Uploaded CPython 3.7 Windows x86-64

pysyntect-0.3.0-cp37-cp37m-manylinux_2_24_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.24+ x86-64

pysyntect-0.3.0-cp37-cp37m-macosx_10_7_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

pysyntect-0.3.0-cp36-none-win_amd64.whl (2.5 MB view details)

Uploaded CPython 3.6 Windows x86-64

pysyntect-0.3.0-cp36-cp36m-manylinux_2_24_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.6m manylinux: glibc 2.24+ x86-64

pysyntect-0.3.0-cp36-cp36m-macosx_10_7_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.6m macOS 10.7+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pysyntect-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ffcb227dc57bffe6e194842944f8d16e49d77d6affd08c8c213d0e2d4e956c43
MD5 e06f38f168cd8dcc94c894facf747e67
BLAKE2b-256 294b2459fcaca396848385c620529c6f24711ff522a05db2c9309b1d9ae47a66

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.3.0-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 61b70ad75477acafe63e567e6162f052b43d73e9a21609592163371d6c59fcf8
MD5 d7a8a838a6acf55fb1779d574b8c352b
BLAKE2b-256 d65c1187c8269e8a66f3b9bcdd4f60f40f9a836cd0bd06a7426ef26224b90963

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.3.0-cp39-cp39-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp39-cp39-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 864307f1da6a45872601ba7389eec48e768cd66268a143ea689a75bf5b262cb9
MD5 2cb9dd2f5ae6595209da168beabbecf5
BLAKE2b-256 e1c8cd0bcfc2d806d9e585219fcad692856e98dfdef2ba5ed54af2d8251e0945

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.3.0-cp39-cp39-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 dd0633f3843bc91106a628d0226a6d737fcdd2f230792d474f9fb0839de7c2be
MD5 e0feace27a7821727856d2782ae13df4
BLAKE2b-256 eedbde95f16f6c7d0458e746da455da5b9fe726dd0a2da03a651564fc0c5d06b

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 b7d7e214c599a3a8cc65b24c5d6ee73186cbc5112cd609e4c767cbd450d023a4
MD5 113c2ffbb20988a1ff36e97d03c3298d
BLAKE2b-256 bb5b42bb5f4d608f8f0afac46e955213c026133dba005d39e604599bcfccd30e

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.3.0-cp38-cp38-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp38-cp38-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 80973fe0c6bada07405c4d498bcefd5d49a94b3a633fbd698d1d00f1347d684f
MD5 7e0b1fe0f33282e99833f21307b46694
BLAKE2b-256 8745a262775155bf8f378288c3991ce4a6b73ed8a27c4263691f51897891e2bc

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 7af9671da2753813ba3665e41a2a83880b74cbfe810780754be388698215c8ea
MD5 3623e3cd478929d120abedb9a63bd007
BLAKE2b-256 0cbaf17b6d5675501fd2005c16e2dcf0f99a1519a86210d08a9282821d7bec6a

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 65d930b9ccce5a8058158436e16442dfd363fa64a855d5ed560facb9e060d4e1
MD5 b5bf63005f6197cea384bfe3b37e49e9
BLAKE2b-256 b0262a8bffa71d260ec9038d1d2f6b73d5d550e510bc3f60babf3cb529caa7a9

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.3.0-cp37-cp37m-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp37-cp37m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 21303f8544c380fbb4cf64497d4898a164915cdebc733ce42072ee512cd66c39
MD5 b6be9d1b86cb6ee05d410c75221f56fa
BLAKE2b-256 b8d2fd9db2d780c65ff4149743844e80234e1f1164db5e890ca0b20236d38538

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 458c9e748151b66178e2ed617c2466d08f7c52c1938641dc5695dbb6f5a5a31c
MD5 2b0daa84eca8d34078f35d1180feea74
BLAKE2b-256 96892dbea8c202252ab12bbf97aa911952e6231a5bd4356e5e1ea0918d413f16

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 f910f40c84e7fc83e8ff2befe34c2bab68f68e5b9decc0d9bf8a80dc355d5661
MD5 553ed1caa2d636f74e7a2d04c22d7a15
BLAKE2b-256 a5d589c6d5c1bedef1ce3a0c31f3f4fee3da7d2465b902c22ae263e63b40287a

See more details on using hashes here.

Provenance

File details

Details for the file pysyntect-0.3.0-cp36-cp36m-manylinux_2_24_x86_64.whl.

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp36-cp36m-manylinux_2_24_x86_64.whl
Algorithm Hash digest
SHA256 6619c93fe3c46f9a22c25098fdf6b72d2735b5b9a4bada00c4ae4a4f9fe6de0a
MD5 61ba1ec003ea741629c4fe8296d1a2e0
BLAKE2b-256 1cfdbfba4f8330f08f883ec05da2c75d18c0868a2157c973acf996e196e615d1

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pysyntect-0.3.0-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 837777bb3356f1838ae6aed4f31bf4a499d90736f0a115e82593e0d690328f27
MD5 11fc8182f9d91d85790662f3189e3797
BLAKE2b-256 a8be3d03e869010fbde1a91d6242d5d240ef5debdb7e7877645cbfff60494918

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