Parse data between `()` of `[Text]()` allowing recursive `()`.
(self, data: str, index: int)
| 714 | return el, m.start(0), index |
| 715 | |
| 716 | def getLink(self, data: str, index: int) -> tuple[str, str | None, int, bool]: |
| 717 | """Parse data between `()` of `[Text]()` allowing recursive `()`. """ |
| 718 | |
| 719 | href = '' |
| 720 | title: str | None = None |
| 721 | handled = False |
| 722 | |
| 723 | m = self.RE_LINK.match(data, pos=index) |
| 724 | if m and m.group(1): |
| 725 | # Matches [Text](<link> "title") |
| 726 | href = m.group(1)[1:-1].strip() |
| 727 | if m.group(2): |
| 728 | title = m.group(2)[1:-1] |
| 729 | index = m.end(0) |
| 730 | handled = True |
| 731 | elif m: |
| 732 | # Track bracket nesting and index in string |
| 733 | bracket_count = 1 |
| 734 | backtrack_count = 1 |
| 735 | start_index = m.end() |
| 736 | index = start_index |
| 737 | last_bracket = -1 |
| 738 | |
| 739 | # Primary (first found) quote tracking. |
| 740 | quote: str | None = None |
| 741 | start_quote = -1 |
| 742 | exit_quote = -1 |
| 743 | ignore_matches = False |
| 744 | |
| 745 | # Secondary (second found) quote tracking. |
| 746 | alt_quote = None |
| 747 | start_alt_quote = -1 |
| 748 | exit_alt_quote = -1 |
| 749 | |
| 750 | # Track last character |
| 751 | last = '' |
| 752 | |
| 753 | for pos in range(index, len(data)): |
| 754 | c = data[pos] |
| 755 | if c == '(': |
| 756 | # Count nested ( |
| 757 | # Don't increment the bracket count if we are sure we're in a title. |
| 758 | if not ignore_matches: |
| 759 | bracket_count += 1 |
| 760 | elif backtrack_count > 0: |
| 761 | backtrack_count -= 1 |
| 762 | elif c == ')': |
| 763 | # Match nested ) to ( |
| 764 | # Don't decrement if we are sure we are in a title that is unclosed. |
| 765 | if ((exit_quote != -1 and quote == last) or (exit_alt_quote != -1 and alt_quote == last)): |
| 766 | bracket_count = 0 |
| 767 | elif not ignore_matches: |
| 768 | bracket_count -= 1 |
| 769 | elif backtrack_count > 0: |
| 770 | backtrack_count -= 1 |
| 771 | # We've found our backup end location if the title doesn't resolve. |
| 772 | if backtrack_count == 0: |
| 773 | last_bracket = index + 1 |
no test coverage detected