Skip to main content

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, 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.

Rust

fastpylight can also be used as a Rust library without PyO3:

[dependencies]
fastpylight = { path = "../fastpylight", default-features = false, features = ["standard-languages"] }

Use write_highlighted_inner when the caller already owns the surrounding <pre><code> block:

let mut out = String::new();
fastpylight::write_highlighted_inner("def f(): return 1", "python", "hl-", &mut out)?;

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. Use the all-languages feature if you want Lumis' full language set.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastpylight-0.1.8.tar.gz (21.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

fastpylight-0.1.8-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

fastpylight-0.1.8-cp313-cp313-macosx_11_0_arm64.whl (8.6 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

fastpylight-0.1.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

fastpylight-0.1.8-cp312-cp312-macosx_11_0_arm64.whl (8.6 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

fastpylight-0.1.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

fastpylight-0.1.8-cp311-cp311-macosx_11_0_arm64.whl (8.6 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

fastpylight-0.1.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

fastpylight-0.1.8-cp310-cp310-macosx_11_0_arm64.whl (8.6 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file fastpylight-0.1.8.tar.gz.

File metadata

  • Download URL: fastpylight-0.1.8.tar.gz
  • Upload date:
  • Size: 21.4 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

Hashes for fastpylight-0.1.8.tar.gz
Algorithm Hash digest
SHA256 ef9d4e9072dfc46bd21057e80719ddfb8364f6a22407759ec6a56ba75d24d836
MD5 f8119cee37be3daa15d23e52adb21683
BLAKE2b-256 7de1cc413c5494f483a922d65c6832790eb5bf06ce67d7625b0775a90ac303cd

See more details on using hashes here.

File details

Details for the file fastpylight-0.1.8-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: fastpylight-0.1.8-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 8.3 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

Hashes for fastpylight-0.1.8-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e52417a0e5c2c6b28f89644b7ad86cdd6fa1a06e0a38bf013db2b972394ad2f2
MD5 84cd7795ab3b8ac2fae76f816cee5964
BLAKE2b-256 f57068caac20a9f0746bf77d4ffb709ef40822ee998aaa8a9cf63abc90d4d04a

See more details on using hashes here.

File details

Details for the file fastpylight-0.1.8-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

  • Download URL: fastpylight-0.1.8-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

Hashes for fastpylight-0.1.8-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c944fadcb31fd14f9815d30db6bfece703a3c80d93c1748eb42e6b3fdc6d3761
MD5 a5035f2b18109684d9a697781b6ab646
BLAKE2b-256 7e5a0fc03f38670b742edef3ecd4a0fe1947e67717ca91c4d948fc5ec5c4183b

See more details on using hashes here.

File details

Details for the file fastpylight-0.1.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: fastpylight-0.1.8-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

Hashes for fastpylight-0.1.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c47fa17282cdb0a7554f81213547b79d4c352c7bdbf68fd8f72414106f7088c3
MD5 cfdda755e6cee3a1441b36fb6a954da5
BLAKE2b-256 e2122cd98bae3c97144707b2dcd9fb9eeff9b507d8fca63f192301579ab2f681

See more details on using hashes here.

File details

Details for the file fastpylight-0.1.8-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

  • Download URL: fastpylight-0.1.8-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

Hashes for fastpylight-0.1.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7c468208ecfe8e2bcdd9e46149baa79827fb4791a553fa0a73630bfd98b84d35
MD5 32b4a20b099d25fa365cd88d78b6ab4c
BLAKE2b-256 4b1dd60ba65f2f47d17ef0e795f83d3fb13e6957599dfa3672b1369a3882aa17

See more details on using hashes here.

File details

Details for the file fastpylight-0.1.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: fastpylight-0.1.8-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

Hashes for fastpylight-0.1.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 23a39b4cd41c7de3a2e0ed2e23cc24b29c1dd260aa6b30f1785f5dee694f5864
MD5 6a973bd94289ce040cc24e47d5ca9c31
BLAKE2b-256 474830168363249a97bcd8bc78f9776f58655b39d97282f979f00cd5505e6035

See more details on using hashes here.

File details

Details for the file fastpylight-0.1.8-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

  • Download URL: fastpylight-0.1.8-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

Hashes for fastpylight-0.1.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0770c1d710bc8d41277b1f4873f7a2d6153eccc03ebc6c639c152e929abf46e2
MD5 009dd482cabe13ae1f66ac5172bde6d7
BLAKE2b-256 c9c744ca3178e9a7dd28a476c6df4c526eda4a84a981087ffc3a99623e343bf1

See more details on using hashes here.

File details

Details for the file fastpylight-0.1.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: fastpylight-0.1.8-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

Hashes for fastpylight-0.1.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d7d0a8375e25d3684543289d0e448ebf0432c6b186efc5531aaac6ce7807fbb2
MD5 77b2b25226364cc3030bdf6238485761
BLAKE2b-256 46451e664a8fc99a38c75cbd08d8e2919c9be3084ade266b6780456e9427a4b6

See more details on using hashes here.

File details

Details for the file fastpylight-0.1.8-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

  • Download URL: fastpylight-0.1.8-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

Hashes for fastpylight-0.1.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b85d75d08d6ac4e61b9a84ae570ae3e4c6c223245c49edd5a520d68fe6210077
MD5 4a7f0c031d1531af230d21098ccfeb22
BLAKE2b-256 16f4f7612279e8b1321e6cae5c21322f089400ac3b7cbe4cbb5cc2a7298ed2c7

See more details on using hashes here.

Supported by

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