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.

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.

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.10.tar.gz (21.0 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.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.4 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

fastpylight-0.1.10-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.10-cp312-cp312-macosx_11_0_arm64.whl (8.6 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

fastpylight-0.1.10-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.10-cp311-cp311-macosx_11_0_arm64.whl (8.6 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

fastpylight-0.1.10-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.10-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.10.tar.gz.

File metadata

  • Download URL: fastpylight-0.1.10.tar.gz
  • Upload date:
  • Size: 21.0 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.10.tar.gz
Algorithm Hash digest
SHA256 55aee4474180d482d2bb7d4eecc82993e2a920031b88243ac8a89eb982eed66a
MD5 c313b28fe93e8ee8eb20cbd2f8c022e8
BLAKE2b-256 aa9e63e6fbb99e297ab3f9568afc28fc1b31275f145b07be03cb46902705f0b7

See more details on using hashes here.

File details

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

File metadata

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

Hashes for fastpylight-0.1.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 852070e93d8550f47529688c6f50c13d775c4c6d1a6fd8a33655ec064eb72246
MD5 b3a5de2c2209c5be075fd9a05576571b
BLAKE2b-256 ff616ad59bcb8a582dd67d6098bbfa67c58c247c2291dec3d0a26f9f5895726a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastpylight-0.1.10-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.10-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cf4250acd4840ccd8995ee6227df2a8a091413c8552d4922fe06162c32733418
MD5 01c7926a1a43de4565b146dc754e43b2
BLAKE2b-256 47f38c5ed50792f11fa9e08c855afe51c014e63baf23a78de6627f13d6b79c84

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastpylight-0.1.10-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.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fb302fe83ec29eda96468bde7be4593e50c17d08bfc69cd24bf90fd32e6d9f63
MD5 987a9f8e5e7d574cacc34ceb5b660c6f
BLAKE2b-256 edb8d371150a65be867f74cd3acc20683684354a248ec0638fbca88d786347bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastpylight-0.1.10-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.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e7098347eb89e70c4b66cc65dcf1a74aadd4817ab68aeaa0aa201571aa3664df
MD5 58c083be82da37ed012fa7437cd6a006
BLAKE2b-256 f2e976798f43bbfe28ea95626d121780e0d20074ed278fec2fc1f3c338eec785

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastpylight-0.1.10-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.10-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 353ab21db1a7dbdbd9a840b89381a3966e152ed80a30848045ac82380d5b82da
MD5 d016e776450b023e1f229cde49b84c8c
BLAKE2b-256 84be99311c6b67b5c0cceb263fce4674d13a0c8eba679e646d3edaeb645b99e9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastpylight-0.1.10-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.10-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ebee44cf4072485b705612b2a284f4660c8e28b0b799c540c63203c4334925d3
MD5 94136abf1fd29da9b9e70d416ffa2ce7
BLAKE2b-256 91cb01107cac63f7b552a3053c5266b2a486ea3c71340e0e384b81911122ddf3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastpylight-0.1.10-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.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 28e5d6ee53d19f2730672d8d3be70f78cc67b387e49a305ac47e0461292349dc
MD5 99c7d63ccd7e8734397818e7ce73159b
BLAKE2b-256 f20d02aabef3d535e6eb8d886746d787dac03dd40e6379941a5528d6add2a7ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastpylight-0.1.10-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.10-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 36f24118e359545a131907cea6fe300310b946b50a9ba2522a1113c4687d18e4
MD5 1425ee70f170a00e5c53bee27d655379
BLAKE2b-256 86d395ba729c03d5be97c1aca9507d1f192cea4085e99cdd8e5592c7146954ae

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