1. Re-level flat headings: flat '## 1)' -> '### 1)' 2. Convert fake headings (like '## OF CLASS I MEDICAL DEVICES') to normal text.
(content: str)
| 519 | # ── layout fixes (paragraph merging & headings) ────────────────────────────── |
| 520 | |
| 521 | def fix_complex_layouts(content: str) -> tuple[str, int]: |
| 522 | """ |
| 523 | 1. Re-level flat headings: flat '## 1)' -> '### 1)' |
| 524 | 2. Convert fake headings (like '## OF CLASS I MEDICAL DEVICES') to normal text. |
| 525 | """ |
| 526 | lines = content.split('\n') |
| 527 | out_lines = [] |
| 528 | modified = 0 |
| 529 | |
| 530 | import re |
| 531 | |
| 532 | for i, line in enumerate(lines): |
| 533 | striped = line.strip() |
| 534 | |
| 535 | # 1. Demote docling level 2 headings that look like sub-chapters |
| 536 | # e.g. "## 1) Confirm product as a medical device" -> "### 1) ..." |
| 537 | # e.g. "## 1.1 Introduction" -> "### 1.1 Introduction" |
| 538 | if re.match(r'^##\s+[0-9]+[\.\)][\s]+', striped): |
| 539 | line = line.replace('##', '###', 1) |
| 540 | modified += 1 |
| 541 | # Level 4 |
| 542 | elif re.match(r'^##\s+[a-z][\.\)][\s]+', striped): |
| 543 | line = line.replace('##', '####', 1) |
| 544 | modified += 1 |
| 545 | |
| 546 | # 2. Revert obviously fake headings to normal text |
| 547 | # If a heading doesn't start with a letter or number, or is a continuation (starts with prepositions like 'OF ', 'FOR ', 'AND ') |
| 548 | elif re.match(r'^##\s+(OF|FOR|AND|TO|OR|IN|ON|WITH|BY|THE|A|AN)\b', striped, re.IGNORECASE): |
| 549 | line = re.sub(r'^##\s+', '', line) |
| 550 | modified += 1 |
| 551 | |
| 552 | out_lines.append(line) |
| 553 | |
| 554 | return '\n'.join(out_lines), modified |
| 555 | |
| 556 | def fix_messy_bullets(content: str) -> tuple[str, int]: |
| 557 | """ |