Skip to content

Commit 7945e5f

Browse files
committed
Support Sphinx RST signatures
1 parent 91ec92d commit 7945e5f

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

docstring_to_markdown/rst.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77

88
class Directive:
9-
def __init__(self, pattern: str, replacement: str, name: Union[str, None] = None):
9+
def __init__(
10+
self, pattern: str, replacement: str,
11+
name: Union[str, None] = None,
12+
flags: int = 0
13+
):
1014
self.pattern = pattern
1115
self.replacement = replacement
1216
self.name = name
17+
self.flags = flags
1318

1419

1520
# https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#cross-referencing-python-objects
@@ -79,6 +84,15 @@ def __init__(self, pattern: str, replacement: str, name: Union[str, None] = None
7984
'term',
8085
)
8186

87+
SPHINX_PARAM = (
88+
'param',
89+
'parameter',
90+
'arg',
91+
'argument',
92+
'key',
93+
'keyword'
94+
)
95+
8296
SPHINX_RULES: List[Directive] = [
8397
Directive(
8498
pattern=r':c:({}):`\.?(?P<name>[^`]+?)`'.format('|'.join(SPHINX_CROSS_REF_C)),
@@ -109,13 +123,40 @@ def __init__(self, pattern: str, replacement: str, name: Union[str, None] = None
109123
replacement=r'`\g<name>`'
110124
),
111125
Directive(
112-
pattern=r'^:param (?P<param>\S+):',
113-
replacement=r'- `\g<param>`:'
126+
pattern=r'^\s*:({}) (?P<type>\S+) (?P<param>\S+):'.format('|'.join(SPHINX_PARAM)),
127+
replacement=r'- `\g<param>` (`\g<type>`):',
128+
flags=re.MULTILINE
114129
),
115130
Directive(
116-
pattern=r'^:return:',
117-
replacement=r'Returns:'
118-
)
131+
pattern=r'^\s*:({}) (?P<param>\S+): (?P<desc>.*)(\n|\r\n?):type \2: (?P<type>.*)$'.format('|'.join(SPHINX_PARAM)),
132+
replacement=r'- `\g<param>` (\g<type>): \g<desc>',
133+
flags=re.MULTILINE
134+
),
135+
Directive(
136+
pattern=r'^\s*:({}) (?P<param>\S+):'.format('|'.join(SPHINX_PARAM)),
137+
replacement=r'- `\g<param>`:',
138+
flags=re.MULTILINE
139+
),
140+
Directive(
141+
pattern=r'^\s*:type (?P<param>\S+):',
142+
replacement=r' . Type: `\g<param>`:',
143+
flags=re.MULTILINE
144+
),
145+
Directive(
146+
pattern=r'^\s*:(return|returns):',
147+
replacement=r'- returns:',
148+
flags=re.MULTILINE
149+
),
150+
Directive(
151+
pattern=r'^\s*:rtype: (?P<type>\S+)',
152+
replacement=r'- return type: `\g<type>`',
153+
flags=re.MULTILINE
154+
),
155+
Directive(
156+
pattern=r'^\s*:(raises|raise|except|exception) (?P<exception>\S+):',
157+
replacement=r'- raises `\g<exception>`:',
158+
flags=re.MULTILINE
159+
),
119160
]
120161

121162

@@ -646,7 +687,7 @@ def flush_buffer():
646687
lines = '\n'.join(lines_buffer)
647688
# rst markup handling
648689
for directive in DIRECTIVES:
649-
lines = re.sub(directive.pattern, directive.replacement, lines)
690+
lines = re.sub(directive.pattern, directive.replacement, lines, flags=directive.flags)
650691

651692
for (section, header) in RST_SECTIONS.items():
652693
lines = lines.replace(header, '\n#### ' + section + '\n')

tests/test_rst.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,28 @@ def func(): pass
624624
"""
625625

626626

627+
# https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#info-field-lists
628+
SPHINX_SIGNATURE = """
629+
:param str sender: The person sending the message
630+
:param str recipient: The recipient of the message
631+
:param str message_body: The body of the message
632+
:param priority: The priority of the message, can be a number 1-5
633+
:type priority: integer or None
634+
:return: the message id
635+
:rtype: int
636+
:raises ValueError: if the message_body exceeds 160 characters
637+
"""
638+
639+
SPHINX_SIGNATURE_MARKDOWN = """\
640+
- `sender` (`str`): The person sending the message
641+
- `recipient` (`str`): The recipient of the message
642+
- `message_body` (`str`): The body of the message
643+
- `priority` (integer or None): The priority of the message, can be a number 1-5
644+
- returns: the message id
645+
- return type: `int`
646+
- raises `ValueError`: if the message_body exceeds 160 characters
647+
"""
648+
627649
RST_CASES = {
628650
'handles prompt continuation and multi-line output': {
629651
'rst': CODE_MULTI_LINE_CODE_OUTPUT,
@@ -739,9 +761,9 @@ def func(): pass
739761
'rst': ':param x: test arg',
740762
'md': '- `x`: test arg'
741763
},
742-
'converts sphinx return': {
743-
'rst': ':return: return description',
744-
'md': 'Returns: return description'
764+
'converts indented sphinx params': {
765+
'rst': '\t:param x: test arg',
766+
'md': '- `x`: test arg'
745767
},
746768
'converts non-standard simple table': {
747769
'rst': SIMPLE_TABLE,
@@ -762,6 +784,10 @@ def func(): pass
762784
'converts nested parameter lists': {
763785
'rst': NESTED_PARAMETERS,
764786
'md': NESTED_PARAMETERS_MARKDOWN
787+
},
788+
'converts sphinx signatures': {
789+
'rst': SPHINX_SIGNATURE,
790+
'md': SPHINX_SIGNATURE_MARKDOWN
765791
}
766792
}
767793

0 commit comments

Comments
 (0)