-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path_renderer.py
More file actions
63 lines (49 loc) · 2 KB
/
_renderer.py
File metadata and controls
63 lines (49 loc) · 2 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
from __future__ import annotations
from numpydoc.docscrape import NumpyDocString
from plum import dispatch
from quartodoc import MdRenderer
from quartodoc import ast as qast
class Renderer(MdRenderer):
style = "siuba"
@dispatch
def render(self, el):
"""General render method.
Note: overloading of `render` enabled via plum.dispatch to allow different
rendering behaviour for some elements.
"""
prev_obj = getattr(self, "crnt_obj", None)
self.crnt_obj = el
res = super().render(el)
self.crnt_obj = prev_obj
return res
@dispatch
def render(self, el: qast.DocstringSectionSeeAlso): # noqa: F811
"""Numpy Docstring style render method.
Note: overloading of `render` enabled via plum.dispatch to allow different
rendering behaviour for some elements.
"""
lines = el.value.split("\n")
# each entry in result has form: ([('func1', '<directive>), ...], <description>)
parsed = NumpyDocString("")._parse_see_also(lines)
result = []
for funcs, description in parsed:
links = [f"[{name}](`{self._name_to_target(name)}`)" for name, role in funcs]
str_links = ", ".join(links)
if description:
str_description = "<br>".join(description)
result.append(f"{str_links}: {str_description}")
else:
result.append(str_links)
return "*\n".join(result)
def _name_to_target(self, name: str):
"""Helper method to convert a function/class name to a full target path,
used for Numpy Docstring style render method.
"""
crnt_path = getattr(self.crnt_obj, "path", None)
parent = crnt_path.rsplit(".", 1)[0] + "."
pkg = "classifai."
if crnt_path and not (name.startswith(pkg) or name.startswith(parent)):
return f"{parent}{name}"
elif not name.startswith(pkg):
return f"{pkg}{name}"
return name