Skip to content

Commit a4cf0e9

Browse files
committed
Complete support for RST admonitions
1 parent 2d84ce4 commit a4cf0e9

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

docstring_to_markdown/rst.py

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,77 @@ def __init__(
156156
]
157157

158158

159+
class Admonition:
160+
def __init__(self, name: str, label: str, icon: str = ''):
161+
self.name = name
162+
self.label = label
163+
self.icon = icon
164+
165+
@property
166+
def block_markdown(self):
167+
if self.icon:
168+
return f'{self.icon} **{self.label}**'
169+
return f'**{self.label}**'
170+
171+
@property
172+
def inline_markdown(self):
173+
return self.block_markdown + ':'
174+
175+
176+
ADMONITIONS = [
177+
Admonition(
178+
name='caution',
179+
label='Caution',
180+
icon='⚠️ '
181+
),
182+
Admonition(
183+
name='attention',
184+
label='Attention',
185+
icon='⚠️ '
186+
),
187+
Admonition(
188+
name='danger',
189+
label='Danger',
190+
icon='⚠️ '
191+
),
192+
Admonition(
193+
name='hint',
194+
label='Hint',
195+
icon='🛈'
196+
),
197+
Admonition(
198+
name='important',
199+
label='Important',
200+
icon='⚠️ '
201+
),
202+
Admonition(
203+
name='note',
204+
label='Note',
205+
icon='🛈'
206+
),
207+
Admonition(
208+
name='tip',
209+
label='Tip',
210+
icon='🛈'
211+
),
212+
Admonition(
213+
name='warning',
214+
label='Warning',
215+
icon='⚠️ '
216+
)
217+
]
218+
219+
220+
ADMONITION_DIRECTIVES: List[Directive] = [
221+
# https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions
222+
Directive(
223+
pattern=rf'\.\. {admonition.name}::',
224+
replacement=admonition.inline_markdown
225+
)
226+
for admonition in ADMONITIONS
227+
]
228+
229+
159230
RST_DIRECTIVES: List[Directive] = [
160231
Directive(
161232
pattern=r'\.\. versionchanged:: (?P<version>\S+)(?P<end>$|\n)',
@@ -169,10 +240,7 @@ def __init__(
169240
pattern=r'\.\. deprecated:: (?P<version>\S+)(?P<end>$|\n)',
170241
replacement=r'*Deprecated since \g<version>*\g<end>'
171242
),
172-
Directive(
173-
pattern=r'\.\. warning::',
174-
replacement=r'**Warning**:'
175-
),
243+
*ADMONITION_DIRECTIVES,
176244
Directive(
177245
pattern=r'\.\. seealso::(?P<short_form>.*)(?P<end>$|\n)',
178246
replacement=r'*See also*\g<short_form>\g<end>'
@@ -604,13 +672,17 @@ def initiate_parsing(self, line: str, current_language: str):
604672

605673
class NoteBlockParser(IndentedBlockParser):
606674
enclosure = '\n---'
607-
directives = {'.. note::', '.. warning::'}
675+
directives = {
676+
f'.. {admonition.name}::': admonition
677+
for admonition in ADMONITIONS
678+
}
608679

609680
def can_parse(self, line: str):
610681
return line.strip() in self.directives
611682

612683
def initiate_parsing(self, line: str, current_language: str):
613-
self._start_block('\n**Note**\n' if 'note' in line else '\n**Warning**\n')
684+
admonition = self.directives[line.strip()]
685+
self._start_block(f'\n{admonition.block_markdown}\n')
614686
return IBlockBeginning(remainder='')
615687

616688

tests/test_rst.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def func(): pass
243243
244244
245245
---
246-
**Note**
246+
🛈 **Note**
247247
248248
The `chararray` class exists for backwards compatibility with
249249
Numarray, it is not recommended for new development.
@@ -399,7 +399,7 @@ def func(): pass
399399
400400
401401
---
402-
**Warning**
402+
⚠️ **Warning**
403403
404404
Loading pickled data received from untrusted sources can be
405405
unsafe.
@@ -421,7 +421,7 @@ def func(): pass
421421
LINE_WARNING_MARKDOWN = """
422422
Create a view into the array with the given shape and strides.
423423
424-
**Warning**: This function has to be used with extreme care, see notes.
424+
⚠️ **Warning**: This function has to be used with extreme care, see notes.
425425
426426
Parameters
427427
"""
@@ -462,7 +462,7 @@ def func(): pass
462462
"""
463463

464464
SIMPLE_TABLE_MARKDOWN = """
465-
**Warning**: This is not a standard simple table
465+
⚠️ **Warning**: This is not a standard simple table
466466
467467
| Character | Meaning |
468468
| --------- | --------------------------------------------------------------- |
@@ -483,7 +483,7 @@ def func(): pass
483483
"""
484484

485485
SIMPLE_TABLE_2_MARKDOWN = """
486-
**Warning**: This is a standard simple table
486+
⚠️ **Warning**: This is a standard simple table
487487
488488
| A | B | A and B |
489489
| ----- | ----- | ------- |

0 commit comments

Comments
 (0)