MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / BlockQuoteProcessor

Class BlockQuoteProcessor

markdown/blockprocessors.py:288–328  ·  view source on GitHub ↗

Process blockquotes.

Source from the content-addressed store, hash-verified

286
287
288class BlockQuoteProcessor(BlockProcessor):
289 """ Process blockquotes. """
290
291 RE = re.compile(r'(^|\n)[ ]{0,3}>[ ]?(.*)')
292
293 def test(self, parent: etree.Element, block: str) -> bool:
294 return bool(self.RE.search(block)) and not util.nearing_recursion_limit()
295
296 def run(self, parent: etree.Element, blocks: list[str]) -> None:
297 block = blocks.pop(0)
298 m = self.RE.search(block)
299 if m:
300 before = block[:m.start()] # Lines before blockquote
301 # Pass lines before blockquote in recursively for parsing first.
302 self.parser.parseBlocks(parent, [before])
303 # Remove `> ` from beginning of each line.
304 block = '\n'.join(
305 [self.clean(line) for line in block[m.start():].split('\n')]
306 )
307 sibling = self.lastChild(parent)
308 if sibling is not None and sibling.tag == "blockquote":
309 # Previous block was a blockquote so set that as this blocks parent
310 quote = sibling
311 else:
312 # This is a new blockquote. Create a new parent element.
313 quote = etree.SubElement(parent, 'blockquote')
314 # Recursively parse block with blockquote as parent.
315 # change parser state so blockquotes embedded in lists use `p` tags
316 self.parser.state.set('blockquote')
317 self.parser.parseChunk(quote, block)
318 self.parser.state.reset()
319
320 def clean(self, line: str) -> str:
321 """ Remove `>` from beginning of a line. """
322 m = self.RE.match(line)
323 if line.strip() == ">":
324 return ""
325 elif m:
326 return m.group(2)
327 else:
328 return line
329
330
331class OListProcessor(BlockProcessor):

Callers 1

build_block_parserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected