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

Method parse_sub_patterns

markdown/inlinepatterns.py:641–700  ·  view source on GitHub ↗

Parses sub patterns. `data`: text to evaluate. `parent`: Parent to attach text and sub elements to. `last`: Last appended child to parent. Can also be None if parent has no children. `idx`: Current pattern index that was used to evaluate the parent.

(
        self, data: str, parent: etree.Element, last: etree.Element | None, idx: int
    )

Source from the content-addressed store, hash-verified

639 return el1
640
641 def parse_sub_patterns(
642 self, data: str, parent: etree.Element, last: etree.Element | None, idx: int
643 ) -> None:
644 """
645 Parses sub patterns.
646
647 `data`: text to evaluate.
648
649 `parent`: Parent to attach text and sub elements to.
650
651 `last`: Last appended child to parent. Can also be None if parent has no children.
652
653 `idx`: Current pattern index that was used to evaluate the parent.
654 """
655
656 offset = 0
657 pos = 0
658
659 length = len(data)
660 while pos < length:
661 # Find the start of potential emphasis or strong tokens
662 if self.compiled_re.match(data, pos):
663 matched = False
664 # See if the we can match an emphasis/strong pattern
665 for index, item in enumerate(self.PATTERNS):
666 # Only evaluate patterns that are after what was used on the parent
667 if index <= idx:
668 continue
669 m = item.pattern.match(data, pos)
670 if m:
671 # Append child nodes to parent
672 # Text nodes should be appended to the last
673 # child if present, and if not, it should
674 # be added as the parent's text node.
675 text = data[offset:m.start(0)]
676 if text:
677 if last is not None:
678 last.tail = text
679 else:
680 parent.text = text
681 el = self.build_element(m, item.builder, item.tags, index)
682 parent.append(el)
683 last = el
684 # Move our position past the matched hunk
685 offset = pos = m.end(0)
686 matched = True
687 if not matched:
688 # We matched nothing, move on to the next character
689 pos += 1
690 else:
691 # Increment position as no potential emphasis start was found.
692 pos += 1
693
694 # Append any leftover text as a text node.
695 text = data[offset:]
696 if text:
697 if last is not None:
698 last.tail = text

Callers 3

build_singleMethod · 0.95
build_doubleMethod · 0.95
build_double2Method · 0.95

Calls 1

build_elementMethod · 0.95

Tested by

no test coverage detected