forked from 7Cav/cScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_wiki_articles.py
More file actions
268 lines (217 loc) · 9.54 KB
/
generate_wiki_articles.py
File metadata and controls
268 lines (217 loc) · 9.54 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
import sys, os, fnmatch, fileinput
import argparse, shutil, subprocess, tempfile, json
import configparser
__version__ = 1.0
# set projecty path
scriptPath = os.path.realpath(__file__)
scriptDir = os.path.dirname(scriptPath)
rootDir = os.path.dirname(os.path.dirname(scriptPath))
outputDir = '{}/doc'.format(rootDir)
function_dir = '{}/cScripts/CavFnc/functions'.format(rootDir)
def color_string(string='', color='\033[0m', auto_color=False):
if auto_color:
return '\033[0m{}{}\033[0m'.format(color,string)
else:
return string
def fetch_objects(path=''):
os.chdir(path)
content = os.listdir(path)
objectList = []
folderList = [] # Collect directories
fileList = [] # Collect files
for obj in content:
if os.path.isfile(obj):
fileList.append(obj)
elif os.path.isdir(obj):
folderList.append(obj)
else:
sys.exit('\nIssues occured when listing files.')
objectList.append(folderList)
objectList.append(fileList)
os.chdir(scriptDir)
return objectList
def get_article_link(function=''):
expanded_articles = {
'fn_moduleApplyVehicleInventory': '7Cav-Modules',
'fn_moduleApplyVehicleLable': '7Cav-Modules',
'fn_moduleCallEndex': '7Cav-Modules',
'fn_moduleCreateFieldHospital': '7Cav-Modules',
'fn_moduleCreateMedicalCrate': '7Cav-Modules',
'fn_moduleCreateSpecialWeaponsCrate': '7Cav-Modules',
'fn_moduleCreateStarterCrate': '7Cav-Modules',
'fn_moduleCreateSupplyCrate': '7Cav-Modules',
'fn_moduleMakeDoctor': '7Cav-Modules',
'fn_moduleMakeEngineer': '7Cav-Modules',
'fn_moduleRegearTrooper': '7Cav-Modules',
'fn_doStarterCrate': 'Starter-Crate',
'fn_doStarterCrateSupplies': 'Starter-Crate',
'fn_teleport': 'Teleport',
'fn_gate': 'Gate',
'fn_setVehicleLable': 'Texture-Label',
'fn_createVehicleLable': 'Texture-Label',
'fn_getVehicleLable': 'Texture-Label',
'fn_doFieldHospital': 'Field-Hospital'
}
article_link = ''
for ekey in expanded_articles:
if ekey == function:
article_link = "https://github.com/7Cav/cScripts/wiki/{}".format(expanded_articles[ekey])
continue
return article_link
def get_author_link(author=''):
names = {
#CavName, GithubUser
'Brostrom.A': 'ColdEvul',
'Dunn.W': 'VinoEtCaseus',
'Citarelli.D': 'davidcit646'
}
for akey in names:
author = author.replace(akey,'[{}](https://github.com/{})'.format(akey,names[akey]))
continue
return author
def write_article(collection={}):
if not os.path.isdir(outputDir):
os.mkdir(outputDir)
article = open('{}/Function-list.md'.format(outputDir),"w+")
# Write index
article.write('## Index\n')
for category in collection:
article.write('\n#### {}\n'.format(category.capitalize()))
for function in collection[category]:
for function_key, call in collection[category][function].items():
if not function_key == 'call':
continue
article.write('- [[{}|Function-list#{}]]\n'.format(call, function))
# Write function articles
article.write('\n# Functions\n')
for category in collection:
article.write('\n## {}\n'.format(category.capitalize()))
for function in collection[category]:
try:
vAuthor = 'Written by: {}'.format(collection[category][function]['author'])
vAuthor = get_author_link(vAuthor)
except KeyError:
vAuthor = ''
vDesc = collection[category][function]['description']
vLink = collection[category][function]['link']
vCall = collection[category][function]['call']
vExAr = collection[category][function]['expandedArticle']
article.write('### {}\n'.format(function))
article.write('[Go to {}]({}), {}\n\n'.format(vCall, vLink, vAuthor))
article.write('{}\n\n'.format(vDesc))
if not vExAr == '':
article.write('You can read more about this function [here]({}).\n\n'.format(vExAr))
article.write('**Arguments:**\n\n')
for exsample in collection[category][function]['arguments']:
article.write('{}\n\n'.format(exsample))
article.write('\n')
article.write('**Exsamples:**\n\n')
for exsample in collection[category][function]['exsamples']:
article.write('```cpp\n{}\n```\n'.format(exsample))
article.write('\n')
def main():
os.chdir(rootDir)
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--json', action='store_true',
help='sum the integers (default: find the max)')
args = parser.parse_args()
#print('Featching all functions...')
function_dict = {}
function_categories = fetch_objects(function_dir)
function_categories = function_categories[0]
for function_category in function_categories:
functions = fetch_objects('{}/{}'.format(function_dir, function_category))
functions = functions[1]
function_dict.update({function_category : {}})
for function in functions:
function_file = function
function_name = function_file.replace('.sqf', '')
function_func = function_name.replace('fn_', 'cScripts_fnc_')
function_link = 'https://github.com/7Cav/cScripts/blob/master/cScripts/CavFnc/functions/{}/{}'.format(function_category,function_file)
function_path = './cScripts/CavFnc/functions/{}/{}'.format(function_category,function_file)
function_dict[function_category].update({function_name : {}})
function_dict[function_category][function_name].update({
'call': function_func,
'file': function_file,
'link': function_link,
'path': function_path,
'arguments': [],
'description': "",
'author': "",
'exsamples': [],
'expandedArticle': ""
})
file = open('{}/{}/{}'.format(function_dir, function_category, function_file))
script_header = []
for i, line in enumerate(file):
if ' */' in line:
break
if '/*' in line:
continue
if ' *' in line:
line = line.replace(' * ', '')
line = line.replace(' *', '')
line = line.replace('\n', '')
script_header.append(line)
description = []
arguments = []
return_value = []
exsamples = []
block = [
False, # Description
False, # Arguments
False, # Return Value
False # Exsample
]
for i, line in enumerate(script_header):
if 'Author:' in line:
author = line.replace('Author: ', '')
function_dict[function_category][function_name].update({'author': author})
block = [False,False,False,False]
continue
if 'Public:' in line:
author = line.replace('Public: ', '')
function_dict[function_category][function_name].update({'public': author})
block = [False,False,False,False]
continue
if 'Arguments:' in line:
block = [False,True,False,False]
if 'Return Value:' in line:
block = [False,False,True,False]
if 'Example:' in line or 'Examples:' in line:
block = [False,False,False,True]
if block[0]:
description.append(line)
continue
if block[1]:
arguments.append(line)
continue
if block[2]:
return_value.append(line)
continue
if block[3]:
exsamples.append(line)
continue
if not line == "":
description.append(line)
if int(len(arguments)) >= 1:
arguments.remove(arguments[0])
arguments = [x for x in arguments if x]
if int(len(return_value)) >= 1:
return_value.remove(return_value[0])
return_value = [x for x in return_value if x]
if int(len(exsamples)) >= 1:
exsamples.remove(exsamples[0])
exsamples = [x for x in exsamples if x]
description_text = '\n'.join(description)
description_text = description_text.lstrip()
function_dict[function_category][function_name].update({'description': description_text})
function_dict[function_category][function_name].update({'expandedArticle': get_article_link(function_name)})
function_dict[function_category][function_name].update({'arguments': arguments})
function_dict[function_category][function_name].update({'exsamples': exsamples})
if args.json:
sys.exit(print(json.dumps(function_dict)))
write_article(function_dict)
if __name__ == "__main__":
sys.exit(main())