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

Class ReferenceProcessor

markdown/blockprocessors.py:577–613  ·  view source on GitHub ↗

Process link references.

Source from the content-addressed store, hash-verified

575
576
577class ReferenceProcessor(BlockProcessor):
578 """ Process link references. """
579 RE = re.compile(
580 r'^[ ]{0,3}\[([^\[\]]*)\]:[ ]*(?:\n[ ]*)?([^\s]+)[ ]*(?:\n[ ]*)?((["\'])(.*)\4[ ]*|\((.*)\)[ ]*)?$',
581 re.MULTILINE
582 )
583
584 def test(self, parent: etree.Element, block: str) -> bool:
585 return True
586
587 def run(self, parent: etree.Element, blocks: list[str]) -> bool:
588 block = blocks.pop(0)
589 m = self.RE.search(block)
590 if m:
591 start = end = m.start()
592 # Record the definitions on the following lines in this pass rather
593 # than one per pass through the block processors. A definition after
594 # the first which spans lines is left for the next pass, as another
595 # processor may claim part of it.
596 for m in self.RE.finditer(block, start):
597 if m.start() > start and (block[end:m.start()].strip('\n') or '\n' in m.group()):
598 break
599 id = m.group(1).strip().lower()
600 link = m.group(2).lstrip('<').rstrip('>')
601 title = m.group(5) or m.group(6)
602 self.parser.md.references[id] = (link, title)
603 end = m.end()
604 if block[end:].strip():
605 # Add any content after match back to blocks as separate block
606 blocks.insert(0, block[end:].lstrip('\n'))
607 if block[:start].strip():
608 # Add any content before match back to blocks as separate block
609 blocks.insert(0, block[:start].rstrip('\n'))
610 return True
611 # No match. Restore block.
612 blocks.insert(0, block)
613 return False
614
615
616class ParagraphProcessor(BlockProcessor):

Callers 1

build_block_parserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected