-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtoml2md.py
66 lines (52 loc) · 2.13 KB
/
toml2md.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
#!/usr/bin/env python3
import tomllib
# python toml2md.py
def convert(ja):
filename = "README.md"
if ja:
filename = "README-ja.md"
o = open(filename, "w", encoding="utf-8", newline="\n")
with open("awesome-nostr-japan.toml", "rb") as toml_in:
toml = tomllib.load(toml_in)
for section_key in toml:
section = toml[section_key]
if section["caption"] == "awesome-nostr-japan":
o.write("# " + section["caption"]+"\n\n")
else:
o.write("## " + section["caption"]+"\n\n")
if ja:
if "description_ja" in section and \
section["description_ja"] != "":
o.write(section["description_ja"]+"\n")
elif "description" in section:
o.write(section["description"]+"\n")
else:
if "description" in section:
o.write(section["description"]+"\n")
for item_key in section:
item = section[item_key]
if not isinstance(item, dict):
continue
line = "* "
if item["name"] == "":
line += "`" + item["address"] + "`"
else:
line += "[" + item["name"] + "]"
line += "(" + item["address"] + ")"
if ja:
if item["description_ja"] != "":
line += " - " + item["description_ja"]
elif item["description"] != "":
line += " - " + item["description"]
else:
if item["description"] != "":
line += " - " + item["description"]
if len(item["author_name"]) > 0:
for i in range(len(item["author_name"])):
if item["author_name"][i] != "":
line += " by [" + item["author_name"][i] + \
"]("+item["author_url"][i]+")"
o.write(line+"\n")
o.write("\n")
convert(False)
convert(True)