Skip to content

Commit df84fe1

Browse files
committed
docs: flesh out handling docstrings page
1 parent 40d4200 commit df84fe1

File tree

5 files changed

+143
-6
lines changed

5 files changed

+143
-6
lines changed

docs/_quarto.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ website:
2121
style: floating
2222
align: left
2323
contents:
24-
- get-started/overview.qmd
25-
- get-started/docstrings.qmd
26-
- get-started/renderers.qmd
27-
- get-started/crossrefs.qmd
24+
- section: "Basics"
25+
contents:
26+
- get-started/overview.qmd
27+
- get-started/basic-docs.qmd
28+
- get-started/crossrefs.qmd
29+
- section: "Advanced"
30+
contents:
31+
- get-started/docstrings.qmd
32+
- get-started/interlinks.qmd
2833
- id: dummy
2934

3035

docs/get-started/renderers.qmd renamed to docs/get-started/basic-docs.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Renderers
2+
title: Generating docs
33
jupyter:
44
kernelspec:
55
display_name: Python 3 (ipykernel)

docs/get-started/docstrings.qmd

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,106 @@ jupyter:
77
name: python3
88
---
99

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+

docs/get-started/interlinks.qmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Interlinks filter
3+
jupyter:
4+
kernelspec:
5+
display_name: Python 3 (ipykernel)
6+
language: python
7+
name: python3
8+
---
9+
10+
Coming soon!
11+
12+
See the [interlinks folder](https://github.com/machow/quartodoc/tree/main/interlinks), which defines a WIP quarto filter for providing crossrefs.

docs/get-started/overview.qmd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@ jupyter:
88
---
99

1010
Coming soon!
11+
12+
## Goals
13+
14+
* Load docstrings (with [griffe](https://github.com/mkdocstrings/griffe))
15+
* Render docstrings (e.g. with [MdRenderer](/api/#sec-MdRenderer))
16+
* Enable cross references to function documentation.
17+
- Link to functions within a doc.
18+
- Link to functions in other docs.
19+
- Generate an inventory file for other docs to link to yours.
20+
* (WIP) Generate high-level summaries.
21+
- Class summaries, with methods.
22+
- Tables of function names and descriptions.
23+
24+
## Different documentation structures
25+
26+
* All functions listed on a single page.
27+
* Functions split across a few pages (e.g. parsers, renderers).
28+
* Each function gets its own page.

0 commit comments

Comments
 (0)