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

Class CodeBlockProcessor

markdown/blockprocessors.py:255–285  ·  view source on GitHub ↗

Process code blocks.

Source from the content-addressed store, hash-verified

253
254
255class CodeBlockProcessor(BlockProcessor):
256 """ Process code blocks. """
257
258 def test(self, parent: etree.Element, block: str) -> bool:
259 return block.startswith(' '*self.tab_length)
260
261 def run(self, parent: etree.Element, blocks: list[str]) -> None:
262 sibling = self.lastChild(parent)
263 block = blocks.pop(0)
264 theRest = ''
265 if (sibling is not None and sibling.tag == "pre" and
266 len(sibling) and sibling[0].tag == "code"):
267 # The previous block was a code block. As blank lines do not start
268 # new code blocks, append this block to the previous, adding back
269 # line breaks removed from the split into a list.
270 code = sibling[0]
271 block, theRest = self.detab(block)
272 code.text = util.AtomicString(
273 '{}\n{}\n'.format(code.text, util.code_escape(block.rstrip()))
274 )
275 else:
276 # This is a new code block. Create the elements and insert text.
277 pre = etree.SubElement(parent, 'pre')
278 code = etree.SubElement(pre, 'code')
279 block, theRest = self.detab(block)
280 code.text = util.AtomicString('%s\n' % util.code_escape(block.rstrip()))
281 if theRest:
282 # This block contained unindented line(s) after the first indented
283 # line. Insert these lines as the first block of the master blocks
284 # list for future processing.
285 blocks.insert(0, theRest)
286
287
288class BlockQuoteProcessor(BlockProcessor):

Callers 1

build_block_parserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected