|
23 | 23 | InputDoc, |
24 | 24 | MarkdownDoc, |
25 | 25 | OutputFormat, |
26 | | - PageIndexes, |
27 | 26 | Pipeline, |
28 | 27 | PipelineType, |
29 | 28 | Result, |
|
34 | 33 | from pydantic_core.core_schema import SerializerFunctionWrapHandler |
35 | 34 |
|
36 | 35 | from .constants import ARTIFACTS, DEFAULT_MD_PAGE_SEP |
37 | | -from .utils import chdir, map_and_preserve, path_to_artifacts_dirname |
| 36 | +from .utils import chdir, map_and_preserve, path_to_artifacts_dirname, write_pages |
38 | 37 |
|
39 | 38 | logger = logging.getLogger(__name__) |
40 | 39 |
|
@@ -115,39 +114,38 @@ def _to_markdown_doc( |
115 | 114 | raise FileExistsError(f"directory {md_dir} already exists") |
116 | 115 | # Let's avoid issue of duplicated input file names flattened top level |
117 | 116 | md_filename = md_dir_name + OutputFormat.MARKDOWN |
118 | | - total_length = 0 |
119 | | - n_pages = len(res.pages) |
120 | | - |
121 | 117 | with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as td: |
122 | 118 | tmp_dir = Path(td) |
123 | | - page_path = Path("page.md") |
124 | | - # We do a chdir to bypass a Docling bug which only allows to maintain relative |
125 | | - # image ref when saving the markdown to a relative path |
126 | | - with (tmp_dir / md_filename).open("w") as f, chdir(tmp_dir): |
127 | | - end_indices = [] |
128 | | - for page_i in range(n_pages): |
129 | | - res.document.save_as_markdown( |
130 | | - page_path, |
131 | | - page_no=page_i + 1, |
132 | | - image_mode=ImageRefMode.REFERENCED, |
133 | | - artifacts_dir=Path(ARTIFACTS), |
134 | | - **kwargs, |
135 | | - ) |
136 | | - content = page_path.read_text() |
137 | | - if page_i > 0: |
138 | | - content += "\n" |
139 | | - if page_i < n_pages - 1: |
140 | | - content += page_sep |
141 | | - total_length += len(content) |
142 | | - end_indices.append(total_length) |
143 | | - f.write(content) |
144 | | - f.flush() |
145 | | - page_path.unlink() |
| 119 | + md_path = tmp_dir / md_filename |
| 120 | + current_page_path = tmp_dir / "page.md" |
| 121 | + with chdir(tmp_dir): |
| 122 | + # We do a chdir to bypass a Docling bug which only allows to maintain |
| 123 | + # relative image ref when saving the markdown to a relative path |
| 124 | + pages = _docling_pages_it(res, current_page_path, **kwargs) |
| 125 | + with md_path.open("wb") as f: |
| 126 | + pages = write_pages(pages, page_sep, f) |
| 127 | + # Clean up the tmp page file before move everything to the end destination |
| 128 | + current_page_path.unlink() |
146 | 129 | shutil.move(tmp_dir, md_dir) |
147 | | - pages = PageIndexes.from_page_end_indices(end_indices) |
148 | 130 | return MarkdownDoc(path=Path(md_dir_name), pages=pages) |
149 | 131 |
|
150 | 132 |
|
| 133 | +def _docling_pages_it( |
| 134 | + res: ConversionResult, output_path: Path, **kwargs |
| 135 | +) -> Iterable[str]: |
| 136 | + n_pages = len(res.pages) |
| 137 | + for page_i in range(n_pages): |
| 138 | + res.document.save_as_markdown( |
| 139 | + output_path, |
| 140 | + page_no=page_i + 1, |
| 141 | + image_mode=ImageRefMode.REFERENCED, |
| 142 | + artifacts_dir=Path(ARTIFACTS), |
| 143 | + **kwargs, |
| 144 | + ) |
| 145 | + content = output_path.read_text() |
| 146 | + yield content |
| 147 | + |
| 148 | + |
151 | 149 | class SerializableFormatOptions(DoclingFormatOption): |
152 | 150 | # Utility class to serialize Python format options into a JSON which can be |
153 | 151 | # correctly deserialized into a docling FormatOption |
|
0 commit comments