A Pygments lexer for SuperCollider
Project description
SuperCollider Pygments lexer plugin
This is a Pygments lexer with custom styles for SuperCollider code. It provides syntax highlighting for SuperCollider files in Pygments-supported environments like mkdocs, sphinx, LaTeX's minted package, and more.
Pygments has a built-in lexer for SuperCollider, but the existing implementation has many issues such as not recognizing class names, incorrectly highlighting words which are not keywords in SuperCollider, etc. This plugin is a full reimplementation with much improved lexing, which means more accurate syntax highlighting of SuperCollider code. It has been submitted to the Pygments project and is awaiting review, but in the meantime, you can use it with this plugin.
Features
- Accurate lexer: Recognizes SuperCollider class names, keywords, symbols, and general syntax
- Custom color themes: Includes both light and dark themes designed specifically for SuperCollider code
- Easy integration: Works with Pygments-based tools such as mkdocs, Sphinx, and LaTeX minted
Important: Language/lexer name
Due to a naming conflict with Pygments' built-in SuperCollider lexer, you must use sc-plugin as the language/lexer name wherever you want to use this plugin.
Do not use sc or supercollider, as those will use the built-in lexer.
Installation
Install the plugin using pip:
pip install pygments-supercollider-lexer
Usage
1. Python
In vanilla Python scripts, you can use the lexer like this:
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
code = '''
SynthDef(\sine, {
var sig = SinOsc.ar(\freq.kr(440), 0, \amp.kr(0.1));
Out.ar(0, sig ! 2);
}).add;
'''
lexer = get_lexer_by_name('sc-plugin')
formatter = HtmlFormatter(style='supercollider_dark') # or 'supercollider_light'
result = highlight(code, lexer, formatter)
with open("output.html", "w") as f:
f.write(result)
Or on the command line:
pygmentize -l sc-plugin -O style=supercollider_dark -f html myfile.scd > output.html
2. Markdown/mkdocs
To use this lexer in markdown files with mkdocs, you need to install the PyMdown Extensions and configure your mkdocs.yml file to include the pymdownx.highlight extension as well as the pymdownx.superfences extension.
markdown_extensions:
- pymdownx.highlight
- pymdownx.superfences
Then, specify sc-plugin as the language name at the top of your code block:
```sc-plugin
{ SinOsc.ar(440) * 0.1 }.play;
```
Using the supplied styles with mkdocs
To use the custom SuperCollider color themes that come with this plugin in a site built with mkdocs, the best solution is to generate CSS rules for the chosen style and include it in your project. Pygments makes it easy to generate CSS files for the styles with the pygmentize command:
# For dark theme
pygmentize -S supercollider_dark -f html -a .highlight > css/supercollider_dark.css
# For light theme
pygmentize -S supercollider_light -f html -a .highlight > css/supercollider_light.css
Then, include the generated CSS files in your mkdocs.yml:
extra_css:
- css/supercollider_dark.css
If you want to support both light and dark modes, you can include both CSS files and wrap their contents in corresponding prefers-color-scheme media query.
If using the Material for MkDocs theme to switch between light and dark modes, you can use [data-md-color-scheme="default"] as a selector for the light theme and [data-md-color-scheme="slate"] for the dark theme.
3. LaTeX minted
To use the lexer with the minted package in LaTeX:
\usepackage{minted}
...
\begin{minted}{sc-plugin}
// SuperCollider code here
(
SynthDef(\sine, {
Out.ar(0, SinOsc.ar(440))
}).add;
)
\end{minted}
Troubleshooting
- If you see incorrect highlighting, double-check that you are using
sc-pluginas the language/lexer name. - If you get "no lexer for alias" errors, ensure the plugin is installed in the same Python environment as your tool (MkDocs, Sphinx, etc).
- If you update the plugin, reinstall it with
pip install -e .to refresh the entry points.
Development
To contribute or modify the lexer, (fork and) clone the repository and install it in editable mode:
git clone https://github.com/aeskildsen/pygments-supercollider-lexer.git
cd pygments-supercollider-lexer
pip install -e .[dev]
Run tests with:
pytest
License
This plugin is licensed under the MIT License. See the LICENSE file for details.
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_supercollider_lexer-0.1.0.tar.gz.
File metadata
- Download URL: pygments_supercollider_lexer-0.1.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b805c3bd9716c0e5f17d2ce5ca89de464b8f83bc72af3c689c5bfb3981466fe
|
|
| MD5 |
0af6bc2df34808e826031c0ec3db858d
|
|
| BLAKE2b-256 |
2abceacaf218c16122618e08075ec646ce48d917fc914daaf7bcfdc892877670
|
File details
Details for the file pygments_supercollider_lexer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pygments_supercollider_lexer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7617e79e39a421fb39659c7b9bdb9ec7f53d601f8b925aa32541b3fac8a27124
|
|
| MD5 |
4b2cdab9e550273c97601334c520e923
|
|
| BLAKE2b-256 |
d0f897f2b453c41da4f6c1f6997e19431714dc6925cd32b7d6fca2a989dd8926
|