Repository navigation
fix(asciidoc): fill a table row from the column count of the first line - #5010
PeterStaar-IBM merged 4 commits into
Conversation
|
✅ DCO Check Passed Thanks @chrikrah, all your commits are properly signed off. 🎉 |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
AI triage (advisory)Checked commit 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 CI safety: no concern found. A maintainer must still approve CI. 2 related pull request(s) in the same code
The |
There was a problem hiding this comment.
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.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
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.matchover-match a duplication spec such as2*?
A maintainer makes the merge decision. This review never approves the pull request.
ceberam
left a comment
There was a problem hiding this comment.
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:
- Column spans (
col_span > 1): In a row with2+|wide |x,wideis placed atstart_col_offset_idx = 0(span 1) andxis placed atstart_col_offset_idx = 1(span 1). Column index 2 in that row is left empty. - Row spans (
row_span > 1): In a table with.2+|tall |x \n |y,tallreceivesrow_span = 1. In the subsequent row,yreceivesstart_col_offset_idx = 0(colliding withtall's column), leaving column index 1 empty. - Downstream export impact: Downstream exporters (Markdown, HTML, JSON) rely on
start_col_offset_idx,start_row_offset_idx,col_span, androw_spaninTableCell. 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
left a comment
There was a problem hiding this comment.
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?
chrikrah
left a comment
There was a problem hiding this comment.
@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]>
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]>
Signed-off-by: Cesar Berrospi Ramis <[email protected]>
472738e to
745cc28
Compare
ceberam
left a comment
There was a problem hiding this comment.
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.
Without a
colsattribute, 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_gridindocling/backend/asciidoc_backend.pymakes a row of every line instead. Example 3 from the basic table page, withoutcolsand cut to three rows, comes out with four one-cell rows: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:
colsis still not read. Acols="3*"table whose header is split over two lines now also splits its next row.No issue exists for this.
Checklist:
Review evidence:
@dolfim-ibm, reading
colswould fix bothcolscases above. I'd send it as a follow-up so this diff stays about rows. Does that work for you?