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

Class ReferenceProcessor

markdown/blockprocessors.py:588–624  ·  view source on GitHub ↗

Process link references.

Source from the content-addressed store, hash-verified

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

Callers 1

build_block_parserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected