fix: align image filenames between parse_html and extract_images when Blank-Page divs present#85
Open
voidborne-d wants to merge 1 commit intodatalab-to:masterfrom
Conversation
… Blank-Page divs are present parse_html counts all divs (including Blank-Page) in its div_idx counter, but extract_images iterates over chunks from parse_layout which filters out Blank-Page divs. When a document has Blank-Page divs before Image/Figure divs, the two functions generate different filenames for the same image: - HTML/Markdown references e.g. 'hash_3_img.webp' - extract_images saves as e.g. 'hash_1_img.webp' This causes broken image references in the output. Fix: store the original div position (counting all divs) in LayoutBlock.div_idx during parse_layout, carry it through to chunks, and use it in extract_images to generate filenames consistent with parse_html. Includes 17 tests covering: - 5 regression tests for the Blank-Page mismatch bug (all fail on old code) - 3 baseline tests (no Blank-Page, normal operation) - 4 parse_layout div_idx tracking tests - 1 markdown output consistency test - 4 edge cases (all blanks, missing img tag, empty HTML, no images)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
parse_htmlandextract_imagesgenerate different filenames for the same image when the document containsBlank-Pagedivs beforeImage/Figuredivs.parse_htmlcounts all top-level divs in itsdiv_idxcounter (includingBlank-Page), whileextract_imagesiterates over chunks fromparse_layout/parse_chunks, which filters outBlank-Pagedivs. This causes the two counters to diverge:The HTML/Markdown output references
hash_4_img.webp, but the extracted image is saved ashash_2_img.webp. Broken image link.This affects any multi-page PDF where the OCR model emits
Blank-Pagelabels for empty or near-empty pages.Fix
LayoutBlock: Adddiv_idxfield to store the original 1-based position among all top-level divs.parse_layout: Useenumerate(top_level_divs, start=1)and store the position in eachLayoutBlock.div_idx, preserving the original count even whenBlank-Pagedivs are skipped.extract_images: Readchunk["div_idx"](carried throughparse_chunks->asdict) instead of maintaining a separate counter.The change is backward-compatible:
div_idxdefaults to0in the dataclass, andextract_imagesfalls back toidx + 1if the field is missing.Tests
17 new tests in
tests/test_image_name_consistency.py:parse_htmlandextract_imageswith Blank-Page divs (all 5 fail on the old code, proving the bug)parse_layoutstores correct original positionsparse_markdownalso references the correct filename