-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd_docs2gds
129 lines (96 loc) · 2.98 KB
/
md_docs2gds
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python
# This is a python script that will take markdown docs and convert them to bbcode and put them in a gdscript scripts.
import markdown2
from gdscript_docs_tools import *
def scan_for_md_files():
import os
md_files = []
for root, dirs, files in os.walk("."):
for file in files:
if file.endswith(".md"):
md_files.append(os.path.join(root, file))
return md_files
# we need to split the md file into sections of vars, consts, signals, and funcs
def parse_md_file(md_file):
with open(md_file, "r") as f:
text = f.readlines()
section = ""
item = ""
gdscript_path = ""
gdscript_tree = {}
for l in text:
parse_md_line(l, gdscript_tree,
gdscript_path, section, item)
color_message(bcolors.OKGREEN,
f"Finished parsing {md_file}")
# we need to save the gdscript_tree to the gdscript file
color_message(bcolors.OKBLUE,
f"Saving to {gdscript_path}")
with open(md_file.removesuffix(".md") + ".gd", "w") as f:
for section in gdscript_tree:
for item in gdscript_tree[section]:
for line in gdscript_tree[section][item]:
f.write(line)
color_message(bcolors.OKGREEN,
f"Finished saving to {gdscript_path}")
return doc_tree
def parse_md_line(l, gdscript_tree, gdscript_path, section, item):
if l.startswith("# "):
imp = md_class_name(l)
gdscript_tree = imp[0]
gdscript_path = imp[1]
elif l.startswith("## "):
section = md_section(l)
elif l.startswith("### "):
item = md_item(l)
elif item in gdscript_tree[section]:
gdscript_tree[section][item] += l
if section == "unknown":
color_message(bcolors.WARNING,
f"Line of unknown section: {l}")
def get_md_item(l, times = 1):
return l.split(" ")[times].removesuffix("\n")
def md_class_name(l):
# we get the class name
class_name = get_md_item(l, 1)
color_message(bcolors.OKGREEN,
f"Found class name: {class_name}")
# we need to get path to gdscript file to add lines later
# so we ask user for the path
mess = f"Enter the path to the gdscript file for {class_name}: "
gdscript_path = input(mess)
# we also need to return gdscript_tree using check_gdsdoc()
return (check_gdsdoc(gdscript_path), gdscript_path)
def md_section(l):
section = ""
match l:
case "## Variables":
section = "vars"
case "## Constants":
section = "consts"
case "## Signals":
section = "signals"
case "## Functions":
section = "funcs"
case _:
section = "unknown"
if section != "unknown":
color_message(bcolors.OKGREEN,
f"Found section: {section} type")
else:
unk = l.split(' ')[1].removesuffix('\n')
color_message(bcolors.WARNING,
f"Unknown type section: {unk}")
return section
def md_item(l, section):
# we are in a new item
item = get_md_item(doc_tree["vars"], l, 2)
if doc_tree["section"] != "unknown":
item = l.split(' ')[2].removesuffix('\n')
color_message(bcolors.OKGREEN,
f"Found item: {item} in section: {doc_tree['section']}")
else:
unk = l.split(' ')[2].removesuffix('\n')
color_message(bcolors.WARNING,
f"Unknown type item: {unk}")
return item