Skip to content

Commit a5dc086

Browse files
authored
Add argparse to pygemnts2chroma_xml.py script (#1140)
I also adapted the README to use `uv` since this removes an install step (uv resolves and installs dependencies on `uv run`). We could also debate if we should switch from `KotlinLexer` as an example to some other lexer as there seems to be a bug upstream in pygments where `KotlinLexer.get_tokendefs()` returns a string that is not valid unicode. You can read more about the bug in [this issue](pygments/pygments#2964). With argparse the error messages are a lot nicer and the script can better explain itself. ```bash uv run pygments2chroma_xml.py --help usage: pygments2chroma_xml.py [-h] lexer_class Converts pygments RegexLexer classes to chroma xml grammar definitions. positional arguments: lexer_class The class name of the pygments lexer, like: 'pygments.lexers.jvm.KotlinLexer'. options: -h, --help show this help message and exit ```
1 parent 3f991b1 commit a5dc086

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ using the included Python 3 script `pygments2chroma_xml.py`. I use something lik
209209
the following:
210210

211211
```sh
212-
python3 _tools/pygments2chroma_xml.py \
212+
uv run --script _tools/pygments2chroma_xml.py \
213213
pygments.lexers.jvm.KotlinLexer \
214-
> lexers/embedded/kotlin.xml
214+
> lexers/embedded/kotlin.xml
215215
```
216216

217217
A list of all lexers available in Pygments can be found in [pygments-lexers.txt](https://github.com/alecthomas/chroma/blob/master/pygments-lexers.txt).

_tools/pygments2chroma_xml.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
#!/usr/bin/env -S uv run --script
2+
# /// script
3+
# requires-python = ">=3.13"
4+
# dependencies = [
5+
# "pygments",
6+
# "pystache",
7+
# ]
8+
# ///
9+
import argparse
210
import functools
311
import importlib
412
import re
@@ -168,7 +176,11 @@ def re_ignorecase(self):
168176

169177

170178
def main():
171-
package_name, symbol_name = sys.argv[1].rsplit(sep=".", maxsplit=1)
179+
parser = argparse.ArgumentParser( prog='pygments2chroma_xml.py', description='Converts pygments RegexLexer classes to chroma xml grammar definitions.')
180+
parser.add_argument('lexer_class', type=str, help="The class name of the pygments lexer, like: 'pygments.lexers.jvm.KotlinLexer'.")
181+
args = parser.parse_args()
182+
183+
package_name, symbol_name = args.lexer_class.rsplit(sep=".", maxsplit=1)
172184

173185
package = importlib.import_module(package_name)
174186

_tools/style.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
#!/usr/bin/env -S uv run --script
2+
# /// script
3+
# requires-python = ">=3.13"
4+
# dependencies = [
5+
# "pygments",
6+
# "pystache",
7+
# ]
8+
# ///
29
import importlib
310
import sys
411

0 commit comments

Comments
 (0)