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