Skip to content

fix(asciidoc): fill a table row from the column count of the first line - #5010

Merged
PeterStaar-IBM merged 4 commits into
docling-project:mainfrom
chrikrah:fix/asciidoc-table-rows-over-lines
Nov 6, 2026
Merged

PeterStaar-IBM merged 4 commits into
docling-project:mainfrom
chrikrah:fix/asciidoc-table-rows-over-lines

Conversation

@chrikrah

@chrikrah chrikrah commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Without a cols attribute, Asciidoctor takes the column count of a table from its first non-empty line and fills each row with that many cells. One row can span several lines. _populate_table_as_grid in docling/backend/asciidoc_backend.py makes a row of every line instead. Example 3 from the basic table page, without cols and cut to three rows, comes out with four one-cell rows:

|===
|Column 1, header row |Column 2, header row

|Cell in column 1, row 2
|Cell in column 2, row 2

|Cell in column 1, row 3
|Cell in column 2, row 3
|===
$ python repro.py example.adoc        # at 29caea8; the table code is unchanged at 90234a7
| Column 1, header row    | Column 2, header row   |
|-------------------------|------------------------|
| Cell in column 1, row 2 |                        |
| Cell in column 2, row 2 |                        |
| Cell in column 1, row 3 |                        |
| Cell in column 2, row 3 |                        |

With this change the same file gives Asciidoctor's two body rows. The parser counts a 2+ cell as two columns and a .2+ cell into the next row. Cells keep their spans and start at the first free column.

The cost: cols is still not read. A cols="3*" table whose header is split over two lines now also splits its next row.

No issue exists for this.

Checklist:

  • Documentation has been updated, if necessary.
    • No page describes how the backend reads AsciiDoc tables.
  • Examples have been added, if necessary.
    • Not applicable: no new option or API.
  • Tests have been added, if necessary.

Review evidence:

$ python -m pytest -q tests/test_backend_asciidoc.py tests/test_backend_text_encoding.py
104 passed in 8.64s   # rebased on 90234a7, Python 3.14.7, uv sync --locked --extra models-local --extra format-markdown
# baseline 90234a7: 102 passed; the asciidoc_01..05 ground truth is unchanged
# fix reverted, tests kept: 2 failed, 102 passed
# ruff 0.15.12 (uv lock) format --check and check: clean on both files
# on the earlier base 29caea8: row spans read as 1 -> 1 failed (['y', 'z'] != ['y']); ty 0.0.33 same three warnings before and after
# tach 0.35.3 check: All modules validated
# not run: tests/test_input_doc.py (needs pypdfium2), the full suite, prek

@dolfim-ibm, reading cols would fix both cols cases above. I'd send it as a follow-up so this diff stays about rows. Does that work for you?

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @chrikrah, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) asciidoc issue related to asciidoc backend bug Something isn't working labels Oct 25, 2026
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit 745cc288b8. The analysis read the diff only. It did not run code from this pull request.

The PR changes how the AsciiDoc backend builds table rows. It takes the column count from the first table line, groups cells into rows over several lines, and reads column spans, row spans and N* duplication factors. The PR also rewrites the test setup to use a _convert_adoc helper.

CI safety: no concern found. A maintainer must still approve CI.

2 related pull request(s) in the same code
PR State Signal Assessment
#5117 open changes the same code related: It replaces attribute references in the same backend. It edits table code only to pass attributes to _continue_table. It does not change row grouping or spans.
#5039 open changes the same code related: It sets the parent of caption items, including the table caption in _add_table_if_nonempty. It does not change how table cells or rows are built.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI first review (advisory): no blocker found

This review covers the full diff at 62578e3952. It read the code and did not run it.

The PR fixes multi-line table row grouping in the AsciiDoc backend by inferring column count from the first parsed line and tracking rowspan coverage. The logic in _group_table_rows and _parse_table_cells is correct for the stated cases. The one known limit — cols attribute is still not read, so a cols="3*" table split over two header lines mis-groups — is explicitly stated in the PR description and is pre-existing. No blocker found; confirm that the _CELL_SPAN_RE .match() (not .fullmatch()) on a spec like 2* does not over-count a duplication factor as a col-span.

A maintainer makes the merge decision. This review never approves the pull request.

Comment thread docling/backend/asciidoc_backend.py Resolved
@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Oct 25, 2026
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.80952% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/asciidoc_backend.py 98.80% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Oct 28, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI first review (advisory): no blocker found

This review covers the changes since 62578e3952 at b2ece3e796. It read the code and did not run it.

The incremental change adds _CELL_REPEAT_RE to parse N* duplication specifiers and expands them in _group_table_rows before the column-count and row-grouping logic runs. The earlier question about _CELL_SPAN_RE.match over-matching 2* is answered: _CELL_SPAN_RE requires a trailing +, so it cannot match a bare 2*. No new defect found in the incremental changes; one line remains uncovered per Codecov but is non-critical.

Earlier findings

  • answered (question): Does _CELL_SPAN_RE.match over-match a duplication spec such as 2*?

A maintainer makes the merge decision. This review never approves the pull request.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Oct 28, 2026

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is legitimate and reproducible on main. AsciiDoc tables without an explicit cols attribute derive the table column count from the first non-empty line of cells. Subsequent cells flow sequentially to fill each row to that column count. This allows multi-line row formatting (such as one cell per line).

I see a blocking issue though, and I also have few minor comments that should be addressed.

Blocker — _populate_table_as_grid writes hardcoded spans and incorrect column offsets for spanned cells

In _group_table_rows, the parser extracts col_span and row_span and uses them to calculate row boundaries and column widths. However, inside _populate_table_as_grid, TableCell instances are still constructed with hardcoded spans and naive column indices:

for row_idx, row in enumerate(rows):
    for col_idx, source in enumerate(row):
        row_span = 1
        col_span = 1

        cell = TableCell(
            text=source.text,
            row_span=row_span,
            col_span=col_span,
            start_row_offset_idx=row_idx,
            end_row_offset_idx=row_idx + row_span,
            start_col_offset_idx=col_idx,
            end_col_offset_idx=col_idx + col_span,
            column_header=row_idx == 0,
            row_header=False,
        )

This creates corrupted TableData grid layouts whenever cell spans are used:

  1. Column spans (col_span > 1): In a row with 2+|wide |x, wide is placed at start_col_offset_idx = 0 (span 1) and x is placed at start_col_offset_idx = 1 (span 1). Column index 2 in that row is left empty.
  2. Row spans (row_span > 1): In a table with .2+|tall |x \n |y, tall receives row_span = 1. In the subsequent row, y receives start_col_offset_idx = 0 (colliding with tall's column), leaving column index 1 empty.
  3. Downstream export impact: Downstream exporters (Markdown, HTML, JSON) rely on start_col_offset_idx, start_row_offset_idx, col_span, and row_span in TableCell. As a result, exported Markdown/HTML tables for spanned cells contain misaligned data and ghost empty columns.

Suggestion: update _populate_table_as_grid] to compute actual grid column positions (start_col_offset_idx), skipping occupied columns from active row spans and preceding column spans, and set row_span = source.row_span and col_span = source.col_span on the emitted TableCell objects.

test_table_rows_over_several_lines_count_spans masks grid coordinate errors

The test helper _table_rows only groups cells by cell.start_row_offset_idx and collects cell.text:

rows: dict[int, list[str]] = {}
for cell in doc.tables[0].data.table_cells:
    rows.setdefault(cell.start_row_offset_idx, []).append(cell.text)
return [rows[idx] for idx in sorted(rows)]

Because _table_rows ignores start_col_offset_idx, col_span, and row_span, it passed even though the table cells inside TableData had incorrect column coordinates and hardcoded span values of 1.

Suggested Improvement: Extend the test assertions to check start_col_offset_idx, end_col_offset_idx, col_span, and row_span on TableCell objects to verify that the 2D grid structure is correctly formed.

_parse_table_line became unused in runtime code

On main, _parse_table_line was called during document conversion (table_data.append(self._parse_table_line(line))). In this PR, line 408 was changed to call _parse_table_cells(line) instead.

As a result, _parse_table_line was converted into a wrapper returning list[str] that is now only invoked by unit tests. Consider updating those tests to call _parse_table_cells directly and remove _parse_table_line completely.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the bot's .match question: a stacked specifier such as 2*3+ is not AsciiDoc, and Asciidoctor keeps it as cell text. A plain 2* is valid, though, and here it under-counted: _CELL_SPAN_RE needs a +, so 2*|x was one cell and a factor on the first line set the column count one short.

The latest commit keeps the factor on the cell and expands it when grouping rows, so a continuation line reaches every copy. Docling still drops 2*3+ as a specifier, as it did before this PR, because _CELL_SPEC allows stacked factors. Narrowing that to one factor would match Asciidoctor, and I'd keep it out of this diff because no real document writes it. The comment above _CELL_REPEAT_RE says it stays text, so I'll correct that in the next push.

$ node probe_dup.mjs   # asciidoctor.js 3.0.4
first line 2*|x |y       => ["x","x","y"] ["z","p","q"]
2*|x then continuation   => ["A","B","C"] ["x\nmore","x\nmore","y"]
$ node probe_stack2.mjs
2*3+|x |y after |a |b |c |d => [["a","b","c","d\n2*3+","x","y"]]
$ python -c "print(_table_rows(...))"   # same table, docling at this commit
[['a', 'b', 'c', 'd'], ['x', 'y']]
$ python -m pytest tests/test_backend_asciidoc.py -q -o addopts="" -p no:cacheprovider   # Python 3.12, docling imported from the export
64 passed in 1.38s     # 62578e3 + this commit; 62578e3: 63 passed
$ (same, asciidoc_backend.py from 62578e3, new test kept)
E         At index 0 diff: ['x', 'y'] != ['x', 'x', 'y']
1 failed, 63 passed in 1.56s
$ ruff format --check / ruff check on both files   # ruff 0.15.12, the uv.lock pin
2 files already formatted / All checks passed!
$ ty check docling/backend/asciidoc_backend.py   # ty 0.0.33
Found 3 diagnostics    # same 3 at 62578e3
# not run: tests/test_backend_text_encoding.py (needs torch), the full suite

Docling joins a continuation with a space where Asciidoctor keeps the newline, as for any other cell. Should cols reading follow in its own PR, as planned?

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 2, 2026

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ceberam all three are in 472738e; sorry this sat for a few days. Each cell now keeps its spans and starts at the first column that no span covers, so your two examples export correctly:

# export of 472738e (b2ece3e plus the fix), Python 3.12; each input sits in a |A |B table
# input 1, one line:  2+|wide
# input 2, two lines: .2+|tall |x   then   |y
input 1   before: | wide |      |     after: | wide | wide |
input 2   before: | y    |      |     after: | tall | y    |   (row 2)
$ python -m pytest tests/test_backend_asciidoc.py -q -o addopts="" -p no:cacheprovider
65 passed     # 64 at b2ece3e
1 failed      # asciidoc_backend.py from b2ece3e: test_table_spans_take_their_grid_positions
$ ruff check && ruff format --check <both files>   # ruff 0.15.12, the uv.lock pin
All checks passed!
$ ty check docling/backend/asciidoc_backend.py   # ty 0.0.33
Found 3 diagnostics   # same 3 at b2ece3e

The new test asserts row, column and both spans per cell. _parse_table_line is gone, and the _CELL_REPEAT_RE comment is fixed as promised.

Could you take another pass at the new head?

Asciidoctor takes the number of columns from the first line of a table
and fills each row with that many cells, so a row can be written over
several lines. Its basic table example writes the header on one line
and one cell per line after it. Each line became its own row, so the
two body rows of that table came out as four rows with an empty second
column.

Group the cells by that column count. A column span fills its columns
and a row span fills a column of the rows below it. A line that holds a
whole row stays one row, also when it is wider, as before.

Signed-off-by: Christopher Krah <[email protected]>
A "2*" factor writes a cell twice, so "2*|x |y" on the first line of a
table sets three columns in Asciidoctor. The row grouping counted it as
one cell, so the column count came out one short and every row written
over several lines after it was split in the wrong place.

Keep the factor on the cell and expand it when grouping rows, so a line
that continues the cell's text applies to every copy, as in Asciidoctor.
A stacked "2*3+" is not AsciiDoc and still reads as before.

Signed-off-by: Christopher Krah <[email protected]>
chrikrah and others added 2 commits November 6, 2026 14:45
The table grid gave every cell a span of 1 and the column index of its
position in the row, so a "2+" cell left a ghost empty column and a cell
below a ".2+" cell landed on the spanned column. Markdown and HTML exports
showed the data misaligned.

Each cell now carries its column and row span and starts at the first
column that no span to its left and no rowspan from above covers. A
rowspan is cut at the last row of the table. The grid test checks the
coordinates and spans, which the row-text helper could not see.

_parse_table_line had no caller left outside the tests, so the tests read
_parse_table_cells and the wrapper is gone. The comment on stacked
specifiers now says what Docling does with "2*3+".

Signed-off-by: Christopher Krah <[email protected]>
@ceberam
ceberam force-pushed the fix/asciidoc-table-rows-over-lines branch from 472738e to 745cc28 Compare November 6, 2026 14:00
@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 6, 2026

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @chrikrah ! All points addressed and resolved.
I've just seen that with this PR the test file became a bit too long and the CI checks failed. After rebasing to main it would be even bigger. So I've just added a commit that reduces many redundancies to make it fit within the limits.

@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 6, 2026
@PeterStaar-IBM
PeterStaar-IBM merged commit 26cff4d into docling-project:main Nov 6, 2026
34 checks passed
@chrikrah
chrikrah deleted the fix/asciidoc-table-rows-over-lines branch November 9, 2026 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) asciidoc issue related to asciidoc backend bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants