feature(extract-core): output page byte ranges#12
Open
ClemDoum wants to merge 1 commit into
Open
Conversation
28ef803 to
8d5032d
Compare
8d5032d to
b531afa
Compare
pirhoo
requested changes
Jun 24, 2026
pirhoo
left a comment
Member
There was a problem hiding this comment.
Thanks for the swift implem! I've made a few suggestions.
| import markdown2 | ||
| import pypdfium2 | ||
| from extract_core import BaseModel, OutputFormat, PageIndexes | ||
| from extract_core import BaseModel, OutputFormat, PageRanges |
Member
There was a problem hiding this comment.
important: I think this PageRangesdoesnt exist anymore. If I understood correctly it's replaced by Page.
| ) -> list[dict[str, tuple[int, int]]]: | ||
| all_pages = [ | ||
| PageIndexes.model_validate_json( | ||
| PageRanges.model_validate_json( |
Member
There was a problem hiding this comment.
important: same here, we need:
Pages.model_validate_json(
(root / compared / "artifacts" / "pages.json").read_text()
).byte_ranges
| with md_path.open("wb") as f: | ||
| pages = write_pages(pages, page_sep, f) | ||
| # Clean up the tmp page file before move everything to the end destination | ||
| current_page_path.unlink() |
Member
There was a problem hiding this comment.
hint: this will crash if the current page doesn't exist. This can be adjusted:
Suggested change
| current_page_path.unlink() | |
| current_page_path.unlink(missing_ok=True) |
| pages_byte_sizes = [] | ||
| sentinel = object() | ||
| while True: | ||
| content = next(pages, sentinel) if next_page is None else next_page |
Member
There was a problem hiding this comment.
hint: write_pages uses next_page is None as the "no lookahead yet" marker. The only re-entry to the next(pages) branch is when next_page is None, which can only recur if a page value is literally None.
You can rewrite with a sentinel, something like:
def write_pages(pages: Iterable[str | None], page_sep: str, out: BinaryIO) -> Pages:
it = iter(pages)
sentinel = object()
sizes = []
prev = next(it, sentinel)
while prev is not sentinel:
cur = next(it, sentinel)
content = prev or "" # This is the trick
if cur is not sentinel:
content += page_sep
sizes.append(out.write(content.encode()))
prev = cur
return Pages.from_pages_bytes_sizes(sizes)
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.
Descriptions
Output mardown page bytes range, preliminary to support ICIJ/datashare#2229 in the
datashare-python'sextract-workerChanges
extract-coreChanged
Pages(total: int, bytes_ranges: list[tuple[int, int]])and replacedConversionOutput.pages: PageIndexes = [])byConversionOutput.pages: Pagesextract-pythonAdded
utils.write_pageshelper to serialize markdown documents and yield pages bytes ranges