Fast syntax highlighting using Lumis and Tree-sitter
Project description
Installation
pip install fastpylight
fastpylight is a small Python/Rust wrapper around Lumis, a Tree-sitter syntax highlighter with Neovim themes. Language names, token scopes, and theme names follow Lumis.
Python
from fastpylight import highlight, highlight_spans, guess, theme_css
code = "def f(x):\n return x + 1\n"
# Output for the CSS Highlight API.
html = highlight(code, "python")
# Static span output.
spans = highlight_spans(code, "python", "hl-")
# CSS rules for the span output.
css = theme_css("github_light", "pre code", "hl-")
highlight returns HTML with token ranges in UTF-16 code units, for use with the CSS Highlight API:
<hl-code toks='[[0,3,"keyword"],[4,5,"function"],[6,7,"variable"],[14,20,"keyword"],[21,22,"variable"],[23,24,"operator"],[25,26,"number"]]'><pre><code>def f(x):
return x + 1
</code></pre></hl-code>
highlight_spans returns normal HTML spans:
<pre><code><span class="hl-keyword">def</span> <span class="hl-function">f</span>(<span class="hl-variable">x</span>):
<span class="hl-keyword">return</span> <span class="hl-variable">x</span> <span class="hl-operator">+</span> <span class="hl-number">1</span>
</code></pre>
The exact classes depend on Lumis scopes. A complete GitHub Light CSS example for both output modes is in docs/github_light.css.
Languages
The default language set enables Lumis web, web-extra, system, and backend bundles, plus R, Julia, PowerShell, Lua, Swift, MATLAB, Perl, Pascal, Fortran, and Objective-C.
languages() lists every available name. The highlighting functions need an exact language name and raise ValueError on an unknown one. Use "plaintext" for un-highlighted output:
highlight(code, "plaintext") # toks='[]', no tokens
To detect a language instead of specifying one, use guess, which takes the code plus an optional hint (a name, extension, or filename) and falls back to "plaintext" when nothing matches:
guess("fn main() {}", "rs") # 'rust' (extension hint)
guess("#!/usr/bin/env python\n") # 'python' (shebang)
guess("just some prose") # 'plaintext' (no match)
highlight(code, guess(code)) # detect, then highlight
CSS Highlight API
The highlight function is for browser code that applies token ranges with the CSS Highlight API. A minimal component looks like this:
class HlCode extends HTMLElement {
connectedCallback(){
if(!CSS.highlights) return;
setTimeout(() => {
const d=this.getAttribute('toks'); if(!d) return;
const tn=this.querySelector('code').firstChild, toks=JSON.parse(d);
this._ranges=[];
toks.forEach(([s,e,k])=>{
const r=new Range(); r.setStart(tn,s); r.setEnd(tn,e);
const h=CSS.highlights.get(k)||new Highlight();
h.add(r); CSS.highlights.set(k,h);
this._ranges.push([r,k]);
});
this.removeAttribute('toks');
}, 0);
}
disconnectedCallback(){
if(!this._ranges) return;
this._ranges.forEach(([r,k])=>{
const h=CSS.highlights.get(k); if(h) h.delete(r);
});
this._ranges=null;
}
}
if(!customElements.get('hl-code')) customElements.define('hl-code',HlCode);
highlight needs ::highlight(...) rules. highlight_spans needs class rules. docs/github_light.css includes both for the default hl- prefix.
You can generate CSS at runtime:
highlight_css = theme_css("github_light")
span_css = theme_css("github_light", "pre code", "hl-")
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastpylight-0.1.11.tar.gz.
File metadata
- Download URL: fastpylight-0.1.11.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53cfd41a592a4ff6c162e58c0378cc95f7a21e298b04febba18824a6b5131c4a
|
|
| MD5 |
d271654dc37d717fadb90d179c47dbbf
|
|
| BLAKE2b-256 |
8a0427e7d54fa39f7bef62aecde7486f3360a9a97b5d19a5332105f653b4de06
|
File details
Details for the file fastpylight-0.1.11-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: fastpylight-0.1.11-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 8.4 MB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c16cbc529d5348b2f41a2f174906874c939fd7361c31f3ccf146685cf837d1e
|
|
| MD5 |
5f5b9803cfeb6315c7ac356ddf60939b
|
|
| BLAKE2b-256 |
d7d8919740569ff0d90d6d425f4927e94c87340f46318cc7df745d4e73c423bb
|
File details
Details for the file fastpylight-0.1.11-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: fastpylight-0.1.11-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.6 MB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91b03a04d281567c22ed4b676fb385d6b9cb7169243bb1216fdad850cd49ec5d
|
|
| MD5 |
a3fd78739ea3cbc0562935dc5cba1ee7
|
|
| BLAKE2b-256 |
3bf3385022f778888767a420fc8a9f8789c2d32ef5023536ad7ad06b0970aef3
|
File details
Details for the file fastpylight-0.1.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: fastpylight-0.1.11-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 8.4 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79001f563e17be1f96d0c3062d0409e9b20633b0afbc722fccd4bfbb53a8d3ae
|
|
| MD5 |
47c914c5cbf8115cb639ceb880ec6800
|
|
| BLAKE2b-256 |
2275199481deb43d82549b7ee7c64422f7da0afc2d1b0c25a7425a6373cdde31
|
File details
Details for the file fastpylight-0.1.11-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: fastpylight-0.1.11-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.6 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
882ddb6a05c7c894db973ab694fb2ab41968188a0e1239f53916632c9d06b8ba
|
|
| MD5 |
eaf40fd5c0bf81746aa06af14eb22d64
|
|
| BLAKE2b-256 |
c226d1bfb6e7258fe7bd69366f93b30973a8e34ae93b47dc54f51b418eb3aaa3
|
File details
Details for the file fastpylight-0.1.11-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: fastpylight-0.1.11-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 8.4 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b2d3b76ec9f7a4dee7a42798063db104820e11f92b69bf5a73e2fc3d4b08f10
|
|
| MD5 |
fc8fcfe6022f1d9111b695540cb5c5d0
|
|
| BLAKE2b-256 |
6ed7a253aaf1361b6649aa4bb6d57fde0d27022db9ea8ff1c898e8b17c567d23
|
File details
Details for the file fastpylight-0.1.11-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: fastpylight-0.1.11-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.6 MB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7511c5f2942b7134e4aff25c937199d372e73d77bf692d38cef344effd0be48b
|
|
| MD5 |
51734d0b63047ac6f12a4300e93c63e1
|
|
| BLAKE2b-256 |
c014e8e587c2e102beb34786b407d8567d6d4f8e9a90c4a2137f653bfc72dddb
|
File details
Details for the file fastpylight-0.1.11-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: fastpylight-0.1.11-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 8.4 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f52765904e72fcbbed2d9640a683aea697c5451922cdfe3c525c8d038902b7e2
|
|
| MD5 |
1bc5d5534766a0f36273031f2cebab87
|
|
| BLAKE2b-256 |
5ef51942d765cd53bbb72ab5b058a1f6ad441849bba0e33bd1ee10969dde5f21
|
File details
Details for the file fastpylight-0.1.11-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: fastpylight-0.1.11-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 8.6 MB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
faf7f80f705a46caec92f293f8dad02ac30b8895c422d90d8bc3701e5c491d79
|
|
| MD5 |
44b4640893b9df1958975499080dcca8
|
|
| BLAKE2b-256 |
43637e622fa9b78b3c5028fdc8185be1d2e56f0dc67fe95aa3fd8a84f9e0a675
|