For each index of `data`, return the index just past the `]` which closes an opening bracket before that index, or `None` if the bracket never closes.
(data: str)
| 1290 | |
| 1291 | @staticmethod |
| 1292 | def _findBracketEnds(data: str) -> list[int | None]: |
| 1293 | """ |
| 1294 | For each index of `data`, return the index just past the `]` which closes an |
| 1295 | opening bracket before that index, or `None` if the bracket never closes. |
| 1296 | |
| 1297 | """ |
| 1298 | ends: list[int | None] = [None] * (len(data) + 1) |
| 1299 | depth = 0 |
| 1300 | # `stack` holds the indexes still waiting for a closing bracket. The bracket |
| 1301 | # depth before each one is strictly increasing from bottom to top. |
| 1302 | stack: list[tuple[int, int]] = [] |
| 1303 | for pos in range(len(data) + 1): |
| 1304 | while stack and depth < stack[-1][1]: |
| 1305 | ends[stack.pop()[0]] = pos |
| 1306 | stack.append((pos, depth)) |
| 1307 | if pos < len(data): |
| 1308 | if data[pos] == '[': |
| 1309 | depth += 1 |
| 1310 | elif data[pos] == ']': |
| 1311 | depth -= 1 |
| 1312 | return ends |
| 1313 | |
| 1314 | |
| 1315 | class ImageInlineProcessor(LinkInlineProcessor): |