Repository navigation
Expand file tree
/
Copy pathdocument_conversion.py
More file actions
451 lines (385 loc) · 14.3 KB
/
Copy pathdocument_conversion.py
File metadata and controls
451 lines (385 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""Small, deterministic document-to-Markdown converters.
The desktop runtime must be able to process every format it advertises without
depending on a system Office installation or the very large optional Docling
stack. This module therefore owns the reliable baseline conversion path for
Markdown, plain text, HTML, and modern DOCX files. PDF extraction remains in
``tools.pdf_downloader`` because it has a dedicated pypdf fallback.
Only document contents are read. DOCX archives are never extracted to disk,
and HTML scripts/styles are discarded before text reaches the Agent workflow.
"""
from __future__ import annotations
import os
import re
import uuid
import zipfile
from dataclasses import dataclass
from html.parser import HTMLParser
from pathlib import Path
from typing import Literal
from urllib.parse import urlparse
from xml.etree import ElementTree
DocumentKind = Literal["markdown", "text", "html", "docx", "pdf"]
_MARKDOWN_SUFFIXES = {".md", ".markdown"}
_TEXT_SUFFIXES = {".txt"}
_HTML_SUFFIXES = {".html", ".htm"}
_DOCX_SUFFIXES = {".docx"}
_MAX_DOCX_XML_BYTES = 32 * 1024 * 1024
_HTML_PREFIX_RE = re.compile(
rb"^\s*(?:<!doctype\s+html\b|<html\b|<head\b|<body\b)",
re.IGNORECASE,
)
_HEADING_STYLE_RE = re.compile(r"heading\s*([1-6])", re.IGNORECASE)
class DocumentConversionError(ValueError):
"""Raised when a supported document cannot be converted safely."""
class UnsupportedDocumentError(DocumentConversionError):
"""Raised when the baseline converter cannot identify the input format."""
@dataclass(frozen=True, slots=True)
class ConversionResult:
"""Result of a successful baseline conversion."""
output_file: Path
input_kind: DocumentKind
character_count: int
def detect_document_kind(path: str | Path) -> DocumentKind:
"""Identify a supported document using magic bytes before its suffix."""
source = Path(path)
try:
with source.open("rb") as handle:
header = handle.read(8192)
except OSError as exc:
raise DocumentConversionError(f"could not read document: {exc}") from exc
if header.startswith(b"%PDF"):
return "pdf"
suffix = source.suffix.lower()
if suffix == ".doc":
raise UnsupportedDocumentError(
"legacy .doc files are not supported; save the document as .docx"
)
if suffix in _DOCX_SUFFIXES or _looks_like_docx(source, header):
return "docx"
if suffix in _MARKDOWN_SUFFIXES:
return "markdown"
if suffix in _HTML_SUFFIXES or _HTML_PREFIX_RE.match(header):
return "html"
if suffix in _TEXT_SUFFIXES:
return "text"
# A URL can legitimately return text or HTML under an unhelpful filename.
# Accept it only when the payload is clearly text; arbitrary binary input
# still fails closed.
text = _decode_text(header)
if _looks_like_html_text(text):
return "html"
if _looks_like_plain_text(text):
return "text"
raise UnsupportedDocumentError(
f"unsupported document format: {suffix or '(no extension)'}"
)
def convert_to_markdown(
input_file: str | Path,
output_file: str | Path | None = None,
) -> ConversionResult:
"""Convert a baseline-supported document to a UTF-8 Markdown file."""
source = Path(input_file).expanduser().resolve(strict=True)
kind = detect_document_kind(source)
if kind == "pdf":
raise UnsupportedDocumentError("PDF conversion is handled by the PDF extractor")
target = (
Path(output_file).expanduser().resolve()
if output_file is not None
else source.with_suffix(".md")
)
if kind == "markdown":
markdown = _read_text(source)
elif kind == "text":
markdown = _read_text(source)
elif kind == "html":
markdown = _html_to_markdown(_read_text(source))
else:
markdown = _docx_to_markdown(source)
normalized = _normalize_markdown(markdown)
if not normalized.strip():
raise DocumentConversionError("document contains no readable text")
if target != source:
_atomic_write_text(target, normalized)
return ConversionResult(
output_file=target,
input_kind=kind,
character_count=len(normalized),
)
def _looks_like_docx(path: Path, header: bytes) -> bool:
if not header.startswith(b"PK"):
return False
try:
with zipfile.ZipFile(path) as archive:
return "word/document.xml" in archive.namelist()
except (OSError, zipfile.BadZipFile):
return False
def _read_text(path: Path) -> str:
try:
return _decode_text(path.read_bytes())
except OSError as exc:
raise DocumentConversionError(f"could not read text document: {exc}") from exc
def _decode_text(data: bytes) -> str:
if b"\x00" in data and not data.startswith((b"\xff\xfe", b"\xfe\xff")):
raise UnsupportedDocumentError("document payload is binary")
encoding = "utf-16" if data.startswith((b"\xff\xfe", b"\xfe\xff")) else "utf-8-sig"
try:
return data.decode(encoding)
except UnicodeDecodeError as exc:
raise DocumentConversionError("text document is not UTF-8 or UTF-16") from exc
def _looks_like_html_text(text: str) -> bool:
prefix = text.lstrip()[:256].lower()
return prefix.startswith(("<!doctype html", "<html", "<head", "<body"))
def _looks_like_plain_text(text: str) -> bool:
if not text:
return True
control_count = sum(
1 for char in text if ord(char) < 32 and char not in "\n\r\t\f\b"
)
return control_count / len(text) < 0.01
class _MarkdownHTMLParser(HTMLParser):
"""Conservative HTML reader that keeps structure but ignores active content."""
_SKIPPED_TAGS = {"script", "style", "noscript", "template", "svg"}
_BLOCK_TAGS = {
"address",
"article",
"aside",
"blockquote",
"div",
"footer",
"header",
"main",
"nav",
"p",
"section",
}
def __init__(self) -> None:
super().__init__(convert_charrefs=True)
self.parts: list[str] = []
self.skip_depth = 0
self.pre_depth = 0
self.links: list[str | None] = []
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
tag = tag.lower()
if tag in self._SKIPPED_TAGS:
self.skip_depth += 1
return
if self.skip_depth:
return
attributes = dict(attrs)
if tag in self._BLOCK_TAGS:
self._break(paragraph=True)
elif tag in {f"h{level}" for level in range(1, 7)}:
self._break(paragraph=True)
self.parts.append(f"{'#' * int(tag[1])} ")
elif tag == "br":
self._break()
elif tag == "li":
self._break()
self.parts.append("- ")
elif tag == "hr":
self._break(paragraph=True)
self.parts.append("---")
self._break(paragraph=True)
elif tag == "pre":
self._break(paragraph=True)
self.parts.append("```\n")
self.pre_depth += 1
elif tag == "code" and not self.pre_depth:
self.parts.append("`")
elif tag == "a":
self.links.append(_safe_link(attributes.get("href")))
elif tag in {"tr"}:
self._break()
elif tag in {"td", "th"}:
self.parts.append("| ")
elif tag == "img":
alt = (attributes.get("alt") or "").strip()
if alt:
self.parts.append(f"[Image: {alt}]")
def handle_endtag(self, tag: str) -> None:
tag = tag.lower()
if tag in self._SKIPPED_TAGS:
if self.skip_depth:
self.skip_depth -= 1
return
if self.skip_depth:
return
if tag in self._BLOCK_TAGS or tag.startswith("h") and tag[1:].isdigit():
self._break(paragraph=True)
elif tag in {"li", "tr"}:
if tag == "tr":
self.parts.append("|")
self._break()
elif tag in {"td", "th"}:
self.parts.append(" ")
elif tag == "pre":
if self.pre_depth:
self.pre_depth -= 1
self._break()
self.parts.append("```")
self._break(paragraph=True)
elif tag == "code" and not self.pre_depth:
self.parts.append("`")
elif tag == "a" and self.links:
href = self.links.pop()
if href:
self.parts.append(f" ({href})")
def handle_data(self, data: str) -> None:
if self.skip_depth or not data:
return
if self.pre_depth:
self.parts.append(data)
return
collapsed = re.sub(r"\s+", " ", data)
if not collapsed.strip():
if self.parts and not self.parts[-1].endswith(("\n", " ")):
self.parts.append(" ")
return
if (
self.parts
and not self.parts[-1].endswith(("\n", " ", "`"))
and not collapsed.startswith((" ", ".", ",", ":", ";", "!", "?"))
):
self.parts.append(" ")
self.parts.append(collapsed.strip())
def _break(self, *, paragraph: bool = False) -> None:
desired = "\n\n" if paragraph else "\n"
current = "".join(self.parts[-2:])
if not current.endswith(desired):
self.parts.append(desired)
def _safe_link(value: str | None) -> str | None:
if not value:
return None
cleaned = value.strip()
parsed = urlparse(cleaned)
if parsed.scheme.lower() in {"javascript", "data", "file"}:
return None
return cleaned
def _html_to_markdown(html: str) -> str:
parser = _MarkdownHTMLParser()
try:
parser.feed(html)
parser.close()
except Exception as exc:
raise DocumentConversionError(f"could not parse HTML document: {exc}") from exc
return "".join(parser.parts)
def _docx_to_markdown(path: Path) -> str:
try:
with zipfile.ZipFile(path) as archive:
try:
info = archive.getinfo("word/document.xml")
except KeyError as exc:
raise DocumentConversionError(
"DOCX archive does not contain word/document.xml"
) from exc
if info.file_size > _MAX_DOCX_XML_BYTES:
raise DocumentConversionError(
"DOCX document XML exceeds the safe conversion limit"
)
xml = archive.read(info)
except zipfile.BadZipFile as exc:
raise DocumentConversionError("DOCX file is not a valid ZIP archive") from exc
except OSError as exc:
raise DocumentConversionError(f"could not read DOCX file: {exc}") from exc
try:
root = ElementTree.fromstring(xml)
except ElementTree.ParseError as exc:
raise DocumentConversionError(f"DOCX XML is invalid: {exc}") from exc
body = next((node for node in root.iter() if _local_name(node.tag) == "body"), None)
if body is None:
raise DocumentConversionError("DOCX document body is missing")
blocks: list[str] = []
for child in body:
name = _local_name(child.tag)
if name == "p":
paragraph = _docx_paragraph(child)
if paragraph:
blocks.append(paragraph)
elif name == "tbl":
table = _docx_table(child)
if table:
blocks.append(table)
return "\n\n".join(blocks)
def _docx_paragraph(node: ElementTree.Element) -> str:
text = _docx_text(node).strip()
if not text:
return ""
style = ""
numbered = False
for descendant in node.iter():
name = _local_name(descendant.tag)
if name == "pStyle":
style = _xml_attribute(descendant, "val") or ""
elif name == "numPr":
numbered = True
heading = _HEADING_STYLE_RE.search(style)
if heading:
return f"{'#' * int(heading.group(1))} {text}"
if style.lower() == "title":
return f"# {text}"
if numbered or "list" in style.lower():
return f"- {text}"
return text
def _docx_table(node: ElementTree.Element) -> str:
rows: list[list[str]] = []
for row in node:
if _local_name(row.tag) != "tr":
continue
cells: list[str] = []
for cell in row:
if _local_name(cell.tag) != "tc":
continue
value = " ".join(
text
for paragraph in cell
if _local_name(paragraph.tag) == "p"
and (text := _docx_text(paragraph).strip())
)
cells.append(value.replace("|", "\\|"))
if cells:
rows.append(cells)
if not rows:
return ""
width = max(len(row) for row in rows)
padded = [row + [""] * (width - len(row)) for row in rows]
lines = ["| " + " | ".join(row) + " |" for row in padded]
lines.insert(1, "| " + " | ".join("---" for _ in range(width)) + " |")
return "\n".join(lines)
def _docx_text(node: ElementTree.Element) -> str:
parts: list[str] = []
for descendant in node.iter():
name = _local_name(descendant.tag)
if name in {"t", "delText"} and descendant.text:
parts.append(descendant.text)
elif name == "tab":
parts.append("\t")
elif name in {"br", "cr"}:
parts.append("\n")
return "".join(parts)
def _local_name(tag: str) -> str:
return tag.rsplit("}", 1)[-1]
def _xml_attribute(node: ElementTree.Element, name: str) -> str | None:
for key, value in node.attrib.items():
if _local_name(key) == name:
return value
return None
def _normalize_markdown(markdown: str) -> str:
text = markdown.replace("\r\n", "\n").replace("\r", "\n")
lines = [line.rstrip() for line in text.splitlines()]
normalized = "\n".join(lines).strip()
normalized = re.sub(r"\n{3,}", "\n\n", normalized)
return f"{normalized}\n" if normalized else ""
def _atomic_write_text(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
try:
with temporary.open("x", encoding="utf-8", newline="\n") as handle:
handle.write(content)
handle.flush()
os.fsync(handle.fileno())
os.replace(temporary, path)
finally:
try:
temporary.unlink()
except FileNotFoundError:
pass