1
+ import quartodoc .ast as qast
1
2
import re
2
3
3
- from enum import Enum
4
4
from griffe .docstrings import dataclasses as ds
5
5
from griffe import dataclasses as dc
6
- from dataclasses import dataclass
7
6
from tabulate import tabulate
8
7
from plum import dispatch
9
8
from typing import Tuple , Union
10
9
11
10
12
- # Docstring rendering =========================================================
13
-
14
11
# utils -----------------------------------------------------------------------
15
- # these largely re-format the output of griffe
16
-
17
-
18
- def tuple_to_data (el : "tuple[ds.DocstringSectionKind, str]" ):
19
- """Re-format funky tuple setup in example section to be a class."""
20
- assert len (el ) == 2
21
-
22
- kind , value = el
23
- if kind .value == "examples" :
24
- return ExampleCode (value )
25
- elif kind .value == "text" :
26
- return ExampleText (value )
27
-
28
- raise ValueError (f"Unsupported first element in tuple: { kind } " )
29
-
30
-
31
- def docstring_section_narrow (el : ds .DocstringSection ) -> ds .DocstringSection :
32
- # attempt to narrow down text sections
33
- to_narrow = [
34
- DocstringSectionSeeAlso ,
35
- DocstringSectionNotes ,
36
- DocstringSectionWarnings ,
37
- ]
38
- prefix = "See Also\n ---"
39
-
40
- if isinstance (el , ds .DocstringSectionText ):
41
- for cls in to_narrow :
42
- prefix = cls .kind .value .title () + "\n ---"
43
- if el .value .lstrip ("\n " ).startswith (prefix ):
44
-
45
- stripped = el .value .replace (prefix , "" , 1 ).lstrip ("-\n " )
46
- return cls (stripped , el .title )
47
-
48
- return el
49
-
50
-
51
- class DocstringSectionKindPatched (Enum ):
52
- see_also = "see also"
53
- notes = "notes"
54
- warnings = "warnings"
55
-
56
-
57
- class DocstringSectionSeeAlso (ds .DocstringSection ):
58
- kind = DocstringSectionKindPatched .see_also
59
-
60
- def __init__ (self , value : str , title : "str | None" ):
61
- self .value = value
62
- super ().__init__ (title )
63
-
64
-
65
- class DocstringSectionNotes (ds .DocstringSection ):
66
- kind = DocstringSectionKindPatched .notes
67
-
68
- def __init__ (self , value : str , title : "str | None" ):
69
- self .value = value
70
- super ().__init__ (title )
71
-
72
-
73
- class DocstringSectionWarnings (ds .DocstringSection ):
74
- kind = DocstringSectionKindPatched .warnings
75
-
76
- def __init__ (self , value : str , title : "str | None" ):
77
- self .value = value
78
- super ().__init__ (title )
79
-
80
-
81
- @dataclass
82
- class ExampleCode :
83
- value : str
84
-
85
-
86
- @dataclass
87
- class ExampleText :
88
- value : str
89
12
90
13
91
14
def escape (val : str ):
@@ -258,7 +181,7 @@ def render(self, el: Union[dc.Object, dc.Alias]):
258
181
pass
259
182
else :
260
183
for section in el .docstring .parsed :
261
- new_el = docstring_section_narrow (section )
184
+ new_el = qast . transform (section )
262
185
title = new_el .kind .value
263
186
body = self .render (new_el )
264
187
@@ -309,7 +232,7 @@ def render(self, el: dc.Parameter):
309
232
# or a section with a header not included in the numpydoc standard
310
233
@dispatch
311
234
def render (self , el : ds .DocstringSectionText ):
312
- new_el = docstring_section_narrow (el )
235
+ new_el = qast . transform (el )
313
236
if isinstance (new_el , ds .DocstringSectionText ):
314
237
# ensures we don't recurse forever
315
238
return el .value
@@ -349,7 +272,7 @@ def render(self, el: ds.DocstringAttribute):
349
272
# see also ----
350
273
351
274
@dispatch
352
- def render (self , el : DocstringSectionSeeAlso ):
275
+ def render (self , el : qast . DocstringSectionSeeAlso ):
353
276
# TODO: attempt to parse See Also sections
354
277
return convert_rst_link_to_md (el .value )
355
278
@@ -358,11 +281,11 @@ def render(self, el: DocstringSectionSeeAlso):
358
281
@dispatch
359
282
def render (self , el : ds .DocstringSectionExamples ):
360
283
# its value is a tuple: DocstringSectionKind["text" | "examples"], str
361
- data = map (tuple_to_data , el .value )
284
+ data = map (qast . transform , el .value )
362
285
return "\n \n " .join (list (map (self .render , data )))
363
286
364
287
@dispatch
365
- def render (self , el : ExampleCode ):
288
+ def render (self , el : qast . ExampleCode ):
366
289
return f"""```python
367
290
{ el .value }
368
291
```"""
@@ -384,7 +307,7 @@ def render(self, el: Union[ds.DocstringReturn, ds.DocstringRaise]):
384
307
# unsupported parts ----
385
308
386
309
@dispatch
387
- def render (self , el : ExampleText ):
310
+ def render (self , el : qast . ExampleText ):
388
311
return el .value
389
312
390
313
@dispatch .multi (
0 commit comments