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
)
| 587 | return el1 |
| 588 | |
| 589 | def parse_sub_patterns( |
| 590 | self, data: str, parent: etree.Element, last: etree.Element | None, idx: int |
| 591 | ) -> None: |
| 592 | """ |
| 593 | Parses sub patterns. |
| 594 | |
| 595 | `data`: text to evaluate. |
| 596 | |
| 597 | `parent`: Parent to attach text and sub elements to. |
| 598 | |
| 599 | `last`: Last appended child to parent. Can also be None if parent has no children. |
| 600 | |
| 601 | `idx`: Current pattern index that was used to evaluate the parent. |
| 602 | """ |
| 603 | |
| 604 | offset = 0 |
| 605 | pos = 0 |
| 606 | |
| 607 | length = len(data) |
| 608 | while pos < length: |
| 609 | # Find the start of potential emphasis or strong tokens |
| 610 | if self.compiled_re.match(data, pos): |
| 611 | matched = False |
| 612 | # See if the we can match an emphasis/strong pattern |
| 613 | for index, item in enumerate(self.PATTERNS): |
| 614 | # Only evaluate patterns that are after what was used on the parent |
| 615 | if index <= idx: |
| 616 | continue |
| 617 | m = item.pattern.match(data, pos) |
| 618 | if m: |
| 619 | # Append child nodes to parent |
| 620 | # Text nodes should be appended to the last |
| 621 | # child if present, and if not, it should |
| 622 | # be added as the parent's text node. |
| 623 | text = data[offset:m.start(0)] |
| 624 | if text: |
| 625 | if last is not None: |
| 626 | last.tail = text |
| 627 | else: |
| 628 | parent.text = text |
| 629 | el = self.build_element(m, item.builder, item.tags, index) |
| 630 | parent.append(el) |
| 631 | last = el |
| 632 | # Move our position past the matched hunk |
| 633 | offset = pos = m.end(0) |
| 634 | matched = True |
| 635 | if not matched: |
| 636 | # We matched nothing, move on to the next character |
| 637 | pos += 1 |
| 638 | else: |
| 639 | # Increment position as no potential emphasis start was found. |
| 640 | pos += 1 |
| 641 | |
| 642 | # Append any leftover text as a text node. |
| 643 | text = data[offset:] |
| 644 | if text: |
| 645 | if last is not None: |
| 646 | last.tail = text |
no test coverage detected