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