@@ -156,6 +156,75 @@ def __init__(
156
156
]
157
157
158
158
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
+
159
228
RST_DIRECTIVES : List [Directive ] = [
160
229
Directive (
161
230
pattern = r'\.\. versionchanged:: (?P<version>\S+)(?P<end>$|\n)' ,
@@ -169,10 +238,7 @@ def __init__(
169
238
pattern = r'\.\. deprecated:: (?P<version>\S+)(?P<end>$|\n)' ,
170
239
replacement = r'*Deprecated since \g<version>*\g<end>'
171
240
),
172
- Directive (
173
- pattern = r'\.\. warning::' ,
174
- replacement = r'**Warning**:'
175
- ),
241
+ * ADMONITION_DIRECTIVES ,
176
242
Directive (
177
243
pattern = r'\.\. seealso::(?P<short_form>.*)(?P<end>$|\n)' ,
178
244
replacement = r'*See also*\g<short_form>\g<end>'
@@ -604,13 +670,17 @@ def initiate_parsing(self, line: str, current_language: str):
604
670
605
671
class NoteBlockParser (IndentedBlockParser ):
606
672
enclosure = '\n ---'
607
- directives = {'.. note::' , '.. warning::' }
673
+ directives = {
674
+ f'.. { admonition .name } ::' : admonition
675
+ for admonition in ADMONITIONS
676
+ }
608
677
609
678
def can_parse (self , line : str ):
610
679
return line .strip () in self .directives
611
680
612
681
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 ' )
614
684
return IBlockBeginning (remainder = '' )
615
685
616
686
0 commit comments