Parse data between `()` of `[Text]()` allowing recursive `()`.
(self, data: str, index: int)
| 1158 | return el, m.start(0), index |
| 1159 | |
| 1160 | def getLink(self, data: str, index: int) -> tuple[str, str | None, int, bool]: |
| 1161 | """Parse data between `()` of `[Text]()` allowing recursive `()`. """ |
| 1162 | |
| 1163 | href = '' |
| 1164 | title: str | None = None |
| 1165 | handled = False |
| 1166 | |
| 1167 | m = self.RE_LINK.match(data, pos=index) |
| 1168 | if m and m.group(1): |
| 1169 | # Matches [Text](<link> "title") |
| 1170 | href = m.group(1)[1:-1].strip() |
| 1171 | if m.group(2): |
| 1172 | title = m.group(2)[1:-1] |
| 1173 | index = m.end(0) |
| 1174 | handled = True |
| 1175 | elif m: |
| 1176 | # Track bracket nesting and index in string |
| 1177 | bracket_count = 1 |
| 1178 | backtrack_count = 1 |
| 1179 | start_index = m.end() |
| 1180 | index = start_index |
| 1181 | last_bracket = -1 |
| 1182 | |
| 1183 | # Primary (first found) quote tracking. |
| 1184 | quote: str | None = None |
| 1185 | start_quote = -1 |
| 1186 | exit_quote = -1 |
| 1187 | ignore_matches = False |
| 1188 | |
| 1189 | # Secondary (second found) quote tracking. |
| 1190 | alt_quote = None |
| 1191 | start_alt_quote = -1 |
| 1192 | exit_alt_quote = -1 |
| 1193 | |
| 1194 | # Track last character |
| 1195 | last = '' |
| 1196 | |
| 1197 | for pos in range(index, len(data)): |
| 1198 | c = data[pos] |
| 1199 | if c == '(': |
| 1200 | # Count nested ( |
| 1201 | # Don't increment the bracket count if we are sure we're in a title. |
| 1202 | if not ignore_matches: |
| 1203 | bracket_count += 1 |
| 1204 | elif backtrack_count > 0: |
| 1205 | backtrack_count -= 1 |
| 1206 | elif c == ')': |
| 1207 | # Match nested ) to ( |
| 1208 | # Don't decrement if we are sure we are in a title that is unclosed. |
| 1209 | if ((exit_quote != -1 and quote == last) or (exit_alt_quote != -1 and alt_quote == last)): |
| 1210 | bracket_count = 0 |
| 1211 | elif not ignore_matches: |
| 1212 | bracket_count -= 1 |
| 1213 | elif backtrack_count > 0: |
| 1214 | backtrack_count -= 1 |
| 1215 | # We've found our backup end location if the title doesn't resolve. |
| 1216 | if backtrack_count == 0: |
| 1217 | last_bracket = index + 1 |
no test coverage detected