@@ -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+
159228RST_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
606672class 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
0 commit comments