Remove TOC tables (and their preceding heading) from content.
(content: str)
| 66 | |
| 67 | |
| 68 | def remove_toc_tables(content: str) -> tuple[str, int]: |
| 69 | """Remove TOC tables (and their preceding heading) from content.""" |
| 70 | lines = content.split('\n') |
| 71 | blocks = split_into_table_blocks(lines) |
| 72 | removed = 0 |
| 73 | out_blocks = [] |
| 74 | for btype, blines in blocks: |
| 75 | if btype == 'table' and is_toc_table(blines): |
| 76 | removed += 1 |
| 77 | # Also remove the preceding heading block if it's a TOC heading |
| 78 | if out_blocks: |
| 79 | prev_entry = out_blocks[-1] |
| 80 | prev_type = prev_entry[0] |
| 81 | prev_lines = prev_entry[1] |
| 82 | if prev_type == 'text': |
| 83 | # Strip trailing blank lines, check if last non-blank is a TOC heading |
| 84 | stripped = [l for l in prev_lines if l.strip()] |
| 85 | if stripped and TOC_HEADING_RE.match(stripped[-1]): |
| 86 | # Remove that heading line from the previous block |
| 87 | new_prev = [] |
| 88 | for l in prev_lines: |
| 89 | if l.strip() and TOC_HEADING_RE.match(l): |
| 90 | continue |
| 91 | new_prev.append(l) |
| 92 | out_blocks[-1] = (prev_type, new_prev) |
| 93 | else: |
| 94 | out_blocks.append((btype, blines)) |
| 95 | return '\n'.join(line for (_t, block) in out_blocks for line in block), removed |
| 96 | |
| 97 | |
| 98 | # ── footnote reformatting ───────────────────────────────────────────────────── |
no test coverage detected