Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 7 additions & 8 deletions docling/backend/asciidoc_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,11 @@ def _add_picture_item(
parents: dict[int, GroupItem | None],
) -> tuple[list[str], bool, ListItem | None]:
"""Add a picture with its pending caption, if any."""
parent = last_list_item if in_list else self._get_current_parent(parents)
caption: Optional[TextItem] = None
if len(caption_data) > 0:
caption = doc.add_text(
text=" ".join(caption_data), label=DocItemLabel.CAPTION
text=" ".join(caption_data), label=DocItemLabel.CAPTION, parent=parent
)

caption_data = []
Expand All @@ -645,11 +646,7 @@ def _add_picture_item(
else None
)
image = self._image_loader.load_image_ref(item["uri"], base_path)
doc.add_picture(
image=image,
caption=caption,
parent=last_list_item if in_list else self._get_current_parent(parents),
)
doc.add_picture(image=image, caption=caption, parent=parent)
return caption_data, in_list, last_list_item

def _add_block(
Expand Down Expand Up @@ -740,11 +737,13 @@ def _add_literal_block(
text_data=text_data,
parent=self._get_current_parent(parents),
)
parent = last_list_item if in_list else self._get_current_parent(parents)
caption: Optional[TextItem] = None
if caption_data:
caption = doc.add_text(
text=" ".join(caption_data),
label=DocItemLabel.CAPTION,
parent=parent,
)
caption_data = []
code_language = (
Expand All @@ -756,7 +755,7 @@ def _add_literal_block(
text=block.text,
code_language=code_language,
caption=caption,
parent=(last_list_item if in_list else self._get_current_parent(parents)),
parent=parent,
)
return text_data, caption_data, in_list, last_list_item, False

Expand Down Expand Up @@ -1020,7 +1019,7 @@ def _add_table_if_nonempty(
caption = None
if caption_data:
caption = doc.add_text(
text=" ".join(caption_data), label=DocItemLabel.CAPTION
text=" ".join(caption_data), label=DocItemLabel.CAPTION, parent=parent
)

data = AsciiDocBackend._populate_table_as_grid(table_data)
Expand Down
4 changes: 2 additions & 2 deletions docling/backend/xml/jats_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ def _add_figure_captions(
# TODO: format label vs caption once styling is supported
fig_text: str = f"{label}{' ' if label and caption else ''}{caption}"
fig_caption: TextItem | None = (
doc.add_text(label=DocItemLabel.CAPTION, text=fig_text)
doc.add_text(label=DocItemLabel.CAPTION, text=fig_text, parent=parent)
if fig_text
else None
)
Expand Down Expand Up @@ -1167,7 +1167,7 @@ def _add_table(
caption = table_xml_component["caption"]
table_text: str = f"{label}{' ' if label and caption else ''}{caption}"
table_caption: TextItem | None = (
doc.add_text(label=DocItemLabel.CAPTION, text=table_text)
doc.add_text(label=DocItemLabel.CAPTION, text=table_text, parent=parent)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Small edge case in _add_table: the caption is created before the data is not None check. parse_table_data returns None when the table contains a nested table, so in that case the caption gets added without its table. Before this change that lone caption ended up at the end of body, but now it sits inside the section with nothing next to it. Maybe move the doc.add_text call inside the if data is not None: block so the caption is only added together with the table? A JATS table-wrap whose table contains another table would make a quick test for it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You're right that the caption is created before the data is not None check. That order is the same as on main, and I think it's the better one to keep. When parse_table_data returns None the table is skipped, and the caption is the only text left from that table-wrap. Moving add_text into the if would drop it from the output entirely, Markdown included, where main still prints it (as the last line of the document).

For a table-wrap whose table contains another table, the tree on this branch is:

section_header: 'One'
  text: 'Before.'
  caption: 'Table 1. Nested.'
  text: 'After.'
section_header: 'Two'
  text: 'Para two.'

On main the same caption is the last child of body, after section "Two". So the lone caption now sits where the table was instead of at the end. Real support for nested tables is the TODO in parse_table_data, and I'd rather keep that out of this PR. If the maintainers prefer to drop the caption when the table is skipped, that's a small change and I can add a test for it.

if table_text
else None
)
Expand Down
542 changes: 271 additions & 271 deletions tests/data/jats/groundtruth/elife-56337.nxml.itxt

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions tests/data/jats/groundtruth/elife-56337.nxml.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,6 @@
"children": [
{
"$ref": "#/texts/0"
},
{
"$ref": "#/texts/16"
},
{
"$ref": "#/texts/17"
},
{
"$ref": "#/texts/27"
},
{
"$ref": "#/texts/30"
},
{
"$ref": "#/texts/44"
},
{
"$ref": "#/texts/52"
}
],
"content_layer": "body",
Expand Down Expand Up @@ -962,9 +944,15 @@
{
"$ref": "#/groups/1"
},
{
"$ref": "#/texts/16"
},
{
"$ref": "#/pictures/0"
},
{
"$ref": "#/texts/17"
},
{
"$ref": "#/tables/0"
},
Expand Down Expand Up @@ -1043,7 +1031,7 @@
{
"self_ref": "#/texts/16",
"parent": {
"$ref": "#/body"
"$ref": "#/texts/11"
},
"children": [],
"content_layer": "body",
Expand All @@ -1055,7 +1043,7 @@
{
"self_ref": "#/texts/17",
"parent": {
"$ref": "#/body"
"$ref": "#/texts/11"
},
"children": [],
"content_layer": "body",
Expand Down Expand Up @@ -1171,6 +1159,9 @@
{
"$ref": "#/texts/26"
},
{
"$ref": "#/texts/27"
},
{
"$ref": "#/pictures/1"
}
Expand All @@ -1197,7 +1188,7 @@
{
"self_ref": "#/texts/27",
"parent": {
"$ref": "#/body"
"$ref": "#/texts/25"
},
"children": [],
"content_layer": "body",
Expand All @@ -1215,6 +1206,9 @@
{
"$ref": "#/texts/29"
},
{
"$ref": "#/texts/30"
},
{
"$ref": "#/pictures/2"
},
Expand Down Expand Up @@ -1244,7 +1238,7 @@
{
"self_ref": "#/texts/30",
"parent": {
"$ref": "#/body"
"$ref": "#/texts/28"
},
"children": [],
"content_layer": "body",
Expand Down Expand Up @@ -1404,6 +1398,9 @@
{
"$ref": "#/texts/43"
},
{
"$ref": "#/texts/44"
},
{
"$ref": "#/pictures/3"
},
Expand Down Expand Up @@ -1463,7 +1460,7 @@
{
"self_ref": "#/texts/44",
"parent": {
"$ref": "#/body"
"$ref": "#/texts/40"
},
"children": [],
"content_layer": "body",
Expand Down Expand Up @@ -1558,6 +1555,9 @@
"$ref": "#/texts/0"
},
"children": [
{
"$ref": "#/texts/52"
},
{
"$ref": "#/tables/1"
},
Expand Down Expand Up @@ -1599,7 +1599,7 @@
{
"self_ref": "#/texts/52",
"parent": {
"$ref": "#/body"
"$ref": "#/texts/51"
},
"children": [],
"content_layer": "body",
Expand Down
Loading
Loading