@@ -110,26 +110,31 @@ class IParser(ABC):
110110
111111 @abstractmethod
112112 def can_parse (self , line : str ) -> bool :
113- pass
113+ """Whether the line looks like a valid beginning of parsed block."""
114114
115115 @abstractmethod
116116 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+ """
118123
119124 @abstractmethod
120125 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?"""
122127
123128 @abstractmethod
124129 def consume (self , line : str ) -> None :
125- pass
130+ """Parse given line."""
126131
127132 @abstractmethod
128133 def finish_consumption (self , final : bool ) -> str :
129- pass
134+ """Finish parsing and return the converted part of the docstring."""
130135
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
133138
134139
135140class BlockParser (IParser ):
@@ -144,27 +149,12 @@ def __init__(self):
144149
145150 @abstractmethod
146151 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."""
159153
160154 def _start_block (self , language : str ):
161155 self ._buffer .append (self .enclosure + language )
162156 self ._block_started = True
163157
164- @abstractmethod
165- def can_consume (self , line : str ) -> bool :
166- pass
167-
168158 def consume (self , line : str ):
169159 if not self ._block_started :
170160 raise ValueError ('Block has not started' )
@@ -222,8 +212,7 @@ def can_consume(self, line: str) -> bool:
222212 return line .strip () != '' and not line .startswith ('>>>' )
223213
224214 def can_parse (self , line : str ) -> bool :
225- # cannot be initiated directly
226- return False
215+ return line .strip () != ''
227216
228217 def initiate_parsing (self , line : str , current_language : str ) -> IBlockBeginning :
229218 self ._start_block ('' )
@@ -250,9 +239,7 @@ def _strip_prompt(self, line: str) -> str:
250239 start = 4 if line .startswith ('>>> ' ) or line .startswith ('... ' ) else 3
251240 return line [start :]
252241
253- def get_follower (self , line : str ) -> Union ['IParser' , None ]:
254- if line :
255- return PythonOutputBlockParser ()
242+ follower = PythonOutputBlockParser ()
256243
257244
258245class DoubleColonBlockParser (IndentedBlockParser ):
@@ -286,12 +273,13 @@ def initiate_parsing(self, line: str, current_language: str):
286273
287274class NoteBlockParser (IndentedBlockParser ):
288275 enclosure = '\n ---'
276+ directives = {'.. note::' , '.. warning::' }
289277
290278 def can_parse (self , line : str ):
291- return line .strip () == '.. note::'
279+ return line .strip () in self . directives
292280
293281 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 ' )
295283 return IBlockBeginning (remainder = '' )
296284
297285
@@ -378,8 +366,8 @@ def flush_buffer():
378366 else :
379367 markdown += flush_buffer ()
380368 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 ) :
383371 active_parser = follower
384372 active_parser .initiate_parsing (line , language )
385373 else :
0 commit comments