Skip to content

fix: HTMLTableSerializer efficiency and dir attribute #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions docling_core/transforms/serializer/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,18 @@ def serialize(

if item.self_ref not in doc_serializer.get_excluded_refs(**kwargs):
body = ""
row_idx = 0

for i in range(nrows):
# Iterate over rows directly
for row in item.data.grid:
if row_idx >= nrows:
break
body += "<tr>"
for j in range(ncols):
cell: TableCell = item.data.grid[i][j]
col_idx = 0
# Iterate over cells in the row
for cell in row:
if col_idx >= ncols:
break

rowspan, rowstart = (
cell.row_span,
Expand All @@ -324,9 +331,11 @@ def serialize(
cell.start_col_offset_idx,
)

if rowstart != i:
if rowstart != row_idx:
col_idx += 1
continue
if colstart != j:
if colstart != col_idx:
col_idx += 1
continue

content = html.escape(cell.text.strip())
Expand All @@ -342,10 +351,12 @@ def serialize(

text_dir = get_text_direction(content)
if text_dir == "rtl":
opening_tag += f' dir="{dir}"'
opening_tag += f' dir="{text_dir}"'

body += f"<{opening_tag}>{content}</{celltag}>"
col_idx += 1
body += "</tr>"
row_idx += 1

if body:
body = f"<tbody>{body}</tbody>"
Expand Down