-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathparse-doxygen-xml.py
executable file
·313 lines (259 loc) · 9.39 KB
/
parse-doxygen-xml.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/env python3
import html
import sys
import textwrap
import xmltodict
from collections import OrderedDict
from pprint import pprint
def small(text):
return "<small>{}</small>".format(text)
def front_matter():
output = textwrap.dedent("""
---
layout: plain
---
""")
print(output.strip())
print()
def _macros_active(memberdef):
name = str(memberdef['name'])
if 'param' in memberdef:
value = ', '.join(x['defname'] for x in memberdef['param'])
value = "(" + value + ")"
else:
try:
if type(memberdef['initializer']) is OrderedDict:
value = str(memberdef['initializer']['#text'])
else:
value = str(memberdef['initializer'])
except KeyError:
value = ""
return "{} {}".format(name, value)
def _macros_detail(memberdef):
try:
desc = memberdef['detaileddescription']['para']
if type(desc) is OrderedDict:
# this is a bit ugly, but the xml parser is
# stripping out the ref and just leaving two
# spaces as the only indication of where it
# should be, so we work with that
tmp = desc['#text']
loc = tmp.find(' ') + 1
desc = tmp[:loc] + desc['ref']['#text'] + tmp[loc:]
desc = "<p>" + desc + "</p>"
except (TypeError, KeyError):
desc = ""
if 'param' in memberdef:
desc += "<p><strong>Value</strong><br />"
value = memberdef['initializer']
if type(value) is OrderedDict:
if type(value['ref']) != list:
values = [value['ref']['#text']]
else:
values = [x['#text'] for x in value['ref']]
tmp = value['#text']
if '\n' in tmp:
tmp = "<br />".join(tmp.split('\n'))
for replace in values:
loc = tmp.find('= ;')
if loc == -1:
loc = tmp.find('-> =')
if loc == -1:
continue
loc += 2
tmp = tmp[:loc] + replace + tmp[loc:]
else:
tmp = html.escape(str(value))
desc += small("<pre>{}</pre>".format(tmp))
desc += "</p>"
return desc
def macros(xml):
navdata = OrderedDict()
print("<h2 id='Macros'>Macros</h2>")
print("<table class='table'>")
for entry in xml['doxygen']['compounddef']['sectiondef']:
for mem in entry['memberdef']:
if mem['@kind'] == 'define':
name = mem['name']
navdata[name] = '#macro_{}'.format(name)
content = """
<ul class="list-group">
<li class="list-group-item active" id="{}">#define {}</li>
<li class="list-group-item">{}</li>
</ul>
""".format(navdata[name][1:],
_macros_active(mem),
_macros_detail(mem))
print("<tr><td>{}</td></tr>".format(content))
print("</table>")
return navdata
def typedefs(xml):
navdata = OrderedDict()
print("<h2 id='Typedefs'>Typedefs</h2>")
print("<table class='table'>")
for entry in xml['doxygen']['compounddef']['sectiondef']:
for mem in entry['memberdef']:
if mem['@kind'] == 'typedef':
definition = mem['definition']
name = mem['name']
navdata[name] = '#typedef_{}'.format(name)
try:
desc = "<p>"
desc += mem['detaileddescription']['para']
desc += "</p>"
except (TypeError, KeyError):
desc = ""
content = """
<ul class="list-group">
<li class="list-group-item active" id="{}">{}</li>
<li class="list-group-item">{}</li>
</ul>
""".format(navdata[name][1:], definition, desc)
print("<tr><td>{}</td></tr>".format(content))
print("</table>")
return navdata
def enums(xml):
navdata = OrderedDict()
print("<h2 id='Enums'>Enums</h2>")
print("<table class='table'>")
for entry in xml['doxygen']['compounddef']['sectiondef']:
for mem in entry['memberdef']:
if mem['@kind'] == 'enum':
name = mem['name']
navdata[name] = '#enum_{}'.format(name)
table = "<table class='table table-condensed table-striped'>"
for value in mem['enumvalue']:
table += "<tr><td>"
table += value['name']
table += "</td><td>"
try:
table += value['detaileddescription']['para']
except (TypeError, KeyError):
pass
table += "</td></tr>"
table += "</table>"
content = """
<ul class="list-group">
<li class="list-group-item active" id="{}">enum {}</li>
<li class="list-group-item">{}</li>
</ul>
""".format(navdata[name][1:], name, table)
print("<tr><td>{}</td></tr>".format(content))
print("</table>")
return navdata
def _func_signature(memberdef):
definition = memberdef['definition']
args = memberdef['argsstring']
return "{} {}".format(definition, args)
def _func_details(memberdef):
details = memberdef['detaileddescription']
output = ""
if not details:
return
for para in details['para']:
if type(para) != OrderedDict:
output += "<p>" + str(para) + "</p>"
elif '#text' in para and 'emphasis' in para:
if type(para['emphasis']) is list:
values = para['emphasis']
else:
values = [para['emphasis']]
tmp = str(para['#text'])
for value in values:
value = "<em>" + str(value) + "</em>"
loc = tmp.find(' ')
if loc == -1:
loc = tmp.find(' .')
if loc == -1:
value = "FAILED!!!!"
loc += 1
tmp = tmp[:loc] + value + tmp[loc:]
output += "<p>" + tmp + "</p>"
elif '#text' in para:
#TODO fix this case
#print >> sys.stderr, memberdef['name']
#print >> sys.stderr, para
pass
elif 'parameterlist' in para:
output += "<h5>Parameters</h5><ul>"
if type(para['parameterlist']['parameteritem']) is list:
items = para['parameterlist']['parameteritem']
else:
items = [para['parameterlist']['parameteritem']]
for entry in items:
dire = '<missing>'
name = '<missing>'
try:
if entry['parameternamelist']:
dire = entry['parameternamelist']['parametername']['@direction']
name = entry['parameternamelist']['parametername']['#text']
if entry['parameterdescription']:
desc = entry['parameterdescription']['para']
except TypeError:
pass
output += "<li>[{}] <em>{}</em> {}</li>".format(dire, name, desc)
output += "</ul>"
if 'simplesect' in para:
output += "<h5>Returns</h5><ul>"
output += "<li>{}</li></ul>".format(para['simplesect']['para'])
return output
def functions(xml):
navdata = OrderedDict()
print("<h2 id='Functions'>Functions</h2>")
print("<table class='table'>")
for entry in xml['doxygen']['compounddef']['sectiondef']:
for mem in entry['memberdef']:
if mem['@kind'] == 'function':
name = mem['name']
navdata[name] = '#func_{}'.format(name)
content = """
<ul class="list-group">
<li class="list-group-item active" id="{}">{}</li>
<li class="list-group-item">{}</li>
</ul>
""".format(navdata[name][1:],
_func_signature(mem),
_func_details(mem))
print("<tr><td>{}</td></tr>".format(content))
print("</table>")
return navdata
def nav(data):
print("""
<ul class="nav nav-stacked">
""")
for category in list(data.keys()):
print("<li>")
print("<a href='#" + category + "'>" + category + "</a>")
print("<ul class='nav nav-stacked'>")
for item in list(data[category].keys()):
print("<li><a href='{}'>{}</a></li>".format(
data[category][item], item))
print("</ul>")
print("</li>")
print("""
</ul>
""")
def main():
with open(sys.argv[1], 'r') as f:
xmldata = f.read()
xml = xmltodict.parse(xmldata)
sidenav = OrderedDict()
front_matter()
print("<div class='row'><div class='col-xs-9'>")
sidenav['Functions'] = functions(xml)
sidenav['Macros'] = macros(xml)
sidenav['Typedefs'] = typedefs(xml)
sidenav['Enums'] = enums(xml)
print("</div>")
print("""
<nav class="col-xs-3">
<div class="bs-docs-sidebar">
""")
nav(sidenav)
print("""
</div>
</nav>
""")
print("</div>")
if __name__ == '__main__':
main()