generate_docs.py
3.06 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
import os
import re
def subfiles(path):
return [name for name in os.listdir(path) if os.path.isfile(os.path.join(path, name)) and not name[0] == '.']
def subdirs(path):
return [name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))]
def formatModule(module):
if module == 'io':
return 'i/o'
else:
return module.capitalize()
def parse(group):
docs = re.compile('/\*\!(.*?)\*/', re.DOTALL)
docsMatch = docs.match(group)
clss = group[docsMatch.end():].strip()
if len(clss) == 0 or 'class' not in clss:
return None
blocks = docsMatch.group().split('\\')[1:]
if len(blocks) == 0:
return None
attributes = {}
for block in blocks:
key = block[:block.find(' ')]
value = block[block.find(' '):].split('\n')[0].strip()
if key in attributes:
attributes[key].append(value)
else:
attributes[key] = [value]
attributes['Name'] = clss[5:clss.find(':')].strip()
attributes['Parent'] = clss[clss.find('public')+6:].strip()
return attributes
def parseProperties(properties):
output = ""
for prop in properties:
split = prop.find(' ')
name = prop[:split]
desc = prop[split:]
output += "\t* **" + name + "**- " + desc + "\n"
return output
def main():
plugins_dir = '../../openbr/plugins/'
output_dir = '../docs/docs/plugins/'
for module in subdirs(plugins_dir):
if module == "cmake":
continue
output_file = open(os.path.join(output_dir, module + '.md'), 'w+')
for plugin in subfiles(os.path.join(plugins_dir, module)):
f = open(os.path.join(os.path.join(plugins_dir, module), plugin), 'r')
content = f.read()
regex = re.compile('/\*\!(.*?)\*/\n(.*?)\n', re.DOTALL)
it = regex.finditer(content)
for match in it:
attributes = parse(match.group())
if not attributes:
continue
output_file.write("---\n\n")
output_file.write("# " + attributes["Name"] + "\n\n")
output_file.write(attributes["brief"][0] + "\n\n")
output_file.write("* **file:** " + os.path.join(module, plugin) + "\n")
output_file.write("* **inherits:** [" + attributes["Parent"] + "](../cpp_api.md#" + attributes["Parent"].lower() + ")\n")
authors = attributes["author"]
if len(authors) > 1:
output_file.write("* **authors:** " + ", ".join(attributes["author"]) + "\n")
else:
output_file.write("* **author:** " + attributes["author"][0] + "\n")
if not 'property' in attributes.keys():
output_file.write("* **properties:** None\n")
else:
properties = attributes['property']
output_file.write("* **properties:**\n\n")
output_file.write(parseProperties(properties) + "\n")
output_file.write("\n")
main()