Parse data between `()` of `[Text]()` allowing recursive `()`.
(self, data: str, index: int)
| 1065 | return el, m.start(0), index |
| 1066 | |
| 1067 | def getLink(self, data: str, index: int) -> tuple[str, str | None, int, bool]: |
| 1068 | """Parse data between `()` of `[Text]()` allowing recursive `()`. """ |
| 1069 | |
| 1070 | href = '' |
| 1071 | title: str | None = None |
| 1072 | handled = False |
| 1073 | |
| 1074 | m = self.RE_LINK.match(data, pos=index) |
| 1075 | if m and m.group(1): |
| 1076 | # Matches [Text](<link> "title") |
| 1077 | href = m.group(1)[1:-1].strip() |
| 1078 | if m.group(2): |
| 1079 | title = m.group(2)[1:-1] |
| 1080 | index = m.end(0) |
| 1081 | handled = True |
| 1082 | elif m: |
| 1083 | # Track bracket nesting and index in string |
| 1084 | bracket_count = 1 |
| 1085 | backtrack_count = 1 |
| 1086 | start_index = m.end() |
| 1087 | index = start_index |
| 1088 | last_bracket = -1 |
| 1089 | |
| 1090 | # Primary (first found) quote tracking. |
| 1091 | quote: str | None = None |
| 1092 | start_quote = -1 |
| 1093 | exit_quote = -1 |
| 1094 | ignore_matches = False |
| 1095 | |
| 1096 | # Secondary (second found) quote tracking. |
| 1097 | alt_quote = None |
| 1098 | start_alt_quote = -1 |
| 1099 | exit_alt_quote = -1 |
| 1100 | |
| 1101 | # Track last character |
| 1102 | last = '' |
| 1103 | |
| 1104 | for pos in range(index, len(data)): |
| 1105 | c = data[pos] |
| 1106 | if c == '(': |
| 1107 | # Count nested ( |
| 1108 | # Don't increment the bracket count if we are sure we're in a title. |
| 1109 | if not ignore_matches: |
| 1110 | bracket_count += 1 |
| 1111 | elif backtrack_count > 0: |
| 1112 | backtrack_count -= 1 |
| 1113 | elif c == ')': |
| 1114 | # Match nested ) to ( |
| 1115 | # Don't decrement if we are sure we are in a title that is unclosed. |
| 1116 | if ((exit_quote != -1 and quote == last) or (exit_alt_quote != -1 and alt_quote == last)): |
| 1117 | bracket_count = 0 |
| 1118 | elif not ignore_matches: |
| 1119 | bracket_count -= 1 |
| 1120 | elif backtrack_count > 0: |
| 1121 | backtrack_count -= 1 |
| 1122 | # We've found our backup end location if the title doesn't resolve. |
| 1123 | if backtrack_count == 0: |
| 1124 | last_bracket = index + 1 |
no test coverage detected