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.1.tar.gz (3.3 MB view hashes)

Uploaded Source

Built Distributions

pysyntect-0.1.1-cp38-none-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

pysyntect-0.1.1-cp38-cp38-manylinux1_x86_64.whl (2.5 MB view hashes)

Uploaded CPython 3.8

pysyntect-0.1.1-cp38-cp38-macosx_10_7_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.8 macOS 10.7+ x86-64

pysyntect-0.1.1-cp37-none-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.7 Windows x86-64

pysyntect-0.1.1-cp37-cp37m-manylinux1_x86_64.whl (2.5 MB view hashes)

Uploaded CPython 3.7m

pysyntect-0.1.1-cp37-cp37m-macosx_10_7_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.7m macOS 10.7+ x86-64

pysyntect-0.1.1-cp36-none-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.6 Windows x86-64

pysyntect-0.1.1-cp36-cp36m-manylinux1_x86_64.whl (2.5 MB view hashes)

Uploaded CPython 3.6m

pysyntect-0.1.1-cp36-cp36m-macosx_10_7_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.6m macOS 10.7+ x86-64

pysyntect-0.1.1-cp35-none-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.5 Windows x86-64

pysyntect-0.1.1-cp35-cp35m-manylinux1_x86_64.whl (2.5 MB view hashes)

Uploaded CPython 3.5m

pysyntect-0.1.1-cp35-cp35m-macosx_10_7_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.5m macOS 10.7+ x86-64

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