Skip to content

Commit 2ce75a5

Browse files
committed
fix: convert See Also section into dataclass
1 parent 0e818f3 commit 2ce75a5

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

quartodoc/ast.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# TODO: these classes are created to wrap some tuple outputs
77
# we should consolidate logic for transforming the griffe
88
# docstring here (or open a griffe issue).
9-
from .renderers import tuple_to_data, ExampleCode, ExampleText
9+
from .renderers import tuple_to_data, docstring_section_narrow, ExampleCode, ExampleText
1010

1111

1212
# Tree previewer ==============================================================
@@ -127,11 +127,15 @@ def get_field(self, obj, k):
127127
return getattr(obj, k)
128128

129129
def transform(self, obj):
130+
# TODO: currently this transform happens here, and in the renderer.
131+
# let's consolidate this into one step (when getting the object)
130132
if isinstance(obj, tuple):
131133
try:
132134
return tuple_to_data(obj)
133135
except ValueError:
134136
pass
137+
elif isinstance(obj, ds.DocstringSectionText):
138+
return docstring_section_narrow(obj)
135139

136140
return obj
137141

quartodoc/renderers.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from enum import Enum
12
from griffe.docstrings import dataclasses as ds
23
from griffe import dataclasses as dc
34
from griffe.expressions import Expression, Name
@@ -26,6 +27,28 @@ def tuple_to_data(el: "tuple[ds.DocstringSectionKind, str]"):
2627
raise ValueError(f"Unsupported first element in tuple: {kind}")
2728

2829

30+
def docstring_section_narrow(el: ds.DocstringSection) -> ds.DocstringSection:
31+
# attempt to narrow down text sections
32+
prefix = "See Also\n---"
33+
if isinstance(el, ds.DocstringSectionText) and el.value.startswith(prefix):
34+
stripped = el.value.replace(prefix, "", 1).lstrip("-\n")
35+
return DocstringSectionSeeAlso(stripped, el.title)
36+
37+
return el
38+
39+
40+
class DocstringSectionKindPatched(Enum):
41+
see_also = "see also"
42+
43+
44+
class DocstringSectionSeeAlso(ds.DocstringSection):
45+
kind = DocstringSectionKindPatched.see_also
46+
47+
def __init__(self, value: str, title: "str | None"):
48+
self.value = value
49+
super().__init__(title)
50+
51+
2952
@dataclass
3053
class ExampleCode:
3154
value: str
@@ -153,8 +176,9 @@ def to_md(self, el: Union[dc.Alias, dc.Object]):
153176
pass
154177
else:
155178
for section in el.docstring.parsed:
156-
title = section.kind.name
157-
body = self.to_md(section)
179+
new_el = docstring_section_narrow(section)
180+
title = new_el.kind.name
181+
body = self.to_md(new_el)
158182

159183
if title != "text":
160184
header = f"{'#' * (self.header_level + 1)} {title.title()}"
@@ -192,9 +216,17 @@ def to_md(self, el: dc.Parameter):
192216

193217
# docstring parts -------------------------------------------------------------
194218

219+
# text ----
220+
# note this can be a number of things. for example, opening docstring text,
221+
# or a section with a header not included in the numpydoc standard
195222
@dispatch
196223
def to_md(self, el: ds.DocstringSectionText):
197-
return el.value
224+
new_el = docstring_section_narrow(el)
225+
if isinstance(new_el, ds.DocstringSectionText):
226+
# ensures we don't recurse forever
227+
return el.value
228+
229+
return self.to_md(new_el)
198230

199231
# parameters ----
200232

@@ -227,6 +259,13 @@ def to_md(self, el: ds.DocstringSectionAttributes):
227259
def to_md(self, el: ds.DocstringAttribute):
228260
return el.name, self.to_md(el.annotation), el.description
229261

262+
# see also ----
263+
264+
@dispatch
265+
def to_md(self, el: DocstringSectionSeeAlso):
266+
# TODO: attempt to parse See Also sections
267+
return el.value
268+
230269
# examples ----
231270

232271
@dispatch

0 commit comments

Comments
 (0)