|
| 1 | +from enum import Enum |
1 | 2 | from griffe.docstrings import dataclasses as ds
|
2 | 3 | from griffe import dataclasses as dc
|
3 | 4 | from griffe.expressions import Expression, Name
|
@@ -26,6 +27,28 @@ def tuple_to_data(el: "tuple[ds.DocstringSectionKind, str]"):
|
26 | 27 | raise ValueError(f"Unsupported first element in tuple: {kind}")
|
27 | 28 |
|
28 | 29 |
|
| 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 | + |
29 | 52 | @dataclass
|
30 | 53 | class ExampleCode:
|
31 | 54 | value: str
|
@@ -153,8 +176,9 @@ def to_md(self, el: Union[dc.Alias, dc.Object]):
|
153 | 176 | pass
|
154 | 177 | else:
|
155 | 178 | 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) |
158 | 182 |
|
159 | 183 | if title != "text":
|
160 | 184 | header = f"{'#' * (self.header_level + 1)} {title.title()}"
|
@@ -192,9 +216,17 @@ def to_md(self, el: dc.Parameter):
|
192 | 216 |
|
193 | 217 | # docstring parts -------------------------------------------------------------
|
194 | 218 |
|
| 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 |
195 | 222 | @dispatch
|
196 | 223 | 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) |
198 | 230 |
|
199 | 231 | # parameters ----
|
200 | 232 |
|
@@ -227,6 +259,13 @@ def to_md(self, el: ds.DocstringSectionAttributes):
|
227 | 259 | def to_md(self, el: ds.DocstringAttribute):
|
228 | 260 | return el.name, self.to_md(el.annotation), el.description
|
229 | 261 |
|
| 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 | + |
230 | 269 | # examples ----
|
231 | 270 |
|
232 | 271 | @dispatch
|
|
0 commit comments