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
8 changes: 3 additions & 5 deletions docling/backend/xml/jats_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,16 +908,13 @@ def _add_figure_captions(
)

caption_node = node.xpath("caption")
caption: str | None
caption: str = ""
if len(caption_node) > 0:
caption = ""
for caption_par in list(caption_node[0]):
if caption_par.xpath(".//supplementary-material"):
continue
caption += JatsDocumentBackend._get_text(caption_par).strip() + " "
caption = caption.strip()
else:
caption = None

# TODO: format label vs caption once styling is supported
fig_text: str = f"{label}{' ' if label and caption else ''}{caption}"
Expand Down Expand Up @@ -1178,7 +1175,8 @@ def _add_tables(

# Label
if len(node.xpath("label")) > 0:
table["label"] = node.xpath("label")[0].text
# lxml sets .text to None for an empty element; fall back to "".
table["label"] = node.xpath("label")[0].text or ""

try:
self._add_table(doc, parent, table)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_backend_jats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,3 +1104,31 @@ def test_jats_empty_article_title_does_not_crash():
exported = doc.export_to_markdown()
# Empty title is serialized as an H1 with no text.
assert exported == "# "


def _caption_texts(doc: DoclingDocument) -> list[str]:
return [t.text for t in doc.texts if t.label == DocItemLabel.CAPTION]


def test_jats_labelled_figure_without_a_caption_omits_the_word_none():
"""A <fig> may carry a <label> and no <caption>, which is legal and common."""
doc = convert_jats_body(
"""<sec><p>Text.</p>
<fig id="f1"><label>Figure 1.</label><graphic/></fig>
</sec>"""
)

assert _caption_texts(doc) == ["Figure 1."]


def test_jats_table_with_an_empty_label_produces_no_caption():
"""An empty <label/> has .text of None in lxml."""
doc = convert_jats_body(
"""<sec><p>Text.</p>
<table-wrap id="t1"><label/>
<table><tbody><tr><td>A</td><td>B</td></tr></tbody></table>
</table-wrap>
</sec>"""
)

assert _caption_texts(doc) == []
Loading