# Copyright (c) 2026 LlamaIndex, Inc. All rights reserved.
# Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository).
# This is not open-source software: see the license for permitted uses.
"""Recover list items stored as HTML rows with a bullet-only first cell."""
import re
from .blocks import Block
_TABLE = re.compile(r"\s*
\s*", re.I | re.S)
_ROW = re.compile(r"]*>(.*?)
", re.I | re.S)
_CELL = re.compile(r"<(td|th)\b([^>]*)>(.*?)\1>", re.I | re.S)
_TAG = re.compile(r"<[^>]*>")
_SPAN = re.compile(r"\b(?:rowspan|colspan)\s*=", re.I)
def split_bullet_tables(answer: str, blocks: list[Block]) -> list[Block]:
"""Keep raw content spans so existing cached word indices remain valid.
Every body row must contain exactly a literal bullet cell and a text cell.
An optional first header becomes a heading. Unparsed content or merged body
cells preserve the whole table, avoiding loss of words or table relationships.
"""
result = []
for block in blocks:
if block.type != "table":
result.append(block)
continue
table = _TABLE.fullmatch(answer[block.start : block.end])
if table is None or "= 2:
result.extend(pieces)
else:
result.append(block)
return result