@@ -110,26 +110,31 @@ class IParser(ABC):
110
110
111
111
@abstractmethod
112
112
def can_parse (self , line : str ) -> bool :
113
- pass
113
+ """Whether the line looks like a valid beginning of parsed block."""
114
114
115
115
@abstractmethod
116
116
def initiate_parsing (self , line : str , current_language : str ) -> IBlockBeginning :
117
- pass
117
+ """Initiate parsing of given line.
118
+
119
+ Arguments:
120
+ line: first line to be parsed (that passed `can_parse()` test)
121
+ current_language: language to use if highlighting code and no other language is specified in `line`
122
+ """
118
123
119
124
@abstractmethod
120
125
def can_consume (self , line : str ) -> bool :
121
- pass
126
+ """Whether the line can be parsed, or does it look like an end of parsable area?"""
122
127
123
128
@abstractmethod
124
129
def consume (self , line : str ) -> None :
125
- pass
130
+ """Parse given line."""
126
131
127
132
@abstractmethod
128
133
def finish_consumption (self , final : bool ) -> str :
129
- pass
134
+ """Finish parsing and return the converted part of the docstring."""
130
135
131
- def get_follower ( self , line : str ) -> Union [ 'IParser' , None ]:
132
- return None
136
+ """Is there another parser that should follow after this parser finished?"""
137
+ follower : Union [ 'IParser' , None ] = None
133
138
134
139
135
140
class BlockParser (IParser ):
@@ -144,27 +149,12 @@ def __init__(self):
144
149
145
150
@abstractmethod
146
151
def can_parse (self , line : str ) -> bool :
147
- """
148
- All children should call _start_block in initiate_parsing() implementation.
149
- """
150
- pass
151
-
152
- @abstractmethod
153
- def initiate_parsing (
154
- self ,
155
- line : str ,
156
- current_language : str
157
- ) -> IBlockBeginning :
158
- pass
152
+ """All children should call _start_block in initiate_parsing() implementation."""
159
153
160
154
def _start_block (self , language : str ):
161
155
self ._buffer .append (self .enclosure + language )
162
156
self ._block_started = True
163
157
164
- @abstractmethod
165
- def can_consume (self , line : str ) -> bool :
166
- pass
167
-
168
158
def consume (self , line : str ):
169
159
if not self ._block_started :
170
160
raise ValueError ('Block has not started' )
@@ -222,8 +212,7 @@ def can_consume(self, line: str) -> bool:
222
212
return line .strip () != '' and not line .startswith ('>>>' )
223
213
224
214
def can_parse (self , line : str ) -> bool :
225
- # cannot be initiated directly
226
- return False
215
+ return line .strip () != ''
227
216
228
217
def initiate_parsing (self , line : str , current_language : str ) -> IBlockBeginning :
229
218
self ._start_block ('' )
@@ -250,9 +239,7 @@ def _strip_prompt(self, line: str) -> str:
250
239
start = 4 if line .startswith ('>>> ' ) or line .startswith ('... ' ) else 3
251
240
return line [start :]
252
241
253
- def get_follower (self , line : str ) -> Union ['IParser' , None ]:
254
- if line :
255
- return PythonOutputBlockParser ()
242
+ follower = PythonOutputBlockParser ()
256
243
257
244
258
245
class DoubleColonBlockParser (IndentedBlockParser ):
@@ -286,12 +273,13 @@ def initiate_parsing(self, line: str, current_language: str):
286
273
287
274
class NoteBlockParser (IndentedBlockParser ):
288
275
enclosure = '\n ---'
276
+ directives = {'.. note::' , '.. warning::' }
289
277
290
278
def can_parse (self , line : str ):
291
- return line .strip () == '.. note::'
279
+ return line .strip () in self . directives
292
280
293
281
def initiate_parsing (self , line : str , current_language : str ):
294
- self ._start_block ('\n **Note**\n ' )
282
+ self ._start_block ('\n **Note**\n ' if 'note' in line else ' \n **Warning** \n ' )
295
283
return IBlockBeginning (remainder = '' )
296
284
297
285
@@ -378,8 +366,8 @@ def flush_buffer():
378
366
else :
379
367
markdown += flush_buffer ()
380
368
markdown += active_parser .finish_consumption (False )
381
- follower = active_parser .get_follower ( line )
382
- if follower :
369
+ follower = active_parser .follower
370
+ if follower and follower . can_parse ( line ) :
383
371
active_parser = follower
384
372
active_parser .initiate_parsing (line , language )
385
373
else :
0 commit comments