-
Notifications
You must be signed in to change notification settings - Fork 0
/
doxygen.py
414 lines (362 loc) · 13 KB
/
doxygen.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/python
# Copyright (C) 2014 Reece H. Dunn
#
# This file is part of documentation-generator.
#
# documentation-generator is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# documentation-generator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with documentation-generator. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import xmlapi
import cpplex
import docs
class Item:
def __init__(self, protection, kind, name):
self.protection = protection if protection else 'public'
self.kind = kind
self.name = name.split('::')[-1]
self.qname = name
self.children = []
self.docs = None
def __repr__(self):
return 'Item({0}, {1}, {2})'.format(self.protection, self.kind, self.name)
class Variable(Item):
def __init__(self, protection, kind, name):
Item.__init__(self, protection, kind, name)
class Function(Item):
def __init__(self, protection, kind, name):
Item.__init__(self, protection, kind, name)
self.args = []
self.retdoc = None
def arg(self, name):
for a in self.args:
if a.name == name:
return a
raise KeyError('argument %s not found' % name)
class FunctionPointer(Function):
def __init__(self, protection, kind, name):
Function.__init__(self, protection, kind, name)
def get_scoped_name(item, scope):
a = item.qname.split('::')
b = scope.qname.split('::') if scope else []
for n in range(1, len(a)+1):
if n <= len(b) and a[n-1] == b[n-1]:
continue
yield a[n-1], '::'.join(a[0:n])
_items = {}
def signature(item, scope):
ret = []
if isinstance(item, Variable) or isinstance(item, Function):
if item.kind == 'typedef':
ret.append(cpplex.Keyword(item.kind))
ret.append(cpplex.WhiteSpace(' '))
ret.extend(item.vartype)
ret.append(cpplex.WhiteSpace(' '))
elif item.kind == 'enumclass':
ret.append(cpplex.Keyword('enum'))
ret.append(cpplex.WhiteSpace(' '))
ret.append(cpplex.Keyword('class'))
ret.append(cpplex.WhiteSpace(' '))
elif item.kind != 'enumvalue':
ret.append(cpplex.Keyword(item.kind))
ret.append(cpplex.WhiteSpace(' '))
if isinstance(item, FunctionPointer):
ret.append(cpplex.Operator('('))
ret.append(cpplex.Operator('*'))
names = list(get_scoped_name(item, scope))
for i, (name, qname) in enumerate(names):
if i < len(names) - 1:
sref = _items[qname]
if len(sref) > 1:
raise Exception('{0} is ambiguous'.format(qname))
ret.append(sref[0])
ret.append(cpplex.Operator('::'))
else:
ret.extend(cpplex.tokenize(name))
if isinstance(item, FunctionPointer):
ret.append(cpplex.Operator(')'))
if isinstance(item, Function):
ret.append(cpplex.Operator('('))
for i, arg in enumerate(item.args):
ret.extend(arg.vartype)
ret.append(cpplex.WhiteSpace(' '))
ret.append(cpplex.Identifier(arg.name))
if i != len(item.args) - 1:
ret.append(cpplex.Operator(','))
ret.append(cpplex.WhiteSpace(' '))
ret.append(cpplex.Operator(')'))
return ret
class ItemRef:
def __init__(self, ref):
self.ref = ref
self.item = None
def __repr__(self):
return 'ItemRef({0} => {1})'.format(self.ref, repr(self.item))
@property
def html(self):
return '<a href="{0}.html">{1}</a>'.format(self.ref, self.item.name)
_item_refs = {}
def create_item_ref(ref):
if ref in _item_refs.keys():
return _item_refs[ref]
ret = ItemRef(ref)
_item_refs[ref] = ret
return ret
def create_item(ref, protection, kind, name):
if kind in ['variable', 'typedef']:
ref.item = Variable(protection, kind, name)
elif kind == 'function':
ref.item = Function(protection, kind, name)
else:
ref.item = Item(protection, kind, name)
if not name in _items.keys():
_items[name] = []
_items[name].append(ref)
_file_cache = {}
def is_enum_class(xml):
# FIXME: Make 'enum class' detection more robust
filename = xml['@file']
line = int(xml['@line'])
if not filename in _file_cache.keys():
with open(filename) as f:
_file_cache[filename] = f.read().split('\n')
tokens = [x for x in list(cpplex.tokenize(_file_cache[filename][line - 2])) if not isinstance(x, cpplex.WhiteSpace)]
if tokens[0].value != 'enum':
raise Exception('Expected an enum declaration.')
return tokens[1].value == 'class'
def is_function_pointer(vartype):
if len(vartype) < 3:
return False
return vartype[-2].value == '(' and vartype[-1].value[-1] == '*'
def _parse_type_node(xml):
ret = []
for child in xml.children():
if child.name == '#text':
ret.extend(list(cpplex.tokenize(child.node.nodeValue)))
elif child.name == 'ref':
xref = create_item_ref(child['@refid'])
ret.append(xref)
return ret
def _parse_memberdef_node(xml, parent):
ref = create_item_ref(xml['@id'])
name = '::'.join([parent.item.qname, xml['name/text()']])
if '<' in name and '>' in name: # template specialization
name = name.split('<')[0]
create_item(ref, xml['@prot'], xml['@kind'], name)
parent.item.children.append(ref)
# doxygen does not indicate if an enum is a C++11 scoped enum
if ref.item.kind == 'enum' and is_enum_class(xml['location']):
ref.item.kind = 'enumclass'
argnum = 0
for child in xml:
if child.name == 'type':
ref.item.vartype = _parse_type_node(child)
if is_function_pointer(ref.item.vartype):
item = ref.item
ref.item = FunctionPointer(ref.item.protection, ref.item.kind, ref.item.name)
ref.item.vartype = item.vartype[0:-2]
elif child.name == 'param':
argnum = argnum + 1
pname = child['declname/text()']
ptype = _parse_type_node(child['type'])
if not pname:
pname = '__arg{0}'.format(argnum)
p = Variable('public', 'parameter', pname)
p.vartype = ptype
ref.item.args.append(p)
elif child.name == 'argsstring':
args = _parse_type_node(child)
elif child.name == 'enumvalue':
vref = create_item_ref(child['@id'])
if ref.item.kind == 'enumclass':
vname = '::'.join([name, child['name/text()']])
else:
vname = '::'.join([parent.item.qname, child['name/text()']])
create_item(vref, child['@prot'], 'enumvalue', vname)
ref.item.children.append(vref)
if isinstance(ref.item, FunctionPointer) and len(ref.item.args) == 0 and len(args) != 0:
# doxygen does not parse function pointer arguments into param items,
# so we need to parse them here ...
if not args[0].value == ')' or not args[1].value == '(' or not args[-1].value == ')':
raise Exception('Invalid function pointer construct.')
args = args[2:]
param = []
argnum = 0
for token in args:
if token.value == ')' and len(param) == 0:
pass
elif token.value == ',' or token.value == ')':
argnum = argnum + 1
if isinstance(param[-1], cpplex.Identifier) and len(param) > 1:
pname = param[-1].value
param = param[:-1]
else:
pname = '__arg{0}'.format(argnum)
if isinstance(param[-1], cpplex.WhiteSpace):
param = param[:-1]
p = Variable('public', 'parameter', pname)
p.vartype = param
ref.item.args.append(p)
param = []
elif len(param) == 0 and isinstance(token, cpplex.WhiteSpace):
pass
else:
param.append(token)
def _parse_sectiondef_node(xml, parent):
for child in xml:
if child.name == 'memberdef':
_parse_memberdef_node(child, parent)
def _parse_compounddef_node(xml):
if xml['@kind'] in ['file', 'dir', 'page']:
return None
ref = create_item_ref(xml['@id'])
for child in xml:
if child.name == 'compoundname':
create_item(ref, xml['@prot'], xml['@kind'], child['text()'])
elif child.name == 'sectiondef':
_parse_sectiondef_node(child, ref)
return ref
def parse_doxygen(filename):
xml = xmlapi.XmlDocument(filename)
item = None
for child in xml:
if child.name == 'compounddef':
if item:
raise Exception('Multiple compounddef nodes found.')
item = _parse_compounddef_node(child)
return item
if __name__ == '__main__':
def escape(text):
return text.encode('utf-8').replace('&', '&').replace('<', '<').replace('>', '>')
def print_etree(e, f=sys.stdout, terminator='\n', scope=None, inline=False):
if e.tag == 'a' and 'href' in e.attrib.keys() and e.attrib['href'].startswith('^^'):
name = e.attrib['href'].replace('^^', '')
try:
ref = _items[name][0]
if e.text == '':
n = ref.item.qname
f.write('<a href="{0}.html">{1}</a>'.format(ref.ref, escape(n)))
else:
f.write('<a href="{0}.html">{1}</a>'.format(ref.ref, escape(e.text)))
except KeyError:
sys.stderr.write('error: cross reference {0} not found\n'.format(name))
f.write('{0}'.format(escape(name)))
if e.tail != None:
f.write('{0}'.format(escape(e.tail)))
return
if not inline:
args=''.join([' {0}="{1}"'.format(x, y) for x, y in e.attrib.items()])
f.write('<{0}{1}>'.format(e.tag, args))
if e.text != None:
f.write('{0}'.format(escape(e.text)))
for child in e:
print_etree(child, f=f, terminator='', scope=scope)
if not inline:
f.write('</{0}>{1}'.format(e.tag, terminator))
if e.tail != None and e.tail.strip() != '':
f.write('{0}'.format(escape(e.tail)))
def generate_html(f, ref, scope=None, recurse_children=True):
f.write('<p><code>')
for token in signature(ref.item, scope):
f.write(token.html)
f.write('</code></p>\n')
if ref.item.docs and ref.item.docs.brief != None:
print_etree(ref.item.docs.brief, f, scope=ref.item)
if isinstance(ref.item, Function) and len(ref.item.args) > 0:
f.write('<table class="parameters">\n')
for arg in ref.item.args:
f.write('<tr>\n')
f.write('<td><code><span class="identifier">{0}</span></code></td>\n'.format(arg.name))
f.write('<td>\n')
if arg.docs and arg.docs.brief != None:
if len(arg.docs.detailed) == 0:
print_etree(arg.docs.brief, f, scope=ref.item, inline=True)
else:
print_etree(arg.docs.brief, f, scope=ref.item)
for doc in arg.docs.detailed:
print_etree(doc, f, scope=ref.item)
f.write('</td>\n')
f.write('</tr>\n')
f.write('</table>\n')
for doc in ref.item.docs.detailed:
print_etree(doc, f, scope=ref.item)
if isinstance(ref.item, Function) and ref.item.retdoc and ref.item.retdoc.brief != None:
f.write('<dl>\n')
f.write('<dt class="return">Returns:</dt>\n')
f.write('<dd>\n')
print_etree(ref.item.retdoc.brief, f, scope=ref.item)
for doc in ref.item.retdoc.detailed:
print_etree(doc, f, scope=ref.item)
f.write('</dd>\n')
f.write('</dl>\n')
if len(ref.item.children) > 0:
if ref.item.kind in ['enum', 'enumclass']:
f.write('<table class="enumeration">\n')
for child in ref.item.children:
f.write('<tr>\n')
f.write('<td><code><span class="identifier">{0}</span></code></td>\n'.format(child.item.name))
f.write('<td>\n')
if child.item.docs and child.item.docs.brief != None:
if len(child.item.docs.detailed) == 0:
print_etree(child.item.docs.brief, f, scope=ref.item, inline=True)
else:
print_etree(child.item.docs.brief, f, scope=ref.item)
for doc in child.item.docs.detailed:
print_etree(doc, f, scope=ref.item)
f.write('</td>\n')
f.write('</tr>\n')
f.write('</table>\n')
elif recurse_children:
for child in ref.item.children:
if child.item.protection == 'public':
generate_html(f, child, scope=ref.item, recurse_children=False)
items = []
for filename in sys.argv[1:]:
if filename.endswith('.xml'):
item = parse_doxygen(filename)
if item:
items.append(item)
for filename in sys.argv[1:]:
if filename.endswith('.md'):
docs.parse(filename, _items)
for qname, refs in sorted(_items.items()):
for ref in refs:
if not ref.item.docs and ref.item.protection == 'public':
sys.stderr.write('error: item {0} is not documented\n'.format(qname))
rootdir = 'docs/api/html'
if not os.path.exists(rootdir):
os.mkdir(rootdir)
for item in items:
if not item.item or item.item.protection != 'public':
continue
with open(os.path.join(rootdir, '%s.html' % item.ref), 'w') as f:
f.write('<!DOCTYPE html>\n')
f.write('<html>\n')
f.write('<head>\n')
f.write('<title>{0} documentation</title>\n'.format(item.item.qname))
f.write('<style type="text/css">\n')
f.write(' table { width: 100%; }')
f.write(' table tr td { vertical-align: top; border-bottom: 1px solid #EEE; }')
f.write(' table tr td * { margin-top: 0; }')
f.write(' blockquote { margin-top: 0; margin-bottom: 0; }\n')
f.write(' .identifier { font-weight: normal; color: navy; }\n')
f.write(' .keyword { font-weight: bold; color: green; }\n')
f.write(' .operator { font-weight: normal; color: black; }\n')
f.write(' .literal { font-weight: normal; color: magenta; }\n')
f.write(' .return { font-weight: bold; color: black; }\n')
f.write('</style>\n')
f.write('</head>\n')
f.write('<body>\n')
generate_html(f, item)
f.write('</body>\n')
f.write('</html>\n')