trezor-core/docs/api.py

58 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/python3
import os
2016-05-06 07:19:10 -07:00
import re
def process_file(fn):
mod, ext = os.path.splitext(fn)
src = open('../%s' % (fn)).readlines()
r = []
2016-05-06 07:19:10 -07:00
cls = ''
if ext in ['.h', '.c']:
for l in src:
l = l.rstrip()
2016-05-05 13:44:47 -07:00
if l.startswith('/// def '):
r.append('``` python')
r.append(l[4:])
r.append('```')
2016-05-05 13:44:47 -07:00
elif l.startswith('/// '):
r.append(l[4:])
elif l == '///':
r.append('')
elif ext == '.py':
mod = mod[4:].replace('/', '.')
for l in src:
l = l.rstrip()
if l.startswith('def '):
r.append('``` python')
r.append('def %s.' % mod + l[4:-1])
r.append('```')
2016-05-06 07:19:10 -07:00
elif l.startswith('### '):
r.append(l[4:])
elif l.startswith('###'):
r.append('')
elif l.startswith('class '):
cls = re.match('class ([A-Za-z0-9_]*)', l).group(1)
elif l.startswith(' def ') and not l.startswith(' def __init__'):
r.append('``` python')
r.append('def %s.' % cls + l[8:-1])
r.append('```')
elif l.startswith(' ### '):
r.append(l[8:])
elif l.startswith(' ###'):
r.append('')
return r
def main():
tpl = open('api.template.md', 'rt').readlines()
f = open('api.md', 'wt')
for line in tpl:
if line.startswith('@'):
for l in process_file(line[1:].strip()):
f.write(l + '\n')
else:
f.write(line)
f.close()
if __name__ == "__main__":
main()