@@ -7,4 +7,106 @@ jupyter:
7
7
name : python3
8
8
---
9
9
10
- Coming soon!
10
+ quartodoc uses the library [ griffe] ( https://github.com/mkdocstrings/griffe ) to load and parse docstrings.
11
+
12
+ ## Reading docstrings
13
+
14
+ Use the function [ get_object] ( /api/#get_object ) to read in a docstring from a module.
15
+
16
+
17
+ ``` {python}
18
+ from quartodoc import get_object
19
+
20
+ f_obj = get_object("quartodoc", "get_object")
21
+ f_obj
22
+ ```
23
+
24
+ The result above is a griffe object representing the function ` quartodoc.get_object ` ,
25
+ which has two important attributes:
26
+
27
+ * ` .name ` : the function's name.
28
+ * ` .parameters ` : the function's parameters.
29
+ * ` .docstring.value ` : the actual docstring
30
+ * ` .docstring.parsed ` : the docstring parsed into a tree of griffe objects
31
+
32
+ ### Function name
33
+
34
+ ``` {python}
35
+ f_obj.name
36
+ ```
37
+
38
+ ### Function parameters
39
+
40
+ ``` {python}
41
+ f_obj.parameters
42
+ ```
43
+
44
+ ### Raw docstring value
45
+
46
+ ``` {python}
47
+ print(f_obj.docstring.value)
48
+ ```
49
+
50
+ ### Parsed docstring
51
+
52
+ ``` {python}
53
+ f_obj.docstring.parsed
54
+ ```
55
+
56
+ The docstring into a tree lets us define visitors, which can visit each element and
57
+ do useful things. For example, print a high-level overview of its structure, or render it to markdown.
58
+
59
+ ## Parsed docstring structure
60
+
61
+ * [ numpydocstring] ( https://numpydoc.readthedocs.io/en/latest/format.html ) - defines the numpydoc format for writing docstrings.
62
+ * griffe modules for representing docstrings:
63
+ - [ griffe.dataclasses] ( https://mkdocstrings.github.io/griffe/reference/griffe/dataclasses/#griffe.dataclasses )
64
+ - [ griffe.docstrings.dataclasses] ( https://mkdocstrings.github.io/griffe/reference/griffe/docstrings/dataclasses/#griffe.docstrings.dataclasses )
65
+
66
+
67
+ ## Rendering docstrings
68
+
69
+ quartodoc uses tree visitors to render parsed docstrings to formats like markdown and HTML.
70
+ Tree visitors define how each type of object in the parse tree should be handled.
71
+
72
+ ``` {python}
73
+ import griffe.dataclasses as dc
74
+ import griffe.docstrings.dataclasses as ds
75
+
76
+ from plum import dispatch
77
+ from typing import Union
78
+
79
+
80
+ class SomeRenderer:
81
+ def __init__(self, header_level: int = 1):
82
+ self.header_level = header_level
83
+
84
+ @dispatch
85
+ def visit(self, el):
86
+ raise NotImplementedError(f"Unsupported type: {type(el)}")
87
+
88
+ @dispatch
89
+ def visit(self, el: Union[dc.Alias, dc.Object]):
90
+ header = "#" * self.header_level
91
+ str_header = f"{header} {el.name}"
92
+ str_params = f"N PARAMETERS: {len(el.parameters)}"
93
+ str_sections = "SECTIONS: " + self.visit(el.docstring)
94
+
95
+ # return something pretty
96
+ return "\n".join([str_header, str_params, str_sections])
97
+
98
+ @dispatch
99
+ def visit(self, el: dc.Docstring):
100
+ return "A docstring with {len(el.parsed)} pieces"
101
+
102
+ print(SomeRenderer(header_level=2).visit(f_obj))
103
+ ```
104
+
105
+ Note 3 big pieces:
106
+
107
+ * ** Generic dispatch** : The plum ` dispatch ` function decorates each ` visit ` method. The type annotations
108
+ specify the types of data each version of visit should dispatch on.
109
+ * ** Default behavior** : The first ` visit ` method ensures a ` NotImplementedError ` is raised by default.
110
+ * ** Tree walking** : ` visit ` methods often call ` visit ` again on sub elements.
111
+
112
+
0 commit comments