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
9 changes: 6 additions & 3 deletions pageindex/flash/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,24 @@ def extract_toc(
doc_name = Path(str(doc_handle)).name
else:
doc_name = "document.pdf"
page_texts = [
"\n".join(block_text(block) for block in (page.secondary_slot or []))
for page in pages
]
result = {
"doc_name": doc_name,
"doc_title": None,
"structure": [],
"has_abstract_or_references_section": False,
"page_texts": page_texts,
}
# Bookmarks need no extracted text, so they can still structure a
# document this gate wrote off as unreadable.
if use_embedded_toc:
from .embedded_toc import apply_embedded_toc
result["structure"], result["toc_source"] = apply_embedded_toc(
[], doc_handle, len(pages),
page_texts=["\n".join(block_text(block)
for block in (page.secondary_slot or []))
for page in pages],
page_texts=page_texts,
)
return result

Expand Down
33 changes: 33 additions & 0 deletions tests/test_flash_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from io import BytesIO

import pymupdf

from pageindex.flash import page_index_flash


def _sparse_bookmarked_pdf():
doc = pymupdf.open()
for marker in ("x", "y", "z"):
page = doc.new_page()
page.insert_text((72, 72), marker)
doc.set_toc([
[1, "Introduction", 1],
[1, "Methods", 2],
[1, "Results", 3],
])
data = doc.tobytes()
doc.close()
return BytesIO(data)


def test_sparse_bookmarked_pdf_can_use_default_summaries():
result = page_index_flash(_sparse_bookmarked_pdf(), summary=True)

assert result["toc_source"] == "hybrid"
assert [node["title"] for node in result["structure"]] == [
"Introduction",
"Methods",
"Results",
]
assert all(node.get("summary") for node in result["structure"])
assert "page_texts" not in result