Skip to content

Commit ef7a066

Browse files
committed
tools, gendoc: Allow constants at module level; gen module index.
Addresses some issues from adafruit#585.
1 parent 4162271 commit ef7a066

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

tools/gendoc.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __init__(self, name, descr):
9393
self.descr = descr
9494

9595
def dump(self, ctx):
96-
return '{}.{} - {}'.format(ctx, self.name, self.descr)
96+
return '`{}.{}` - {}'.format(ctx, self.name, self.descr)
9797

9898
class DocFunction(DocItem):
9999
def __init__(self, name, args):
@@ -175,7 +175,7 @@ def dump(self):
175175
s.append("## Constants")
176176
for c in sorted(self.constants.values(), key=lambda x:x.name):
177177
s.append('')
178-
s.append('`{}`'.format(c.dump(self.name)))
178+
s.append(c.dump(self.name))
179179
return '\n'.join(s)
180180

181181
class DocModule(DocItem):
@@ -217,7 +217,16 @@ def process_method(self, lex, d):
217217
self.cur_class.process_method(lex, d)
218218

219219
def process_constant(self, lex, d):
220-
self.cur_class.process_constant(lex, d)
220+
if self.cur_class is None:
221+
# a module-level constant
222+
name = d['id']
223+
if name in self.constants:
224+
lex.error("multiple definition of constant '{}'".format(name))
225+
self.constants[name] = DocConstant(name, d['descr'])
226+
lex.opt_break()
227+
else:
228+
# a class-level constant
229+
self.cur_class.process_constant(lex, d)
221230

222231
def validate(self):
223232
if self.descr is None:
@@ -234,6 +243,12 @@ def dump(self):
234243
for f in sorted(self.functions.values(), key=lambda x:x.name):
235244
s.append('')
236245
s.append(f.dump(self.name))
246+
if self.constants:
247+
s.append('')
248+
s.append("## Constants")
249+
for c in sorted(self.constants.values(), key=lambda x:x.name):
250+
s.append('')
251+
s.append(c.dump(self.name))
237252
if self.classes:
238253
s.append('')
239254
s.append('## Classes')
@@ -310,7 +325,19 @@ def validate(self):
310325
for m in self.modules.values():
311326
m.validate()
312327

328+
def dump(self):
329+
s = []
330+
if self.modules:
331+
s.append('')
332+
s.append('# Modules')
333+
for m in sorted(self.modules.values(), key=lambda x:x.name):
334+
s.append('')
335+
s.append('[`{}`]({}) - {}'.format(m.name, m.name, m.descr))
336+
return '\n'.join(s)
337+
313338
def write(self, dir):
339+
with open(os.path.join(dir, 'index.html'), 'wt') as f:
340+
f.write(markdown.markdown(self.dump()))
314341
for m in self.modules.values():
315342
mod_dir = os.path.join(dir, 'module', m.name)
316343
makedirs(mod_dir)
@@ -324,7 +351,7 @@ def write(self, dir):
324351
(Doc.process_function, re.compile(r'\\function (?P<id>[a-z0-9_]+)(?P<args>\(.*\))$')),
325352
(Doc.process_classmethod, re.compile(r'\\classmethod (?P<id>\\?[a-z0-9_]+)(?P<args>\(.*\))$')),
326353
(Doc.process_method, re.compile(r'\\method (?P<id>\\?[a-z0-9_]+)(?P<args>\(.*\))$')),
327-
(Doc.process_constant, re.compile(r'\\constant (?P<id>[A-Z0-9_]+) - ' + regex_descr + r'$')),
354+
(Doc.process_constant, re.compile(r'\\constant (?P<id>[A-Za-z0-9_]+) - ' + regex_descr + r'$')),
328355
#(Doc.process_classref, re.compile(r'\\classref (?P<id>[A-Za-z0-9_]+)$')),
329356
(Doc.process_class, re.compile(r'\\class (?P<id>[A-Za-z0-9_]+) - ' + regex_descr + r'$')),
330357
)

0 commit comments

Comments
 (0)