1
1
from abc import ABC , abstractmethod
2
2
from types import SimpleNamespace
3
- from typing import Union , List
3
+ from typing import Union , List , Dict
4
4
import re
5
5
6
6
@@ -68,6 +68,40 @@ def __init__(self, pattern: str, replacement: str, name: Union[str, None] = None
68
68
)
69
69
]
70
70
71
+
72
+ _RST_SECTIONS = {
73
+ 'Parameters' ,
74
+ 'Returns' ,
75
+ 'See Also' ,
76
+ 'Examples' ,
77
+ 'Attributes' ,
78
+ 'Notes' ,
79
+ 'References'
80
+ }
81
+
82
+
83
+ # TODO: type with RstSection = Literal[], and generate _RST_SECTIONS out of it once
84
+ # support for Python 3.6 can be safely dropped
85
+ SECTION_DIRECTIVES : Dict [str , List [Directive ]] = {
86
+ 'Parameters' : [
87
+ Directive (
88
+ pattern = r'^(?P<other_args>\*\*kwargs|\*args)$' ,
89
+ replacement = r'- `\g<other_args>`'
90
+ ),
91
+ Directive (
92
+ pattern = r'^(?P<arg1>[^:\s]+\d), (?P<arg2>[^:\s]+\d), \.\.\. : (?P<type>.+)$' ,
93
+ replacement = r'- `\g<arg1>`, `\g<arg2>`, `...`: \g<type>'
94
+ )
95
+ ],
96
+ 'References' : [
97
+ Directive (
98
+ pattern = r'^\.\. \[(?P<number>\d+)\] (?P<first_line>.+)$' ,
99
+ replacement = r'- [\g<number>] \g<first_line>'
100
+ )
101
+ ]
102
+ }
103
+
104
+
71
105
ESCAPING_RULES : List [Directive ] = [
72
106
Directive (
73
107
pattern = r'__(?P<text>\S+)__' ,
@@ -86,16 +120,6 @@ def _find_directive_pattern(name: str):
86
120
HIGHLIGHT_PATTERN = _find_directive_pattern ('highlight' )
87
121
CODE_BLOCK_PATTERN = _find_directive_pattern ('code-block' )
88
122
89
- _RST_SECTIONS = {
90
- 'Parameters' ,
91
- 'Returns' ,
92
- 'See Also' ,
93
- 'Examples' ,
94
- 'Attributes' ,
95
- 'Notes' ,
96
- 'References'
97
- }
98
-
99
123
100
124
def looks_like_rst (value : str ) -> bool :
101
125
# check if any of the characteristic sections (and the properly formatted underline) is there
@@ -406,19 +430,12 @@ def flush_buffer():
406
430
match = re .match (r'^(?P<argument>[^:\s]+) : (?P<type>.+)$' , trimmed_line )
407
431
if match :
408
432
line = '- `' + match .group ('argument' ) + '`: ' + match .group ('type' ) + ''
409
- elif most_recent_section == 'Parameters' :
410
- kwargs_or_args_match = re .match (r'^(?P<other_args>\*\*kwargs|\*args)$' , trimmed_line )
411
- if kwargs_or_args_match :
412
- line = '- `' + kwargs_or_args_match .group ('other_args' ) + '`'
413
- else :
414
- numpy_args_match = re .match (
415
- r'^(?P<arg1>[^:\s]+\d), (?P<arg2>[^:\s]+\d), \.\.\. : (?P<type>.+)$' ,
416
- trimmed_line
417
- )
418
- if numpy_args_match :
419
- groups = numpy_args_match .groupdict ()
420
- line = f'- `{ groups ["arg1" ]} `, `{ groups ["arg2" ]} `, `...`: { groups ["type" ]} '
421
433
else :
434
+ if most_recent_section in SECTION_DIRECTIVES :
435
+ for section_directive in SECTION_DIRECTIVES [most_recent_section ]:
436
+ if re .match (section_directive .pattern , trimmed_line ):
437
+ line = re .sub (section_directive .pattern , section_directive .replacement , trimmed_line )
438
+ break
422
439
if trimmed_line .rstrip () in RST_SECTIONS :
423
440
most_recent_section = trimmed_line .rstrip ()
424
441
0 commit comments