Skip to content

Commit a568217

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

File tree

2 files changed

+81
-11
lines changed

2 files changed

+81
-11
lines changed

Diff for: docstring_to_markdown/rst.py

+76-6
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,75 @@ 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+
return f'{self.icon} **{self.label}**'
168+
169+
@property
170+
def inline_markdown(self):
171+
return self.block_markdown + ':'
172+
173+
174+
ADMONITIONS = [
175+
Admonition(
176+
name='caution',
177+
label='Caution',
178+
icon='⚠️ '
179+
),
180+
Admonition(
181+
name='attention',
182+
label='Attention',
183+
icon='⚠️ '
184+
),
185+
Admonition(
186+
name='danger',
187+
label='Danger',
188+
icon='⚠️ '
189+
),
190+
Admonition(
191+
name='hint',
192+
label='Hint',
193+
icon='🛈'
194+
),
195+
Admonition(
196+
name='important',
197+
label='Important',
198+
icon='⚠️ '
199+
),
200+
Admonition(
201+
name='note',
202+
label='Note',
203+
icon='🛈'
204+
),
205+
Admonition(
206+
name='tip',
207+
label='Tip',
208+
icon='🛈'
209+
),
210+
Admonition(
211+
name='warning',
212+
label='Warning',
213+
icon='⚠️ '
214+
)
215+
]
216+
217+
218+
ADMONITION_DIRECTIVES: List[Directive] = [
219+
# https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions
220+
Directive(
221+
pattern=rf'\.\. {admonition.name}::',
222+
replacement=admonition.inline_markdown
223+
)
224+
for admonition in ADMONITIONS
225+
]
226+
227+
159228
RST_DIRECTIVES: List[Directive] = [
160229
Directive(
161230
pattern=r'\.\. versionchanged:: (?P<version>\S+)(?P<end>$|\n)',
@@ -169,10 +238,7 @@ def __init__(
169238
pattern=r'\.\. deprecated:: (?P<version>\S+)(?P<end>$|\n)',
170239
replacement=r'*Deprecated since \g<version>*\g<end>'
171240
),
172-
Directive(
173-
pattern=r'\.\. warning::',
174-
replacement=r'**Warning**:'
175-
),
241+
*ADMONITION_DIRECTIVES,
176242
Directive(
177243
pattern=r'\.\. seealso::(?P<short_form>.*)(?P<end>$|\n)',
178244
replacement=r'*See also*\g<short_form>\g<end>'
@@ -604,13 +670,17 @@ def initiate_parsing(self, line: str, current_language: str):
604670

605671
class NoteBlockParser(IndentedBlockParser):
606672
enclosure = '\n---'
607-
directives = {'.. note::', '.. warning::'}
673+
directives = {
674+
f'.. {admonition.name}::': admonition
675+
for admonition in ADMONITIONS
676+
}
608677

609678
def can_parse(self, line: str):
610679
return line.strip() in self.directives
611680

612681
def initiate_parsing(self, line: str, current_language: str):
613-
self._start_block('\n**Note**\n' if 'note' in line else '\n**Warning**\n')
682+
admonition = self.directives[line.strip()]
683+
self._start_block(f'\n{admonition.block_markdown}\n')
614684
return IBlockBeginning(remainder='')
615685

616686

Diff for: tests/test_rst.py

+5-5
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)