Ultra-fast Python syntax highlighter for FastHTML/StarHTML with Datastar support
Project description
Starlighter
Starlighter is a high-performance, zero-dependency Python syntax highlighter designed for server-side rendering. Built specifically for StarHTML applications with full FastHTML compatibility for legacy projects.
✨ Key Features
- ⚡ Blazing Fast: ~3ms P99 latency for 500-line files, ~1.5ms for 200-line files
- 🔒 XSS-Safe: Secure HTML output with proper entity encoding
- 📦 Zero Dependencies: Pure Python standard library implementation
- 🎨 Multiple Themes: Built-in themes (VS Code Dark/Light, GitHub Dark, Monokai, Catppuccin, Dracula)
- 🌟 StarHTML Native: Built for StarHTML with Datastar attribute highlighting
- 🔄 FastHTML Compatible: Full backward compatibility for FastHTML projects
- 🐍 Complete Python Support: All Python 3.11+ syntax elements
- 📱 Mobile Responsive: Optimized CSS for all screen sizes
- ♿ Accessible: WCAG compliant with high contrast mode support
🚀 Quick Start
Installation
# Using uv (recommended)
uv add starlighter
# Using pip
pip install starlighter
Basic Usage
from starlighter import highlight
# Highlight Python code
code = '''def fibonacci(n):
"""Calculate the nth Fibonacci number."""
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(f"The 10th Fibonacci number is: {fibonacci(10)}")'''
# Generate highlighted HTML
highlighted_html = highlight(code)
Output:
<pre><code class="language-python">
<span class="token-keyword">def</span> <span class="token-identifier">fibonacci</span>(<span class="token-identifier">n</span>):
<span class="token-comment">"""Calculate the nth Fibonacci number."""</span>
<span class="token-keyword">if</span> <span class="token-identifier">n</span> <span class="token-operator"><=</span> <span class="token-number">1</span>:
<span class="token-keyword">return</span> <span class="token-identifier">n</span>
<span class="token-keyword">return</span> <span class="token-identifier">fibonacci</span>(<span class="token-identifier">n</span><span class="token-operator">-</span><span class="token-number">1</span>) <span class="token-operator">+</span> <span class="token-identifier">fibonacci</span>(<span class="token-identifier">n</span><span class="token-operator">-</span><span class="token-number">2</span>)
<span class="token-builtin">print</span>(<span class="token-string">f"The 10th Fibonacci number is: {fibonacci(10)}"</span>)
</code></pre>
🌟 StarHTML Integration
Starlighter is designed specifically for StarHTML with Datastar support:
from starhtml import *
from starlighter import CodeBlock, StarlighterStyles
app, rt = star_app(
hdrs=(
# Include all Starlighter themes
StarlighterStyles(
"github-dark", "vscode", "light", "monokai", "catppuccin", "dracula"
),
Style("""
.container { max-width: 1200px; margin: 0 auto; padding: 2rem; }
.code-example { margin: 2rem 0; }
""")
)
)
@rt("/")
def home():
code_sample = '''@dataclass
class User:
"""A user with DataStar reactivity."""
name: str
email: str
active: bool = True
def greet(self) -> str:
return f"Hello from StarHTML, I'm {self.name}!"'''
return Div(
H1("🌟 StarHTML + Starlighter"),
P("Server-side Python syntax highlighting with DataStar support"),
Div(
H3("Interactive Code Example:"),
CodeBlock(code_sample, theme="github-dark"),
cls="code-example"
),
cls="container"
)
serve()
🔄 FastHTML Compatibility
For projects using FastHTML, Starlighter provides full backward compatibility:
from fasthtml.common import *
from starlighter import CodeBlock, StarlighterStyles
app, rt = fast_app(
pico=False,
hdrs=(
StarlighterStyles("vscode", "github-dark", "light"),
Style(".container { max-width: 1200px; margin: 0 auto; padding: 2rem; }")
)
)
@rt("/")
def get():
code_sample = '''def hello_fasthtml():
return "FastHTML compatibility maintained!"'''
return Div(
H1("FastHTML + Starlighter"),
CodeBlock(code_sample, theme="vscode"),
cls="container"
)
serve()
🌈 Themes
Built-in themes available:
from starlighter import get_theme_css, CodeBlock
# Available themes
themes = ["vscode", "light", "github-dark", "monokai", "catppuccin", "dracula"]
# Get CSS for a specific theme
css = get_theme_css("vscode")
# Use with CodeBlock (StarHTML/FastHTML)
block = CodeBlock(code, theme="github-dark")
📊 Performance
Starlighter delivers exceptional performance optimized for server-side rendering:
Benchmark Results (Verified)
Based on actual performance measurements:
# Real benchmark results from parser_bench_post2_20250810_212948.json:
# 200-line files: ~1.5ms P99 latency
# 500-line files: ~3.0ms P99 latency
# 1000-line files: ~6ms P99 latency (estimated)
Performance Metrics
- P99 Latency: ~1.5ms for 200-line files, ~3ms for 500-line files
- Cold Start: <100ms import time
- Package Size: ~200KB installed (actual measured size)
- Memory Usage: <50MB peak for large files
- Dependencies: Zero external dependencies
Performance Comparison
Independent benchmarking shows Starlighter achieves:
- Sub-millisecond highlighting for typical code blocks (100-200 lines)
- Consistent performance with minimal variance across runs
- Zero memory leaks - stateless operation with automatic cleanup
🔒 Security
Starlighter prioritizes security with XSS-safe output:
# Potentially dangerous input
malicious_code = '''def hack():
return "</pre><script>alert('XSS')</script><pre>"'''
# Starlighter safely encodes everything
safe_html = highlight(malicious_code)
# Output: <span class="token-string">"</pre><script>alert('XSS')</script><pre>"</span>
📚 API Documentation
Core Functions
from starlighter import highlight, CodeBlock, StarlighterStyles, get_theme_css
# Basic highlighting
highlight(code: str, language: str = "python") -> str
# StarHTML/FastHTML component (requires framework)
CodeBlock(code: str, theme: str = "github-dark", **kwargs) -> Component
# Multiple theme styles (requires framework)
StarlighterStyles(*themes: str, auto_switch: bool = False, **kwargs) -> Style
# Get theme CSS
get_theme_css(theme_name: str) -> str
Error Handling
from starlighter import highlight, InputError, ParseError, RenderError
try:
html = highlight(user_code)
except InputError as e:
print(f"Invalid input: {e}")
except ParseError as e:
print(f"Parse error: {e}")
except RenderError as e:
print(f"Render error: {e}")
🎨 CSS Classes
Starlighter uses semantic CSS classes for flexible theming:
| Class | Usage |
|---|---|
.token-keyword |
Keywords: def, class, if, for |
.token-string |
String literals: "text", 'text', f"text" |
.token-comment |
Comments and docstrings |
.token-number |
Numeric literals: 42, 3.14, 0xFF |
.token-operator |
Operators: +, -, ==, != |
.token-identifier |
Variable and function names |
.token-builtin |
Built-in functions: print, len, str |
.token-punctuation |
Punctuation: (), [], {}, , |
🛠️ Development
Setup
git clone https://github.com/banditburai/starlighter.git
cd starlighter
# Using uv (recommended)
uv sync --all-extras
# Run tests
uv run pytest tests/ -v --cov=starlighter
# Run linting
uv run ruff check
uv run ruff format --check
Running Examples
# StarHTML example (primary)
cd examples/starhtml_example_project/
uv run python app.py
🔗 Links
- StarHTML: https://github.com/banditburai/starHTML
- PyPI: https://pypi.org/project/starlighter/
- GitHub: https://github.com/banditburai/starlighter
Built with ❤️ for the StarHTML community
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
Built Distribution
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 starlighter-0.1.3.tar.gz.
File metadata
- Download URL: starlighter-0.1.3.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f68db3357ddad6a845006bb21430e4bdf4d38b8af5a7ba0f017e605ed3b0d436
|
|
| MD5 |
1cd3743c74a0cf6e7f75878895621fa4
|
|
| BLAKE2b-256 |
b9c749ce761647ee1bc3c57f022e211080894175817d524aef4bd745c63c5eac
|
Provenance
The following attestation bundles were made for starlighter-0.1.3.tar.gz:
Publisher:
release.yml on banditburai/starlighter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
starlighter-0.1.3.tar.gz -
Subject digest:
f68db3357ddad6a845006bb21430e4bdf4d38b8af5a7ba0f017e605ed3b0d436 - Sigstore transparency entry: 418144805
- Sigstore integration time:
-
Permalink:
banditburai/starlighter@b30229e78f84ba7710963da846fe9676410fcb5c -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/banditburai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b30229e78f84ba7710963da846fe9676410fcb5c -
Trigger Event:
push
-
Statement type:
File details
Details for the file starlighter-0.1.3-py3-none-any.whl.
File metadata
- Download URL: starlighter-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68f7e2a0d3ded42b5086447575870ab5cd3cec414bc8f16453631e3484773ded
|
|
| MD5 |
3f55fabf79f4d481aefbd38a580a6fdc
|
|
| BLAKE2b-256 |
86c7049aacd8a63e53d60734f1cc4329f56d3377d634085a6a6c1e1c13c80477
|
Provenance
The following attestation bundles were made for starlighter-0.1.3-py3-none-any.whl:
Publisher:
release.yml on banditburai/starlighter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
starlighter-0.1.3-py3-none-any.whl -
Subject digest:
68f7e2a0d3ded42b5086447575870ab5cd3cec414bc8f16453631e3484773ded - Sigstore transparency entry: 418144865
- Sigstore integration time:
-
Permalink:
banditburai/starlighter@b30229e78f84ba7710963da846fe9676410fcb5c -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/banditburai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b30229e78f84ba7710963da846fe9676410fcb5c -
Trigger Event:
push
-
Statement type: