A Pygments plugin providing syntax highlighting for the Splunk Search Processing Language (SPL)
Project description
pygments-lexer-spl
A Pygments lexer plugin for the Splunk Search Processing Language (SPL). Pygments is the default syntax highlighter for Sphinx (and therefore Read the Docs). This package adds Splunk SPL support to Pygments.
Installation
Install the package directly:
pip install pygments-lexer-spl
Or add it to your project's dependencies:
# pyproject.toml
[project]
dependencies = [
"pygments-lexer-spl",
]
Usage
Once installed, the lexer is automatically available to Pygments via the plugin entry point. You can use it with any Pygments-compatible tool.
Command line (pygmentize)
pygmentize -l spl example.spl
pygmentize -l splunk example.spl
Python API
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments_lexer_spl import SPLLexer
code = open('example.spl').read()
print(highlight(code, SPLLexer(), TerminalFormatter()))
Terminal preview
python preview.py
DEBUG=1 python preview.py # Print each token and its type
Visual preview server
pip install 'pygments-lexer-spl[server]'
python server.py
# Then open http://localhost:8080
MkDocs
Install pygments-lexer-spl alongside MkDocs. The lexer is picked up
automatically via the Pygments plugin entry point, so no extra configuration is
required. Use the spl language identifier in fenced code blocks:
```spl
index=main sourcetype=access_combined
| stats count BY host
```
Sphinx
Install pygments-lexer-spl in the same environment as Sphinx. The lexer
registers itself automatically, so it is available in code-block directives
without any additional configuration:
.. code-block:: spl
index=main sourcetype=access_combined
| stats count BY host
Flask
Use Pygments directly to render highlighted HTML and serve it from a Flask route:
from flask import Flask
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments_lexer_spl import SPLLexer
app = Flask(__name__)
formatter = HtmlFormatter()
@app.route("/highlight")
def highlight_code():
code = open("example.spl").read()
highlighted = highlight(code, SPLLexer(), formatter)
css = f"<style>{formatter.get_style_defs('.highlight')}</style>"
return css + highlighted
Django
Add Pygments to your Django project and call it from a view or template tag:
# views.py
from django.utils.safestring import mark_safe
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments_lexer_spl import SPLLexer
formatter = HtmlFormatter()
def highlight_code(request):
from django.shortcuts import render
code = open("example.spl").read()
highlighted = highlight(code, SPLLexer(), formatter)
css = formatter.get_style_defs(".highlight")
return render(request, "highlight.html", {
"css": mark_safe(css),
"code": mark_safe(highlighted),
})
{# highlight.html #}
<style>{{ css }}</style>
{{ code }}
Colors
The lexer tells Pygments how to identify tokens. Pygments wraps each token in a
span tag with a class related to that token type. If you want to change how
the tokens are highlighted, change themes or add custom CSS.
Supported aliases
| Alias | Description |
|---|---|
spl |
Primary alias |
splunk |
Alternative alias |
splunk-spl |
Fully qualified alias |
Development
Install dependencies:
pip install -e ".[dev,server]"
Run the test suite:
pytest
Start the visual preview server (available at http://localhost:8080):
python server.py
Run the terminal preview script:
python preview.py
Enable debug mode to print each token and its value:
DEBUG=1 python preview.py
License
MIT
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 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 pygments_lexer_spl-0.1.0.tar.gz.
File metadata
- Download URL: pygments_lexer_spl-0.1.0.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cda7942069c8dcc57c791c02e081f828088762dcbb270e3160ceac4a2c214a6
|
|
| MD5 |
dbbbf853b60e647cab315d5c77f15382
|
|
| BLAKE2b-256 |
e6a7ec4a8612ed38f3394b7bb60ec75d4b55d7f16b77af6cfc5bd4167d4c9986
|
File details
Details for the file pygments_lexer_spl-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pygments_lexer_spl-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca93e1def59ca5eb9c1e599d94528156e971234750956a5f5ca0cf4fc5f12d2f
|
|
| MD5 |
b10262738e88c1197913509fb2d5aaf4
|
|
| BLAKE2b-256 |
a0b29fb5892f0215b7ef5b414032de9e8606b17576a057d9633c22198f4b4917
|