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

Class HashHeaderProcessor

markdown/blockprocessors.py:466–501  ·  view source on GitHub ↗

Process Hash Headers.

Source from the content-addressed store, hash-verified

464
465
466class HashHeaderProcessor(BlockProcessor):
467 """ Process Hash Headers. """
468
469 # Detect a header at start of any line in block
470 RE = re.compile(r'(?:^|\n)(?P<level>#{1,6})(?P<header>(?:\\.|[^\\#]|#+(?=[^#\n]))*?)#*(?:\n|$)')
471
472 def test(self, parent: etree.Element, block: str) -> bool:
473 # Check for a line starting with `#` before running the regex, which is
474 # slow to scan long blocks.
475 return (block.startswith('#') or '\n#' in block) and bool(self.RE.search(block))
476
477 def run(self, parent: etree.Element, blocks: list[str]) -> None:
478 block = blocks.pop(0)
479 m = self.RE.search(block)
480 if m:
481 before = block[:m.start()] # All lines before header
482 after = block[m.end():] # All lines after header
483 if before:
484 # As the header was not the first line of the block and the
485 # lines before the header must be parsed first,
486 # recursively parse this lines as a block.
487 self.parser.parseBlocks(parent, [before])
488 # Create header using named groups from RE
489 h = etree.SubElement(parent, 'h%d' % len(m.group('level')))
490 h.text = m.group('header').strip()
491 if after:
492 # Insert remaining lines as first block for future parsing.
493 if self.parser.state.isstate('looselist'):
494 # This is a weird edge case where a header is a child of a loose list
495 # and there is no blank line after the header. To ensure proper
496 # parsing, the line(s) after need to be detabbed. See #1443.
497 after = self.looseDetab(after)
498 blocks.insert(0, after)
499 else: # pragma: no cover
500 # This should never happen, but just in case...
501 logger.warn("We've got a problem header: %r" % block)
502
503
504class SetextHeaderProcessor(BlockProcessor):

Callers 1

build_block_parserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected