-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathmake-index.py
executable file
·50 lines (41 loc) · 1.22 KB
/
make-index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# This makes the file kvg-index.json which is used by the online
# KanjiVG viewer at https://kanjivg.tagaini.net/viewer.html. This
# script was contributed by
# https://github.com/KanjiVG/kanjivg/pull/430, but copy-pasted into
# the repository, see that pull request for details of why. This does
# not use the other kanjivg libraries in this directory.
if __name__ == "__main__":
import glob
import json
import logging
import os
import re
from collections import defaultdict
from pathlib import Path
def file_to_kanji(file):
matched = re.match(r"^([0-9a-f]{5})(-.*)?\.svg$", file)
if matched is None:
return (None, None)
return (chr(int(matched[1], 16)), matched[2])
current_dir = Path(__file__).parent
# The index we write to the file.
index = defaultdict(list[str])
files = sorted(
map(os.path.basename, glob.glob(str(current_dir / "kanji" / "*.svg")))
)
for file in files:
(kanji, _ex) = file_to_kanji(file)
if kanji is None:
logging.warning(f"Could not get kanji from {file}")
continue
index[kanji].append(file)
with open(current_dir / "kvg-index.json", "w") as f:
json.dump(
index,
f,
ensure_ascii=False,
indent="\t",
separators=(",", ":"),
)
f.write("\n")