diff --git a/docling/utils/deepseekocr_utils.py b/docling/utils/deepseekocr_utils.py index 76a39aaca5..f7d8161796 100644 --- a/docling/utils/deepseekocr_utils.py +++ b/docling/utils/deepseekocr_utils.py @@ -158,17 +158,16 @@ def _collect_annotation_content( break ii += 1 else: - # Original logic for other labels + # Collect the block's content lines: consecutive non-empty lines up to the + # next annotation or the first blank line, so continuation lines survive. while i < len(lines): content_line = lines[i].strip() + if re.match(annotation_pattern, content_line): + break if content_line: - if re.match(annotation_pattern, content_line): - break visited_lines.add(i) content_lines.append(lines[i].rstrip()) i += 1 - if label_str not in ["figure", "image"]: - break else: i += 1 if content_lines: diff --git a/tests/test_unlimited_ocr_markdown.py b/tests/test_unlimited_ocr_markdown.py index dd0d44da46..4cabf21c15 100644 --- a/tests/test_unlimited_ocr_markdown.py +++ b/tests/test_unlimited_ocr_markdown.py @@ -71,3 +71,58 @@ def test_parse_builds_document_with_heading_and_table(): def test_response_format_enum_value(): assert ResponseFormat.UNLIMITED_OCR_MARKDOWN.value == "unlimited_ocr_markdown" + + +def test_continuation_lines_survive_in_a_text_block(): + raw = ( + "text[[100,100,900,300]]\n" + "First line\n" + "Second line\n" + "\n" + "text[[100,400,900,500]]\n" + "Next block\n" + ) + + doc = parse_unlimited_ocr_markdown(raw, PAGE_SIZE, page_no=1, filename="page.png") + markdown = doc.export_to_markdown() + + assert "First line" in markdown + assert "Second line" in markdown + assert "Next block" in markdown + texts = [t.text for t in doc.texts] + assert any("First line" in t and "Second line" in t for t in texts) + + +def test_unlimited_ocr_continuation_lines_survive_in_a_text_block(): + raw = ( + "<|det|>text [100, 100, 900, 300]<|/det|>First line\n" + "Second line\n" + "\n" + "<|det|>text [100, 400, 900, 500]<|/det|>Next block\n" + ) + + doc = parse_unlimited_ocr_markdown(raw, PAGE_SIZE, page_no=1, filename="page.png") + texts = [t.text for t in doc.texts] + + assert len(texts) >= 2 + assert any("First line" in t and "Second line" in t for t in texts) + + +def test_annotation_and_blank_line_still_end_a_block(): + raw = ( + "text[[100,100,900,300]]\n" + "First line\n" + "Second line\n" + "\n" + "\n" + "Loose trailing text\n" + "text[[100,400,900,500]]\n" + "Next block\n" + ) + + doc = parse_unlimited_ocr_markdown(raw, PAGE_SIZE, page_no=1, filename="page.png") + texts = [t.text for t in doc.texts] + + assert any(t == "First line\nSecond line" for t in texts) + assert any("Next block" in t for t in texts) + assert not any("Loose trailing text" in t for t in texts)