-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtxt2lintel.py
71 lines (57 loc) · 1.97 KB
/
txt2lintel.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# txt2lintel.py - IDS.TXT to Lintel groups.
# (c) 2025 DWNfonts.
import liblintel, io
def txt2lintelGroup(txt: str | io.TextIOWrapper, lookup: list | str = []):
import sys, re
def _lookup(value: int):
try:
return lookup[value]
except:
return "?"
result = {}
for line in txt:
if line.startswith("#"):
print(f"Comment line: `{line.rstrip()}'", file=sys.stderr)
continue
else:
try:
lineText = line.strip()
unencoded = re.findall("\\{(.*?)\\}", lineText)
if len(unencoded) > 0:
for i in [int(x) - 1 for x in unencoded]:
data = _lookup(i)
index = "{%d}" % i
lineText = lineText.replace(f"{{{i+1}}}", data)
line = lineText.split("\t")
zi = line[1]
ids = line[2:]
for i in range(len(ids)):
if ids[i].startswith("*"): # Inline comment
print(
f"Inline comment: `{"\t".join(ids[i:])}'",
file=sys.stderr,
)
ids = ids[1:i]
break
lintelList = [liblintel.importIDSWithSource(i) for i in ids]
result[zi] = lintelList
except:
print(
f"Invaild data: `{line}', skip",
file=sys.stderr,
)
return result
def lintelGroup2json(lintelGroup: dict):
import json
result = {}
for zi in lintelGroup:
result[zi] = [str(i) for i in lintelGroup[zi]]
return json.dumps(result, sort_keys=True, indent=4, ensure_ascii=False)
def _main():
import sys
txt = sys.stdin.read()
result = txt2lintelGroup(txt)
print(lintelGroup2json(result))
if __name__ == "__main__":
_main()