Skip to content

Commit d12df56

Browse files
authored
Merge pull request #20 from krassowski/full-admonition-support
Complete support for RST admonitions
2 parents d8f2f8b + a568217 commit d12df56

File tree

2 files changed

+81
-11
lines changed

2 files changed

+81
-11
lines changed

docstring_to_markdown/rst.py

Lines changed: 76 additions & 6 deletions
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>'
@@ -605,13 +671,17 @@ def initiate_parsing(self, line: str, current_language: str):
605671

606672
class NoteBlockParser(IndentedBlockParser):
607673
enclosure = '\n---'
608-
directives = {'.. note::', '.. warning::'}
674+
directives = {
675+
f'.. {admonition.name}::': admonition
676+
for admonition in ADMONITIONS
677+
}
609678

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

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

617687

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
| --------- | --------------------------------------------------------------- |
@@ -501,7 +501,7 @@ def func(): pass
501501
"""
502502

503503
SIMPLE_TABLE_2_MARKDOWN = """
504-
**Warning**: This is a standard simple table
504+
⚠️ **Warning**: This is a standard simple table
505505
506506
| A | B | A and B |
507507
| ----- | ----- | ------- |

0 commit comments

Comments
 (0)