forked from lucjon/Py-StackExchange
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakedoc.py
More file actions
executable file
·134 lines (95 loc) · 3.12 KB
/
makedoc.py
File metadata and controls
executable file
·134 lines (95 loc) · 3.12 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
#!/usr/bin/env python
# Creates HTML documentation from api.yml
import yaml
class Function(object):
def __init__(self, id, tree_ob):
def use(key, v=None):
if key in tree_ob:
val = tree_ob[key]
if isinstance(val, str):
val = val.replace('<', '<').replace('>', '>')
setattr(self, key, val)
return True
else:
if v is not None:
setattr(self, key, v)
return False
self.id = id
use('description', '')
use('se_route', self.description)
if not use('unimplemented', False):
use('function')
use('returns')
use('parameters')
use('see')
use('example')
@property
def prototype(self):
if self.unimplemented:
raise AttributeError('prototype')
elif hasattr(self, 'see'):
return self.see
else:
params = ''
if hasattr(self, 'parameters'):
params = ', '.join(self.parameters.keys())
return '%s(%s)' % (self.function, params)
class HTMLDocGenerator(object):
def __init__(self, file_ob):
self.categories = []
self.parse(file_ob)
def parse(self, file_ob):
self.tree = yaml.load(file_ob)
unimplemented = 0
total = 0
for name, category in self.tree.items():
if name.startswith('__'):
continue
current_category = []
for funct_id, function in category.iteritems():
f = Function('%s.%s' % (name, funct_id), function)
if f.unimplemented:
unimplemented += 1
total += 1
current_category.append(f)
self.categories.append((name, current_category))
def to_html(self):
html = []
html.append('<style type="text/css">%s</style>' % self.tree.get('__style__', ''))
for category, functions in self.categories:
html.append('<h2>%s</h2>' % category.title())
for funct in functions:
html.append('<a name="%s"></a>' % funct.id)
html.append('<h3>%s</h3>' % funct.se_route)
html.append('<div class="api">')
if hasattr(funct, 'see'):
html.append('<div class="see">see <a href="#%s">%s</a></div>' % (funct.see, funct.see))
html.append('</div>')
continue
if not funct.unimplemented:
html.append('<div class="prototype">%s</div>' % funct.prototype)
html.append('<div class="description">%s</div>' % funct.description)
if funct.unimplemented:
html.append('<div class="unimplemented">Unimplemented.</div>')
html.append('</div>')
continue
if hasattr(funct, 'returns'):
html.append('<div class="returns">Returns: <span>%s</span></div>' % funct.returns)
if hasattr(funct, 'parameters'):
html.append('<h4>Parameters</h4>')
html.append('<div class="params">')
for key, desc in funct.parameters.iteritems():
html.append('<div><span class="param_name">%s</span> <span class="param_desc">%s</span></div>' % (key, desc))
html.append('</div>')
if hasattr(funct, 'example'):
html.append('<h4>Example</h4>')
html.append('<pre class="example">%s</pre>' % funct.example)
html.append('</div>')
return '\n'.join(html)
if __name__ == '__main__':
in_handle = open('api.yml')
out_handle = open('api.html', 'w')
docgen = HTMLDocGenerator(in_handle)
out_handle.write(docgen.to_html())
in_handle.close()
out_handle.close()