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
33 changes: 27 additions & 6 deletions docling_core/transforms/deserializer/doclang.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ def _dispatch_element(self, *, doc: DoclingDocument, el: Element, parent: NodeIt
self._parse_picture(doc=doc, el=el, parent=parent)
elif host_el.tagName == DocLangToken.CODE.value:
self._parse_code_group(doc=doc, el=el, code_el=host_el, parent=parent)
elif host_el.tagName == DocLangToken.TRACK.value:
_, footnotes = self._extract_satellites(doc=doc, el=el)
self._parse_track(doc=doc, el=host_el, parent=parent, footnotes=footnotes)
else:
self._parse_table(doc=doc, el=el, parent=parent)
else:
Expand Down Expand Up @@ -1846,8 +1849,18 @@ def _parse_picture_body(self, *, doc: DoclingDocument, picture_el: Element, pic:
DocLangToken.MSECS.value: 1,
}

def _parse_track(self, *, doc: DoclingDocument, el: Element, parent: NodeItem | None) -> None:
"""Parse ``<track>`` into a ``TrackItem`` with a cue item per frame and transcript turn."""
def _parse_track(
self,
*,
doc: DoclingDocument,
el: Element,
parent: NodeItem | None,
footnotes: list[TextItem] | None = None,
) -> None:
"""Parse ``<track>`` into a ``TrackItem`` with a cue item per frame and transcript turn.

``footnotes`` are the ``<footnote>`` siblings of the track in its float group.
"""
captions = self._extract_captions(doc=doc, el=el)
prov_list = self._extract_provenance(doc=doc, el=el)
track = doc.add_track(
Expand All @@ -1857,7 +1870,7 @@ def _parse_track(self, *, doc: DoclingDocument, el: Element, parent: NodeItem |
)
for prov in prov_list[1:]:
track.prov.append(prov)
self._attach_satellites(item=track, captions=captions, footnotes=[])
self._attach_satellites(item=track, captions=captions, footnotes=footnotes or [])
self._source_recorder.bind_item(el, track)
self._apply_custom_meta_from_element(item=track, el=el)

Expand Down Expand Up @@ -2170,14 +2183,15 @@ def _float_group_host(self, el: Element) -> Element | None:
they are emitted as ``<footnote>`` siblings of the host inside an unnamed
``<group>``. ``<text>`` siblings are read as extra captions, as emitted before
DocLang 0.8 allowed several captions in the element head. Such a group wraps exactly one
``<picture>``, ``<table>``, ``<index>`` or ``<code>`` and nothing but satellites
besides it.
``<picture>``, ``<table>``, ``<index>``, ``<code>`` or ``<track>`` and nothing but
satellites besides it. A ``<track>`` only takes ``<footnote>`` satellites.
"""
host_tags = {
DocLangToken.PICTURE.value,
DocLangToken.TABLE.value,
DocLangToken.INDEX.value,
DocLangToken.CODE.value,
DocLangToken.TRACK.value,
}
satellite_tags = {DocLangToken.TEXT.value, DocLangToken.FOOTNOTE.value, DocLangToken.CAPTION.value}
hosts: list[Element] = []
Expand All @@ -2188,7 +2202,14 @@ def _float_group_host(self, el: Element) -> Element | None:
hosts.append(node)
elif node.tagName not in satellite_tags:
return None
return hosts[0] if len(hosts) == 1 else None
if len(hosts) != 1:
return None
if hosts[0].tagName == DocLangToken.TRACK.value and any(
isinstance(node, Element) and node.tagName in {DocLangToken.TEXT.value, DocLangToken.CAPTION.value}
for node in el.childNodes
):
return None
return hosts[0]

def _extract_satellites(self, *, doc: DoclingDocument, el: Element) -> tuple[list[TextItem], list[TextItem]]:
"""Extract the satellites of a float group: ``<text>`` as captions, ``<footnote>`` as footnotes."""
Expand Down
14 changes: 12 additions & 2 deletions docling_core/transforms/serializer/doclang.py
Original file line number Diff line number Diff line change
Expand Up @@ -2294,10 +2294,20 @@ def _serialize_track(
**kwargs,
)
inner = head + cover + cues
if not inner and params.suppress_empty_elements:
before, after = _serialize_floating_satellites(
item=item, doc_serializer=doc_serializer, doc=doc, params=params, **kwargs
)
if not inner and not (before or after) and params.suppress_empty_elements:
_release_track(doc_serializer, item.self_ref)
return create_ser_result()
return create_ser_result(text=_wrap(text=inner, wrap_tag=DocLangToken.TRACK.value), span_source=item)
track_text = _wrap(text=inner, wrap_tag=DocLangToken.TRACK.value)
if before or after:
# Wrap the host with its satellites so that they can be re-attached on deserialization.
track_text = _wrap(
text="".join(r.text for r in before) + track_text + "".join(r.text for r in after),
wrap_tag=DocLangToken.GROUP.value,
)
return create_ser_result(text=track_text, span_source=[create_ser_result(span_source=item), *before, *after])


def _claim_track(doc_serializer: BaseDocSerializer, key: str) -> None:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_doclang_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,26 @@ def _track_with_cue(doc: DoclingDocument, **track_kwargs) -> TrackItem:
return track


def test_track_footnotes_are_written_and_read_back():
doc = DoclingDocument(name="doc")
track = _track_with_cue(doc)
for text in ("First note", "Second note"):
track.footnotes.append(doc.add_text(label=DocItemLabel.FOOTNOTE, text=text).get_ref())

xml = DocLangDocSerializer(doc=doc).serialize().text
validate_dclg_xml(xml)
assert "<group>" in xml
assert "<footnote>First note</footnote>" in xml
assert "<footnote>Second note</footnote>" in xml

back = DocLangDocDeserializer().deserialize_str(xml)
assert len(back.tracks) == 1
footnotes = [ref.resolve(back) for ref in back.tracks[0].footnotes]
assert [note.text for note in footnotes] == ["First note", "Second note"]
assert all(note.label == DocItemLabel.FOOTNOTE for note in footnotes)
assert DocLangDocSerializer(doc=back).serialize().text == xml


def test_cues_take_the_layer_of_their_track():
doc = DocLangDocDeserializer().deserialize_str(
'<doclang version="0.10"><track><layer value="furniture"/><bdiv/><seconds value="0"/>hi</track></doclang>'
Expand Down
1 change: 1 addition & 0 deletions tests/test_doclang_track_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ def _sec(value: int, msecs: int | None = None) -> str:
"between_texts": (_doc(f"<text>Before</text><track><bdiv/>{_sec(1)}Hello</track><text>After</text>"), None),
"two_tracks": (_doc(f"<track><bdiv/>{_sec(1)}A</track><track><bdiv/>{_sec(1)}B</track>"), None),
"in_group_with_text": (_doc(f"<group><text>Intro</text><track><bdiv/>{_sec(1)}Hello</track></group>"), _GROUP),
"track_footnote": (_doc(f"<group><track><bdiv/>{_sec(1)}Hello</track><footnote>Note</footnote></group>"), None),
"in_table_cell": (_doc(f"<table><fcel/><track><bdiv/>{_sec(1)}Hello</track><nl/></table>"), None),
# 8. Rich transcripts
"bold_turn": (_track(f"<bdiv/>{_sec(1)}<bold>Hello</bold> there"), _RICH),
Expand Down
Loading