diff --git a/docling/backend/asciidoc_backend.py b/docling/backend/asciidoc_backend.py index 63c984b44b..5330cf1475 100644 --- a/docling/backend/asciidoc_backend.py +++ b/docling/backend/asciidoc_backend.py @@ -318,6 +318,11 @@ def _parse(self, doc: DoclingDocument): if not in_list: in_list = True + # The list ends the paragraph before it, which would + # otherwise be added later, inside the list. + text_data = self._flush_text_data( + doc=doc, text_data=text_data, parent=parents[level] + ) # GroupItem has no caption slot; emit the pending block # title as a bold paragraph immediately before the list. if caption_data: @@ -387,6 +392,13 @@ def _parse(self, doc: DoclingDocument): in_delimited_table = False table_data = [] else: + # The table ends the paragraph before it, which would + # otherwise be added after the table. + text_data = self._flush_text_data( + doc=doc, + text_data=text_data, + parent=self._get_current_parent(parents), + ) in_table = True in_delimited_table = True diff --git a/tests/test_backend_asciidoc.py b/tests/test_backend_asciidoc.py index 3b227cf375..7b552003cd 100644 --- a/tests/test_backend_asciidoc.py +++ b/tests/test_backend_asciidoc.py @@ -978,6 +978,37 @@ def test_symmetric_titles_drop_their_closing_marks() -> None: ] +def _convert_source(src: bytes) -> DoclingDocument: + in_doc = InputDocument( + path_or_stream=BytesIO(src), + format=InputFormat.ASCIIDOC, + backend=AsciiDocBackend, + filename="flush.adoc", + ) + return in_doc._backend.convert() + + +def test_list_flushes_pending_paragraph() -> None: + # The text before a list was only added at the next blank line, so it + # landed inside the list group, after the list items. + doc = _convert_source(b"Intro:\n* a\n* b\n") + + intro = next(item for item in doc.texts if item.text == "Intro:") + assert intro.parent == doc.body.get_ref() + assert doc.export_to_markdown() == "Intro:\n\n- a\n- b" + + +def test_table_flushes_pending_paragraph() -> None: + # The text before a delimited table was added after the table. + doc = _convert_source(b"Before\n|===\n|a|b\n|===\n") + + assert [item.label for item, _ in doc.iterate_items()] == [ + DocItemLabel.PARAGRAPH, + DocItemLabel.TABLE, + ] + assert doc.export_to_markdown().startswith("Before\n\n| a ") + + def test_block_title_without_floating_target_keeps_content() -> None: # A block title followed by a paragraph used to absorb every following # line as "multiline caption" and drop all of it at EOF.