...
1#!/usr/bin/env python3
2import re
3from collections import defaultdict
4from subprocess import check_output
5
6README_FILE = "README.md"
7
8
9lines = check_output(["go", "run", "./cmd/chroma/main.go", "--list"]).decode("utf-8").splitlines()
10lines = [line.strip() for line in lines if line.startswith(" ") and not line.startswith(" ")]
11lines = sorted(lines, key=lambda l: l.lower())
12
13table = defaultdict(list)
14
15for line in lines:
16 table[line[0].upper()].append(line)
17
18rows = []
19for key, value in table.items():
20 rows.append("{} | {}".format(key, ", ".join(value)))
21tbody = "\n".join(rows)
22
23with open(README_FILE, "r") as f:
24 content = f.read()
25
26with open(README_FILE, "w") as f:
27 marker = re.compile(r"(?P<start>:----: \\| --------\n).*?(?P<end>\n\n)", re.DOTALL)
28 replacement = r"\g<start>%s\g<end>" % tbody
29 updated_content = marker.sub(replacement, content)
30 f.write(updated_content)
31
32print(tbody)
View as plain text