forked from beancount/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
52 lines (42 loc) · 2.23 KB
/
build.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
import sys
import inspect
from mkdocs.__main__ import cli
from mkdocstrings.handlers import python as python_handler
def rebuild_category_lists(obj: dict) -> None:
"""
Recursively rebuild the category lists of a collected object.
Since `pytkdocs` dumps JSON on standard output, it must serialize the object-tree and flatten it to reduce data
duplication and avoid cycle-references. Indeed, each node of the object-tree has a `children` list, containing
all children, and another list for each category of children: `attributes`, `classes`, `functions`, `methods`
and `modules`. It replaces the values in category lists with only the paths of the objects.
Here, we reconstruct these category lists by picking objects in the `children` list using their path.
For each object, we recurse on every one of its children.
Args:
obj: The collected object, loaded back from JSON into a Python dictionary.
"""
obj["attributes"] = [obj["children"][path] for path in obj["attributes"]]
obj["classes"] = [obj["children"][path] for path in obj["classes"]]
obj["functions"] = [obj["children"][path] for path in obj["functions"]]
obj["methods"] = [obj["children"][path] for path in obj["methods"]]
obj["modules"] = [obj["children"][path] for path in obj["modules"]]
obj["children"] = [v for k, v in obj["children"].items()]
for child in obj["children"]:
rebuild_category_lists(child)
# Workaround for https://github.com/pawamoy/mkdocstrings/issues/108
for section in obj['docstring_sections']:
if section['type'] == 'markdown':
section['value'] = section['value'].\
replace('<', '<').\
replace('>', '>')
elif section['type'] == 'parameters':
for item in section['value']:
item['description'] = item['description'].\
replace('<', '<').\
replace('>', '>')
elif section['type'] == 'return':
section['value']['description'] = section['value']['description'].\
replace('<', '<').\
replace('>', '>')
python_handler.rebuild_category_lists = rebuild_category_lists
if __name__ == '__main__':
sys.exit(cli())