-
Notifications
You must be signed in to change notification settings - Fork 24
/
documentation_builder.py
178 lines (162 loc) · 6.8 KB
/
documentation_builder.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os,sys, argparse,shutil,re
from functools import reduce
#====================================
# Will build my documentation site based on notes and
# variables inside my scripts.
#====================================
def main():
parser = argparse.ArgumentParser(description='Supply path to root folder of scripts and will build docs webpages')
parser.add_argument("-p","--path",dest="path",help="Full path to root of scripts directory to scan.")
parser.add_argument("-m","--main",dest="main",help="The main page, full http path. EX: https://wesleywh.github.io/GameDevRepo/")
args = parser.parse_args()
if args.main == None:
args.main = "https://wesleywh.github.io/GameDevRepo/"
build_var_pages(args)
build_navigation_page(args)
def build_navigation_page(args):
cur_dir = os.path.dirname(os.path.realpath(__file__))
tree=""
navigation={}
for fullpath, dirs, files in os.walk(os.path.join(cur_dir,"docs")):
for filename in files:
parts=os.path.join(fullpath,filename).replace(cur_dir+"%sdocs%s"%(os.sep,os.sep),"").split(os.sep)
docs_path=os.path.join(fullpath,filename).replace(cur_dir+"%s"%os.sep,"").replace("docs%s"%os.sep,"",1)
if len(parts) < 2:
parts=["Miscellaneous",parts[0]]
if parts[0] not in navigation:
navigation[parts[0]]=[]
navigation[parts[0]].append("[%s](%s)"%(parts[-1].replace(".md",""),docs_path.replace(os.sep,"/")))
with open(os.path.join(cur_dir,"docs","navigation.md"),"w") as f:
f.write("[Back To Main Page](%s)\n\n"%args.main)
f.write("# Navigation Tree\n")
f.write("Find documentation relating to various script files found in this repository here.\n")
f.write("This is automatically generated based on comments within the script itself.\n")
f.write("If there is missing data it is because a comment has not been made in that file.\n\n")
cur_key=""
for key,value in navigation.items():
if cur_key != key:
cur_key=key
f.write("\n### %s\n"%key)
for item in value:
f.write(" + %s\n"%item)
def build_var_pages(args):
cur_dir = os.path.dirname(os.path.realpath(__file__))
for fullpath, dirs, files in os.walk(args.path, topdown=True):
for filename in files:
if (filename.endswith(".cs") and "InputManager" not in fullpath and
"PatchLib" not in fullpath and "Vehicles" not in fullpath):
with open(os.path.join(fullpath,filename)) as f:
contents = f.readlines()
description = extract_file_description(contents)
variables = extract_variables(contents)
sep=os.pathsep
final_path = os.path.join(fullpath,filename).replace(args.path,"").replace(".cs",".md",-1).lstrip("/").lstrip("\\\\")
script_title = filename.replace(".cs","",-1)
part1=os.path.join(cur_dir,"docs")
part2=final_path
save_path=os.path.join(part1,part2)
dir_path=os.path.dirname(save_path)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
with open(save_path,'w') as save_file:
save_file.write("[Back To Navigation Tree](%sdocs/navigation.html)\n"%args.main)
save_file.write("# %s"%script_title)
save_file.write("\n\n")
save_file.write("## Description:\n")
save_file.write(description)
save_file.write("\n\n")
save_file.write("## Variables:\n")
save_file.write("List of variables that you can modify in the inspector.\n\n")
save_file.write("|Access|Name|Type|Default Value|Description|\n")
save_file.write("|---|---|---|---|---|\n")
for var in variables:
save_file.write("|%s|%s|%s|%s|%s|\n"%(var["ACCESS"],var["NAME"],var["TYPE"],var["DEFAULT"],var["DESC"]))
#Variable Parsing
def extract_variables(contents):
variables=[]
regex="((public|private)?.+?[ ].+?([ ])?\(.+?\))"
for line in contents:
if ("SerializeField" in line or ("public" in line and "class" not in line) and
"void" not in line and re.match(regex,line) is None):
variables.append({
"ACCESS":get_access(line),
"TYPE":get_type(line),
"NAME":get_name(line),
"DEFAULT":get_default(line),
"DESC":get_description(line)})
return variables
def get_access(line):
line=line.strip()
retVal=""
if "SerializeField" in line:
line=line.split("SerializeField")[1].replace("]","").strip()
if "public" in line:
retVal="public"
elif "private" in line:
retVal="private"
else:
retVal="private"
return retVal
def get_description(line):
line=line.strip()
retVal=""
val = line.split("#")
if len(val) > 1:
retVal = val[1].strip().lstrip("#")
else:
retVal="No description."
return retVal
def get_type(line):
retVal=line.strip()
if "SerializeField" in retVal:
retVal=line.split("SerializeField")[1].replace("]","",1).strip()
if "public" in retVal:
retVal = retVal.split("public")[1].strip()
retVal = retVal.split(" ")[0].strip()
elif "private" in retVal:
retVal = retVal.split("private")[1].strip()
retVal = retVal.split(" ")[0].strip()
else:
retVal = line.split(" ")[0].strip()
return retVal
def get_name(line):
line=line.strip()
retVal=line.split(get_type(line))[1].strip().split("=")[0].strip()
retVal=retVal.split("#")[0].strip().replace(";","")
return retVal
def get_default(line):
retVal=""
if "#" in line:
retVal=line.split("#")[0].strip()
else:
retVal=line.strip()
if "=" in retVal:
retVal=retVal.split("=")[1].strip().split(";")[0].strip().split(" ")[-1].strip()
else:
retVal="no default"
return retVal
#File description parsing
def extract_file_description(contents):
inDescription=False
retVal=""
description=[]
for line in contents:
if inDescription == True:
if "</summary>" in line:
break
description.append(line.replace("///","",1).strip())
elif "using" in line and inDescription==False:
retVal="File has no description."
break
elif "summary" in line:
inDescription=True
if len(description) > 0:
content=""
for line in description:
if line == "":
content+="\n\n"
else:
content+=" "+line
retVal=content
return retVal
main()