generate_plugin_docs.py
8.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
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
import os, sys
import re
abstractions = ['FileList', 'File',
'TemplateList', 'Template',
'UntrainableTransform',
'MetaTransform', 'UntrainableMetaTransform',
'MetadataTransform', 'UntrainableMetadataTransform',
'TimeVaryingTransform',
'CompositeTransform', 'WrapperTransform',
'Transform',
'UntrainableDistance', 'Distance',
'MatrixOutput', 'Output',
'Format',
'FileGallery', 'Gallery',
'Representation',
'Classifier'
]
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
clean_docs = docsMatch.group()[3:-2].replace('*', ' ').strip()
blocks = clean_docs.split('\\')[1:]
if len(blocks) == 0:
return None
attributes = {}
for block in blocks:
key = block[:block.find(' ')]
value = block[block.find(' '):].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().strip(',') # Handles the edge case of multiple inheritence
return attributes
def parseBrief(briefs):
if not briefs:
print 'All plugins need a description!'
sys.exit(1)
output = ""
for brief in briefs:
for abstraction in abstractions:
regex = re.compile('\s' + abstraction + "'?s?")
matches = regex.finditer(brief)
for match in matches:
name = ' [' + abstraction + ']'
link = '(../cpp_api/' + abstraction.lower() + '/' + abstraction.lower() + '.md)'
brief = brief.replace(match.group(), name + link).strip() # strip removes a possible leading whitespace
for line in brief.split('\n'):
if not line.strip():
output += "\n"
else:
output += line.strip() + "\n"
output += ' '
return output + '\n\n'
def parseInheritance(inheritance):
if inheritance in abstractions:
return '../cpp_api/' + inheritance.lower() + '/' + inheritance.lower() + '.md'
else: # Not an abstraction must inherit in the local file!
return '#' + inheritance.lower()
def parseLinks(links):
if not links:
return ""
output = "* **see:**"
if len(links) > 1:
output += "\n\n"
for link in links:
output += "\t* [" + link + "](" + link + ")\n"
output += "\n"
else:
output += " [" + links[0] + "](" + links[0] + ")\n"
return output
def parsePapers(papers):
if not papers:
return ""
output = "* **read:**\n\n"
for i in range(len(papers)):
info = papers[i].split('\n')
authors = info[0].strip()
title = None
other = None
if len(info) >= 2:
title = info[1].strip()
if len(info) >= 3:
other = info[2].strip()
output += "\t" + str(i+1) + ". *" + authors + "*\n"
if title:
output += "\t **" + title + "**\n"
if other:
output += "\t " + other + "\n"
output += "\n"
return output
def parseAuthors(authors, citations):
if not authors:
print 'All plugins need an author!'
sys.exit(1)
if len(authors) != len(citations):
print 'Different number of authors and citations!'
print authors, citations
return "* **authors:** PARSING ERROR\n"
output = "* **author(s):** "
for i in range(len(authors)):
output += "[" + authors[i] + "][" + citations[i] + "], "
output = output[:-2] + "\n"
return output
def parseProperties(properties):
if not properties:
return "* **properties:** None\n\n"
output = "* **properties:**\n\n"
output += "\tProperty | Type | Description\n"
output += "\t--- | --- | ---\n"
for prop in properties:
split = prop.split(' ')
ty = split[0]
name = split[1]
desc = ' '.join(split[2:])
table_regex = re.compile('\[(.*?)\]')
table_match = table_regex.search(desc)
while table_match:
before = desc[:table_match.start()]
after = desc[table_match.end():]
table_content = desc[table_match.start()+1:table_match.end()-1].split(',')
table = "<ul>"
for field in table_content:
table += "<li>" + field.strip() + "</li>"
table += "</ul>"
desc = before.strip() + table + after.strip()
table_match = table_regex.search(desc)
output += "\t" + name + " | " + ty + " | " + desc + "\n"
return output
def parseFormats(formats):
if not formats:
return ""
output = "* **format:** "
for f in formats:
in_raw = False
for line in f.split('\n'):
if not line.strip():
output += "<pre><code>" if not in_raw else "</code></pre>"
in_raw = not in_raw
continue
clean_line = line.strip()
# <> are often used describing formats. Unfortunately they are also html tags.
# So we need to replace them with safe alternatives
if '<' in clean_line:
clean_line = clean_line.replace('<', '<')
if '>' in clean_line:
clean_line = clean_line.replace('>', '>')
output += clean_line
if in_raw:
output += "\n"
else:
output += ' '
if in_raw:
output += "</code></pre>"
return output + "\n"
def main():
plugins_dir = '../../openbr/plugins/'
output_dir = '../docs/api_docs/plugins/'
for module in subdirs(plugins_dir):
if module == "cmake":
continue
output_file = open(os.path.join(output_dir, module + '.md'), 'w+')
names = []
docs = {} # Store the strings here first so they can be alphabetized
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 or (attributes and attributes["Parent"] == "Initializer"):
continue
print 'documenting ' + module + "/" + plugin + '...'
plugin_string = "# " + attributes["Name"] + "\n\n"
plugin_string += parseBrief(attributes.get("brief", None))
plugin_string += "* **file:** " + os.path.join(module, plugin) + "\n"
plugin_string += "* **inherits:** [" + attributes["Parent"] + "](" + parseInheritance(attributes["Parent"]) + ")\n"
plugin_string += parseAuthors(attributes.get("author", None), attributes.get("cite", None))
plugin_string += parseLinks(attributes.get("br_link", None))
plugin_string += parsePapers(attributes.get("br_paper", None))
plugin_string += parseFormats(attributes.get("br_format", None))
plugin_string += parseProperties(attributes.get("br_property", None))
plugin_string += "\n---\n\n"
names.append(attributes["Name"])
docs[attributes["Name"]] = plugin_string
for name in sorted(names):
output_file.write(docs[name])
main()