Skip to content
Draft
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
9 changes: 5 additions & 4 deletions src/newsdom_api/dom_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ def _build_pages_without_page_idx(
"Some blocks are missing page_idx; content was assigned to page_idx 0 while preserving model-declared page count."
)
pages = []
for page_idx in sorted(page_info_by_idx):
page_info = page_info_by_idx.get(page_idx, {})

for page_idx, page_info in sorted(page_info_by_idx.items()):
pages.append(
_build_page_dom(
content_list if page_idx == 0 else [],
Expand Down Expand Up @@ -425,11 +425,12 @@ def _build_pages_with_page_idx(

pages = []
article_seq = count(1)
for page_idx in sorted(blocks_by_page_idx):

for page_idx, blocks in sorted(blocks_by_page_idx.items()):
page_info = page_info_by_idx.get(page_idx, {})
pages.append(
_build_page_dom(
blocks_by_page_idx[page_idx],
blocks,
page_number=_page_number_from_info(page_info, page_idx + 1),
article_seq=article_seq,
width=page_info.get("width"),
Expand Down
56 changes: 56 additions & 0 deletions tests/test_dom_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,59 @@ def test_bbox_helper_returns_none_for_invalid_y0_x1_y1():
assert _bbox_from_values([0, "bad", 1, 1]) is None
assert _bbox_from_values([0, 0, "bad", 1]) is None
assert _bbox_from_values([0, 0, 1, "bad"]) is None


def test_missing_page_idx_multi_metadata():
content = [{"type": "text", "text": "hello"}]
model = [{"page_info": {"width": 100}}, {"page_info": {"width": 200}}]
res = build_dom(content, "doc1", model)
assert len(res.pages) == 2
assert len(res.pages[0].articles) == 1
assert len(res.pages[1].articles) == 0


def test_sparse_page_idx():
content = [{"type": "text", "text": "hello", "page_idx": 2}]
model = [
{"page_info": {"width": 100}},
{"page_info": {"width": 200}},
{"page_info": {"width": 300}},
]
res = build_dom(content, "doc1", model)
assert len(res.pages) == 1
assert res.pages[0].page_number == 3
assert len(res.pages[0].articles) == 1


def test_mixed_missing_page_idx():
content = [
{"type": "text", "text": "hello", "page_idx": 1},
{"type": "text", "text": "world"},
]
model = [{"page_info": {"width": 100}}, {"page_info": {"width": 200}}]
res = build_dom(content, "doc1", model)
assert len(res.pages) == 2
assert len(res.pages[0].articles) == 1
assert res.pages[0].articles[0].body_blocks[0] == "world"
assert len(res.pages[1].articles) == 1
assert res.pages[1].articles[0].body_blocks[0] == "hello"


def test_metadata_fallback():
content = [{"type": "text", "text": "hello"}]
res = build_dom(content, "doc1", [{"page_info": {}}])
assert len(res.pages) == 1
assert res.pages[0].page_number == 1


def test_deterministic_ordering():
content = [
{"type": "text", "text": "page1", "page_idx": 0},
{"type": "text", "text": "page3", "page_idx": 2},
{"type": "text", "text": "page2", "page_idx": 1},
]
res = build_dom(content, "doc1")
assert len(res.pages) == 3
assert res.pages[0].articles[0].body_blocks[0] == "page1"
assert res.pages[1].articles[0].body_blocks[0] == "page2"
assert res.pages[2].articles[0].body_blocks[0] == "page3"
Loading