-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_docs.py
More file actions
184 lines (147 loc) · 5.12 KB
/
generate_docs.py
File metadata and controls
184 lines (147 loc) · 5.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
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
"""
Parses Python source files to generate a single markdown document of API
reference documentation.
"""
import ast
import sys
LIVE_BASE_URL = "https://github.com/matomatical/matthewplotlib/blob/main"
def main(
paths: list[str],
/,
code_base_url: str = LIVE_BASE_URL,
):
for path in paths:
if path.endswith(".md"):
print(f"echoing {path}...", file=sys.stderr)
with open(path) as f:
print(f.read())
elif path.endswith(".py"):
print(f"processing {path}...", file=sys.stderr)
# parse
with open(path) as f:
source_code = f.read()
tree = ast.parse(source_code)
# generate
visitor = MarkdownVisitor(path, code_base_url)
visitor.visit(tree)
print("\n".join(visitor.markdown))
else:
print(f"skipping {path}...", file=sys.stderr)
print("done.", file=sys.stderr)
class MarkdownVisitor(ast.NodeVisitor):
"""
Traverses an Abstract Syntax Tree and extracts docstrings
from modules, classes, and functions to build a Markdown document.
"""
def __init__(
self,
path: str,
code_base_url: str,
):
module = path[:-3].replace("/", ".")
if module.endswith(".__init__"):
self.module = module[:-len(".__init__")]
else:
self.module = module
self.path = path
self.code_url = "/".join([code_base_url, path])
self.markdown: list[str] = []
self.classctx: None | str = None
def visit_Module(self, node: ast.Module):
# section for the module
name = s(self.module)
self.markdown.append(f"## module {name}\n")
# path and source link
self.markdown.append(
f"**{s(self.path)}** "
f"([source]({self.code_url}))\n"
)
# docstring
module_docstring = ast.get_docstring(node)
if module_docstring:
self.markdown.append(f"{module_docstring}\n")
# children
self.generic_visit(node)
def visit_ClassDef(self, node: ast.ClassDef):
# skip private classes
if node.name.startswith('_'):
return
# subsection for the class
if self.classctx:
# inner class
name = f'{self.classctx}.{s(node.name)}'
else:
name = s(node.name)
self.markdown.append(f"### class {name}\n")
# inheritance and source link
bases = ', '.join(
[s(ast.unparse(b)) for b in node.bases if b is not None]
)
self.markdown.append(
f"**{name}({bases}):** "
f"([source]({self.code_url}#L{node.lineno}))\n"
)
# docstring
docstring = ast.get_docstring(node)
if docstring:
self.markdown.append(f"{docstring}\n")
# children
oldctx = self.classctx
self.classctx = name
self.generic_visit(node)
self.classctx = oldctx
# end class with a horizontal divider
self.markdown.append("---\n")
def visit_FunctionDef(self, node: ast.FunctionDef):
# skip private methods (but not dunder methods)
if node.name.startswith('_') and not node.name.startswith('__'):
return
# (but *do* skip __init__ methods)
if node.name == '__init__':
return
# section for the method or function
name = s(node.name)
if self.classctx is not None:
self.markdown.append(f"### method {self.classctx}.{name}\n")
else:
self.markdown.append(f"### function {name}\n")
# signature and source link
args_str = ", ".join([
arg.arg
+ f": {ast.unparse(arg.annotation)}" if arg.annotation else ""
for arg in node.args.args
])
return_str = f" -> {ast.unparse(node.returns)}" if node.returns else ""
signature = f"{name}({s(args_str)}){s(return_str)}"
self.markdown.append(
f"**{signature}:** "
f"([source]({self.code_url}#L{node.lineno}))\n"
)
# docstring
docstring = ast.get_docstring(node)
if docstring:
self.markdown.append(f"{docstring}\n")
# no recursion to children of methods or functions...
# end function with a horizontal divider
if self.classctx is None:
self.markdown.append("---\n")
def visit_TypeAlias(self, node: ast.TypeAlias):
# skip private methods
if node.name.id.startswith('_'):
return
# section for the type alias
name = s(node.name.id)
self.markdown.append(f"### type {name}\n")
# TODO: allow a docstring somehow...
# title and source link
self.markdown.append(
f"**{s(ast.unparse(node))}** "
f"([source]({self.code_url}#L{node.lineno}))\n"
)
def s(s: str) -> str:
"""
Sanitise string for markdown.
"""
return s.replace("_", r"\_").replace("*", r"\*")
if __name__ == "__main__":
main(sys.argv[1:])