Damson/tools/vendor_syntax.py
astar76 11c1a7808b Build the syntax definitions into the app
Highlighting relied on the syntax-highlighting package, so removing it
left every file uncoloured. The 462 definitions now live in assets/syntax
and are compressed into the binary at build time, unpacked per language
when used. User .xml files still add to or replace them, and the licence
each file declares is recorded in assets/syntax/LICENSES.md.

Also moves files to the trash without needing gio.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 20:35:27 -06:00

30 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Copy Kate's syntax definitions into assets/syntax and record each file's licence.
python tools/vendor_syntax.py <folder of KSyntaxHighlighting .xml files> <its version>
"""
import html, os, re, shutil, sys
src, version = sys.argv[1], sys.argv[2]
dst = "assets/syntax"
shutil.rmtree(dst, ignore_errors=True)
os.makedirs(dst)
rows = []
for f in sorted(os.listdir(src)):
if not f.endswith(".xml"):
continue
shutil.copy(os.path.join(src, f), dst)
head = open(os.path.join(src, f), "rb").read(8192).decode("utf8", "replace")
tag = head[head.find("<language"):]
attr = lambda k: html.unescape(next(iter(re.findall(r"""\s%s=(?:"([^"]*)"|'([^']*)')""" % k, tag)[:1]), ("", ""))[0] or "")
rows.append((f, attr("name"), attr("license") or "not stated", attr("author")))
with open(f"{dst}/LICENSES.md", "w") as o:
o.write("# Syntax definitions\n\n")
o.write(f"These files come unmodified from KDE's KSyntaxHighlighting {version}, and each keeps the licence\n")
o.write("and authors stated at the top of the file. This table lists what each one declares.\n")
o.write("Where none is stated, KSyntaxHighlighting as a whole is released under the MIT licence.\n\n")
o.write("Source: https://invent.kde.org/frameworks/syntax-highlighting\n\n")
o.write("| File | Language | Licence | Author |\n|---|---|---|---|\n")
for r in rows:
o.write("| " + " | ".join(x.replace("|", "/") for x in r) + " |\n")
print(len(rows), "definitions")