Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: implement source document preview for HTML files and remove OFD preview
myhloliff349a940df7
diff --git a/mineru/kit/gradio/app.py b/mineru/kit/gradio/app.pyindex d6796e19..9a401a77 100644--- a/mineru/kit/gradio/app.py+++ b/mineru/kit/gradio/app.py@@ -14,7 +14,14 @@ from typing import Any, Literalfrom urllib.parse import quotefrom ...errors import MineruError-from ...filetypes import FLASH_ONLY_PARSE_EXTENSIONS, IMAGE_EXTENSIONS, OFFICE_EXTENSIONS, PARSEABLE_EXTENSIONS, PDF_EXTENSIONS+from ...filetypes import (+ FLASH_ONLY_PARSE_EXTENSIONS,+ HTML_EXTENSIONS,+ IMAGE_EXTENSIONS,+ OFFICE_EXTENSIONS,+ PARSEABLE_EXTENSIONS,+ PDF_EXTENSIONS,+)from ...types import TIERS, Tierfrom ...utils.logger import configure_global_log_levelfrom ...utils.stdio import configure_standard_streams@@ -26,10 +33,10 @@ from .client import (V1ServerCapabilities,)from .i18n import MESSAGES, localized_text, preview_placeholder, translations-from .ofd_preview import prepare_ofd_previewfrom .page_range import effective_page_range as _effective_page_rangefrom .page_range import pdf_page_metadata, validate_max_pagesfrom .pdf_preview import pdf_preview_js, register_pdf_preview_resources+from .source_preview import prepare_source_previewfrom .status import (DEFAULT_STATUS as _DEFAULT_STATUS,STATUS_COMPLETED,@@ -164,27 +171,28 @@ _KIT_MENU_CSS = """}#mineru-kit-download-options :is(button, a):hover { background: var(--background-fill-secondary, #f3f4f6); }.mineru-kit-empty-preview { min-height: 160px; display: grid; place-items: center; opacity: .65; }-/* Gradio 6.8 会按逗号拆分并重写选择器,PDF/OFD 使用独立选择器避免破坏 :has。 */-/* PDF/OFD 直接贴合面板边框,独立预览不再沿用旧组件的标签留白与额外高度。 */-.mineru-kit-preview:has(.mineru-pdf-frame), .mineru-kit-preview:has(.mineru-ofd-frame) { padding: 0; gap: 0; overflow: hidden; }+/* Gradio 6.8 会按逗号拆分并重写选择器,PDF/源文档预览使用独立选择器避免破坏 :has。 */+/* PDF/源文档预览直接贴合面板边框,独立预览不再沿用旧组件的标签留白与额外高度。 */+.mineru-kit-preview:has(.mineru-pdf-frame),+.mineru-kit-preview:has(.mineru-source-frame) { padding: 0; gap: 0; overflow: hidden; }.mineru-kit-preview > .block.mineru-kit-pdf-preview,-.mineru-kit-preview > .block.mineru-kit-ofd-preview {+.mineru-kit-preview > .block.mineru-kit-source-preview {height: var(--mineru-preview-content-height, 775px) !important;min-height: 0 !important; max-height: none !important;}-.mineru-kit-pdf-preview, .mineru-kit-ofd-preview { height: 100%; padding: 0 !important; }+.mineru-kit-pdf-preview, .mineru-kit-source-preview { height: 100%; padding: 0 !important; }.mineru-kit-pdf-preview .html-container, .mineru-kit-pdf-preview .prose,-.mineru-kit-ofd-preview .html-container, .mineru-kit-ofd-preview .prose { height: 100%; padding: 0 !important; }+.mineru-kit-source-preview .html-container, .mineru-kit-source-preview .prose { height: 100%; padding: 0 !important; }.mineru-kit-pdf-preview:not(:has(.mineru-pdf-frame, [role="alert"])) { display: none !important; }-.mineru-pdf-frame, .mineru-ofd-frame { display: block; width: 100%; height: 100%; border: 0; }-.mineru-kit-ofd-preview:not(:has(iframe)):not(:has([data-mineru-i18n-key])) { display: none !important; }+.mineru-pdf-frame, .mineru-source-frame { display: block; width: 100%; height: 100%; border: 0; }+.mineru-kit-source-preview:not(:has(iframe)):not(:has([data-mineru-i18n-key])) { display: none !important; }.mineru-kit-image-preview img { max-height: var(--mineru-pdf-page-height, 720px); object-fit: contain; }-/* 桌面两栏共用行高,PDF/OFD 填满伸展后的面板;窄屏仍采用独立预览高度。 */+/* 桌面两栏共用行高,PDF/源文档预览填满伸展后的面板;窄屏仍采用独立预览高度。 */@media (min-width: 901px) {.mineru-kit-results, .mineru-kit-preview:has(.mineru-pdf-frame),- .mineru-kit-preview:has(.mineru-ofd-frame) { align-self: stretch !important; height: auto; }+ .mineru-kit-preview:has(.mineru-source-frame) { align-self: stretch !important; height: auto; }.mineru-kit-preview > .block.mineru-kit-pdf-preview,- .mineru-kit-preview > .block.mineru-kit-ofd-preview { flex: 1 1 0; height: auto !important; }+ .mineru-kit-preview > .block.mineru-kit-source-preview { flex: 1 1 0; height: auto !important; }}@media (max-width: 900px) {.mineru-kit-workspace { flex-direction: column !important; }@@ -455,13 +463,13 @@ def build_gradio_app(min_height=320,elem_classes=["mineru-kit-office-preview", "mineru-office-preview-html"],)- ofd_preview = gr.HTML(+ source_preview = gr.HTML(value="",apply_default_css=False,- elem_classes=["mineru-kit-ofd-preview"],+ elem_classes=["mineru-kit-source-preview"],)- ofd_ticket = gr.Textbox(value="", visible=False)- ofd_receipt = gr.Textbox(value="", visible=False)+ source_ticket = gr.Textbox(value="", visible=False)+ source_receipt = gr.Textbox(value="", visible=False)generic_preview = gr.HTML(value=preview_placeholder("empty_preview"),visible=True,@@ -554,7 +562,8 @@ def build_gradio_app(_preview_update(gr, preview_placeholder("source_preview"), visible=False),*reset_result,)- if suffix == "ofd":+ if suffix == "ofd" or suffix in HTML_EXTENSIONS:+ # OFD/HTML 源预览由独立异步事件挂载,此处只隐藏占位组件。return (_pdf_preview_update(gr, None),_preview_update(gr, None, visible=False),@@ -590,35 +599,35 @@ def build_gradio_app(input_file.change(fn=None, inputs=input_file, outputs=pdf_viewer, js=pdf_preview_js("reset"), **private_event_kwargs)clear_button.click(fn=None, inputs=[], outputs=pdf_viewer, js=pdf_preview_js("clear"), **private_event_kwargs)- ofd_script = _resource_text("gradio_ofd_preview.js")+ source_script = _resource_text("gradio_source_preview.js")input_file.change(fn=None,inputs=input_file,- outputs=[ofd_ticket, ofd_preview],- js=f"(...args) => ({ofd_script})('begin', ...args)",+ outputs=[source_ticket, source_preview],+ js=f"(...args) => ({source_script})('begin', ...args)",**private_event_kwargs,)# Gradio 6.8 的纯前端事件不可靠地触发 then;通过请求值变化启动后台转换。- render_ofd = ofd_ticket.change(- fn=prepare_ofd_preview,- inputs=[input_file, ofd_ticket],- outputs=ofd_receipt,+ render_source_preview = source_ticket.change(+ fn=prepare_source_preview,+ inputs=[input_file, source_ticket],+ outputs=source_receipt,concurrency_limit=None,trigger_mode="multiple",**private_event_kwargs,)- render_ofd.then(+ render_source_preview.then(fn=None,- inputs=ofd_receipt,- outputs=ofd_preview,- js=f"(...args) => ({ofd_script})('apply', ...args)",+ inputs=source_receipt,+ outputs=source_preview,+ js=f"(...args) => ({source_script})('apply', ...args)",**private_event_kwargs,)clear_button.click(fn=None,inputs=[],- outputs=[ofd_ticket, ofd_preview],- js=f"(...args) => ({ofd_script})('clear', ...args)",+ outputs=[source_ticket, source_preview],+ js=f"(...args) => ({source_script})('clear', ...args)",**private_event_kwargs,)@@ -827,8 +836,8 @@ def build_gradio_app(gr.update(value="", visible=False),gr.update(value=generic_html, visible=bool(generic_html)),)- if _is_office(source_path) or suffix == "ofd":- # Office/OFD 源预览已在上传时挂载,成功后保留原内容和浏览位置。+ if _is_office(source_path) or suffix == "ofd" or suffix in HTML_EXTENSIONS:+ # Office/OFD/HTML 源预览已在上传时挂载,成功后保留原内容和浏览位置。result_preview_updates = tuple(gr.skip() for _ in range(4))return (rendered_html,diff --git a/mineru/kit/gradio/i18n.py b/mineru/kit/gradio/i18n.pyindex c2225fcb..1ef8fdc8 100644--- a/mineru/kit/gradio/i18n.py+++ b/mineru/kit/gradio/i18n.py@@ -71,6 +71,10 @@ MESSAGES: dict[str, tuple[str, str]] = {"Could not preview this OFD file. You can still try converting it.","OFD 预览生成失败,仍可尝试解析该文件。",),+ "html_preview_failed": (+ "Could not preview this HTML file. You can still try converting it.",+ "HTML 预览生成失败,仍可尝试解析该文件。",+ ),"unsupported_preview": ("No source preview for this format", "该格式暂无源文档预览"),"result_ready": ("Results are ready", "结果已生成"),"markdown": ("Markdown", "Markdown"),diff --git a/mineru/kit/gradio/ofd_preview.py b/mineru/kit/gradio/ofd_preview.pyindex 04a16c38..249b0f13 100644--- a/mineru/kit/gradio/ofd_preview.py+++ b/mineru/kit/gradio/ofd_preview.py@@ -1,17 +1,10 @@-"""OFD 源文档预览适配,不参与语义解析或结果导出。"""+"""OFD 源文档预览转换,不参与语义解析或结果导出。"""from __future__ import annotations-import asyncioimport html-import json-from pathlib import Path-from loguru import logger--from .i18n import preview_placeholder--__all__ = ["build_ofd_preview", "prepare_ofd_preview"]+__all__ = ["build_ofd_preview"]def build_ofd_preview(payload: bytes) -> str:@@ -27,21 +20,6 @@ def build_ofd_preview(payload: bytes) -> str:)document = document.replace("<head>", "<head>" + style, 1)return (- '<iframe class="mineru-ofd-frame" title="OFD preview" sandbox="" '+ '<iframe class="mineru-source-frame" title="OFD preview" sandbox="" 'f'srcdoc="{html.escape(document, quote=True)}"></iframe>')---async def prepare_ofd_preview(file_path: str | None, ticket: str) -> str:- """后台生成带请求标识的预览回执,交由浏览器丢弃过期结果。"""- request = json.loads(ticket)- result = {"id": request["id"], "html": ""}- if not file_path or request.get("path") != file_path or Path(file_path).suffix.lower() != ".ofd":- return json.dumps(result)- try:- payload = await asyncio.to_thread(Path(file_path).read_bytes)- result["html"] = await asyncio.to_thread(build_ofd_preview, payload)- except Exception as exc:- logger.warning("OFD source preview failed: {}", exc)- result["html"] = preview_placeholder("ofd_preview_failed")- return json.dumps(result)diff --git a/mineru/kit/gradio/source_preview.py b/mineru/kit/gradio/source_preview.pynew file mode 100644index 00000000..16aadda1--- /dev/null+++ b/mineru/kit/gradio/source_preview.py@@ -0,0 +1,94 @@+"""源文档 HTML 预览的异步装配,不参与语义解析或结果导出。"""++from __future__ import annotations++import asyncio+import codecs+import html+import json+import re+from collections.abc import Callable+from pathlib import Path++from loguru import logger++from .i18n import preview_placeholder+from .ofd_preview import build_ofd_preview++__all__ = ["build_html_preview", "prepare_source_preview"]++# 与结果预览一致允许脚本执行:MathJax/KaTeX 等公式排版依赖内联配置、eval 加载+# 与 XHR 拉取字体;沙箱保持 opaque origin(无 allow-same-origin),隔离父页面、+# 存储与顶级导航,媒体、内嵌框架和表单仍由 default-src 'none' 封禁。+_CSP_META = (+ '<meta http-equiv="Content-Security-Policy" content="default-src \'none\'; '+ "script-src 'unsafe-inline' 'unsafe-eval' https: http:; "+ "style-src 'unsafe-inline' https: http:; img-src data: blob: https: http:; "+ 'font-src data: blob: https:; connect-src https: http:">'+)++# 只嗅探文档头部即可覆盖 meta 声明,避免扫描超大文件。+_CHARSET_SNIFF_SIZE = 4096+_CHARSET_PATTERN = re.compile(rb"""charset\s*=\s*["']?\s*([A-Za-z0-9_.:+-]+)""", re.IGNORECASE)+++def _decode_html(payload: bytes) -> str:+ """按 BOM 与头部 charset 声明解码,未知或损坏编码一律回退 UTF-8 替换字符。"""+ if payload.startswith(codecs.BOM_UTF8):+ return payload.decode("utf-8-sig", errors="replace")+ encoding = "utf-8"+ if match := _CHARSET_PATTERN.search(payload[:_CHARSET_SNIFF_SIZE]):+ try:+ codecs.lookup(match.group(1).decode("ascii", errors="ignore"))+ encoding = match.group(1).decode("ascii", errors="ignore")+ except (LookupError, UnicodeDecodeError):+ pass+ return payload.decode(encoding, errors="replace")+++def _inject_head(document: str, snippet: str) -> str:+ """把头部片段注入既有 head;缺失时补建,绝不置于 doctype 之前以免触发 quirks mode。"""+ # (?:\s[^>]*)? 限定标签名边界,避免 <head 规则误吞 <header>、<html 规则误吞其他前缀。+ if match := re.search(r"(?is)<head(?:\s[^>]*)?>", document):+ return document[: match.end()] + snippet + document[match.end() :]+ if match := re.search(r"(?is)<html(?:\s[^>]*)?>", document):+ return document[: match.end()] + f"<head>{snippet}</head>" + document[match.end() :]+ if match := re.match(r"(?is)\s*<!doctype[^>]*>", document):+ return document[: match.end()] + f"<head>{snippet}</head>" + document[match.end() :]+ return f"<head>{snippet}</head>" + document+++def build_html_preview(payload: bytes) -> str:+ """按声明编码解码源 HTML,并在仅开放脚本的隔离框架中显示自包含页面。"""+ document = _inject_head(_decode_html(payload), _CSP_META)+ return (+ '<iframe class="mineru-source-frame" title="HTML preview" sandbox="allow-scripts" '+ f'srcdoc="{html.escape(document, quote=True)}"></iframe>'+ )+++# 后缀到预览构造器和失败占位键的显式映射,避免运行时注册。+_PREVIEW_KINDS: dict[str, tuple[Callable[[bytes], str], str]] = {+ ".ofd": (build_ofd_preview, "ofd_preview_failed"),+ ".html": (build_html_preview, "html_preview_failed"),+ ".htm": (build_html_preview, "html_preview_failed"),+ ".shtml": (build_html_preview, "html_preview_failed"),+}+++async def prepare_source_preview(file_path: str | None, ticket: str) -> str:+ """后台生成带请求标识的预览回执,交由浏览器丢弃过期结果。"""+ request = json.loads(ticket)+ result = {"id": request["id"], "html": ""}+ suffix = Path(file_path).suffix.lower() if file_path else ""+ kind = _PREVIEW_KINDS.get(suffix) if request.get("path") == file_path else None+ if kind is None:+ return json.dumps(result)+ builder, failure_key = kind+ try:+ payload = await asyncio.to_thread(Path(file_path).read_bytes)+ result["html"] = await asyncio.to_thread(builder, payload)+ except Exception as exc:+ logger.warning("Source preview failed for '{}': {}", suffix, exc)+ result["html"] = preview_placeholder(failure_key)+ return json.dumps(result)diff --git a/mineru/resources/gradio_ofd_preview.js b/mineru/resources/gradio_source_preview.jssimilarity index 92%rename from mineru/resources/gradio_ofd_preview.jsrename to mineru/resources/gradio_source_preview.jsindex e191c30e..ae219977 100644--- a/mineru/resources/gradio_ofd_preview.js+++ b/mineru/resources/gradio_source_preview.js@@ -1,6 +1,6 @@(action, ...args) => {// 浏览器会话保存最新请求标识,清除和重复上传同一文件也会使旧请求失效。- const key = "__mineruOfdPreview";+ const key = "__mineruSourcePreview";if (action === "begin" || action === "clear") {const file = action === "clear" ? null : args[0];const ticket = {id: (window[key] || 0) + 1, path: file?.path || ""};diff --git a/tests/unittest/test_kit_gradio_ofd_preview.cjs b/tests/unittest/test_kit_gradio_source_preview.cjssimilarity index 79%rename from tests/unittest/test_kit_gradio_ofd_preview.cjsrename to tests/unittest/test_kit_gradio_source_preview.cjsindex 126412cc..97088092 100644--- a/tests/unittest/test_kit_gradio_ofd_preview.cjs+++ b/tests/unittest/test_kit_gradio_source_preview.cjs@@ -3,10 +3,10 @@ const fs = require('node:fs');const path = require('node:path');const vm = require('node:vm');let sequence = 0;-const preview = vm.runInNewContext(fs.readFileSync(path.join(__dirname, '../../mineru/resources/gradio_ofd_preview.js'), 'utf8'), {+const preview = vm.runInNewContext(fs.readFileSync(path.join(__dirname, '../../mineru/resources/gradio_source_preview.js'), 'utf8'), {window: {}, crypto: {randomUUID: () => String(++sequence)},});-const first = JSON.parse(preview('begin', {path: '/first.ofd'})[0]);+const first = JSON.parse(preview('begin', {path: '/first.html'})[0]);const second = JSON.parse(preview('begin', {path: '/second.ofd'})[0]);assert.equal(preview('apply', JSON.stringify({id: second.id, html: 'new'})), 'new');assert.equal(preview('apply', JSON.stringify({id: first.id, html: 'old'})).__type__, 'update');@@ -15,4 +15,4 @@ assert.equal(preview('apply', JSON.stringify({id: second.id, html: 'stale'})).__const third = JSON.parse(preview('begin', {path: '/second.ofd'})[0]);assert.notEqual(third.id, second.id);assert.equal(preview('apply', JSON.stringify({id: third.id, html: 'reload'})), 'reload');-console.log('OFD preview: switch, clear, re-upload and stale receipts passed');+console.log('Source preview: switch, clear, re-upload and stale receipts passed');diff --git a/tests/unittest/test_kit_gradio_ofd_preview.py b/tests/unittest/test_kit_gradio_source_preview.pysimilarity index 52%rename from tests/unittest/test_kit_gradio_ofd_preview.pyrename to tests/unittest/test_kit_gradio_source_preview.pyindex c3e3930f..9dcf8b52 100644--- a/tests/unittest/test_kit_gradio_ofd_preview.py+++ b/tests/unittest/test_kit_gradio_source_preview.py@@ -1,4 +1,4 @@-"""OFD 预览的真实转换、隔离和失败回执回归。"""+"""源文档预览(OFD/HTML)的真实转换、隔离和失败回执回归。"""import asyncioimport io@@ -10,7 +10,8 @@ import pytestfrom lxml import htmlfrom PIL import Image-from mineru.kit.gradio.ofd_preview import build_ofd_preview, prepare_ofd_preview+from mineru.kit.gradio.ofd_preview import build_ofd_preview+from mineru.kit.gradio.source_preview import build_html_preview, prepare_source_previewdef ofd_bytes(text: str = "中文发票 & <script>", *, entity: str = "") -> bytes:@@ -49,10 +50,16 @@ def ofd_bytes(text: str = "中文发票 & <script>", *, entity: str =return output.getvalue()+def _frame_document(frame_html: str) -> str:+ """取出沙箱 iframe 的 srcdoc 文档,属性转义在往返解析中被验证。"""+ return html.fromstring(frame_html).get("srcdoc")++def test_real_conversion_embeds_pages_and_escapes_text() -> None:"""验证两页 SVG、模板路径、图片和文字转义,并禁止 iframe 脚本。"""frame = html.fromstring(build_ofd_preview(ofd_bytes()))assert frame.get("sandbox") == ""+ assert frame.get("class") == "mineru-source-frame"document = html.fromstring(frame.get("srcdoc"))assert len(document.xpath("//svg")) == 2assert len(document.xpath("//svg/path")) == 2@@ -87,23 +94,105 @@ def test_external_entity_is_not_read(tmp_path: Path) -> None:assert "OFD_EXTERNAL_ENTITY_SECRET" not in frame+def test_html_preview_injects_csp_into_head_and_keeps_content() -> None:+ """源 HTML 注入 CSP 后放入无权限沙箱,正文与属性转义保持不变。"""+ payload = (+ "<!DOCTYPE html><html><head><title>报告</title></head>"+ '<body><p>中文 & 引号 "quoted"</p><img src="a.png"></body></html>'+ ).encode("utf-8")+ frame = html.fromstring(build_html_preview(payload))+ assert frame.get("sandbox") == "allow-scripts"+ assert frame.get("class") == "mineru-source-frame"+ document = html.fromstring(frame.get("srcdoc"))+ csp = document.xpath("//head/meta[@http-equiv='Content-Security-Policy']")+ assert len(csp) == 1+ assert "default-src 'none'" in csp[0].get("content")+ assert "img-src data: blob: https: http:" in csp[0].get("content")+ assert '中文 & 引号 "quoted"' in document.text_content()+++def test_html_preview_keeps_formula_scripts_for_typesetting() -> None:+ """MathJax/KaTeX 脚本与 TeX 源文本完整保留,CSP 允许其执行以渲染公式。"""+ payload = (+ "<!DOCTYPE html><html><head>"+ '<script>window.MathJax = {tex: {inlineMath: [["$", "$"]]}};</script>'+ '<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>'+ "</head><body><p>质能方程 \\(E=mc^2\\)</p></body></html>"+ ).encode("utf-8")+ frame = html.fromstring(build_html_preview(payload))+ assert frame.get("sandbox") == "allow-scripts"+ document = html.fromstring(frame.get("srcdoc"))+ scripts = document.xpath("//script")+ assert len(scripts) == 2+ assert scripts[0].text is not None and "inlineMath" in scripts[0].text+ assert scripts[1].get("src").endswith("tex-chtml.js")+ assert "E=mc^2" in document.text_content()+ csp = document.xpath("//head/meta[@http-equiv='Content-Security-Policy']")[0].get("content")+ assert "script-src 'unsafe-inline' 'unsafe-eval' https: http:" in csp+ assert "connect-src https: http:" in csp+++def test_html_preview_decodes_declared_charset() -> None:+ """按头部 charset 声明解码 GBK 等存量文档,未知编码回退 UTF-8 替换。"""+ payload = '<html><head><meta charset="gbk"></head><body><p>中文内容</p></body></html>'.encode("gbk")+ assert "中文内容" in _frame_document(build_html_preview(payload))+ broken = b"<html><head><meta charset='utf-8'></head><body>\xff\xfe</body></html>"+ assert "\ufffd" in _frame_document(build_html_preview(broken))+++ "payload",+ [+ b"<!DOCTYPE html><html><body><p>no head</p></body></html>",+ b"<p>fragment only</p>",+ b"<html><body><header>site nav</header><p>body</p></body></html>",+ ],+)+def test_html_preview_builds_missing_head_after_doctype(payload: bytes) -> None:+ """缺失 head 时补建注入点,<header> 标签不得被误当作 head。"""+ document = _frame_document(build_html_preview(payload))+ assert "Content-Security-Policy" in document+ if payload.startswith(b"<!DOCTYPE"):+ assert document.lstrip().lower().startswith("<!doctype")+ if b"<header>" in payload:+ assert document.index("Content-Security-Policy") < document.index("<header>")++def test_preview_receipts_and_failure(tmp_path: Path) -> None:"""成功和失败均回传请求标识,预览错误不向解析流程抛出。"""source = tmp_path / "中文.OFD"source.write_bytes(ofd_bytes())ticket = json.dumps({"id": "request-1", "path": str(source)})- receipt = json.loads(asyncio.run(prepare_ofd_preview(str(source), ticket)))+ receipt = json.loads(asyncio.run(prepare_source_preview(str(source), ticket)))assert receipt["id"] == "request-1" and "iframe" in receipt["html"]source.write_bytes(b"bad")- receipt = json.loads(asyncio.run(prepare_ofd_preview(str(source), ticket)))+ receipt = json.loads(asyncio.run(prepare_source_preview(str(source), ticket)))assert "ofd_preview_failed" in receipt["html"]- assert json.loads(asyncio.run(prepare_ofd_preview(None, ticket)))["html"] == ""- assert json.loads(asyncio.run(prepare_ofd_preview(str(source) + "x", ticket)))["html"] == ""--+ assert json.loads(asyncio.run(prepare_source_preview(None, ticket)))["html"] == ""+ assert json.loads(asyncio.run(prepare_source_preview(str(source) + "x", ticket)))["html"] == ""++[email protected]("name", ["page.html", "page.htm", "page.shtml"])+def test_html_preview_receipts(tmp_path: Path, name: str) -> None:+ """三种 HTML 后缀都生成预览回执;文件消失与未知后缀走失败或空回执。"""+ source = tmp_path / name+ source.write_text("<!DOCTYPE html><html><body><p>hello</p></body></html>", encoding="utf-8")+ ticket = json.dumps({"id": "request-2", "path": str(source)})+ receipt = json.loads(asyncio.run(prepare_source_preview(str(source), ticket)))+ assert receipt["id"] == "request-2"+ assert "mineru-source-frame" in receipt["html"] and "Content-Security-Policy" in receipt["html"]+ source.unlink()+ receipt = json.loads(asyncio.run(prepare_source_preview(str(source), ticket)))+ assert "html_preview_failed" in receipt["html"]+ other = tmp_path / "notes.txt"+ other.write_text("plain", encoding="utf-8")+ assert json.loads(asyncio.run(prepare_source_preview(str(other), json.dumps({"id": 1, "path": str(other)}))))["html"] == ""++[email protected]("name", ["source.ofd", "source.html"])@pytest.mark.parametrize("fail", [False, True])-def test_parse_preserves_source_preview(tmp_path: Path, fail: bool) -> None:- """解析成功或失败只更新结果,OFD 源预览始终由独立事件维护。"""+def test_parse_preserves_source_preview(tmp_path: Path, name: str, fail: bool) -> None:+ """解析成功或失败只更新结果,OFD/HTML 源预览始终由独立事件维护。"""from types import SimpleNamespacefrom unittest.mock import AsyncMock@@ -113,8 +202,8 @@ def test_parse_preserves_source_preview(tmp_path: Path, fail: bool) -> None:from mineru.kit.gradio.client import V1ServerCapabilitiesfrom mineru.parser.base import ParseResult- source = tmp_path / "source.ofd"- source.write_bytes(ofd_bytes())+ source = tmp_path / name+ source.write_bytes(ofd_bytes() if name.endswith(".ofd") else b"<html><body><p>hi</p></body></html>")parse = AsyncMock(return_value=ParseResult(middle_json=_middle_json(with_image=False)))if fail:parse.side_effect = ValueError("expected parse failure")@@ -125,8 +214,8 @@ def test_parse_preserves_source_preview(tmp_path: Path, fail: bool) -> None:enable_example=False,)convert = next(fn for fn in demo.fns.values() if fn.name == "convert_handler")- ofd = next(block for block in demo.blocks.values() if "mineru-kit-ofd-preview" in (block.elem_classes or []))- assert ofd not in convert.outputs+ preview_block = next(block for block in demo.blocks.values() if "mineru-kit-source-preview" in (block.elem_classes or []))+ assert preview_block not in convert.outputsupload = next(fn for fn in demo.fns.values() if fn.name == "update_file_preview")updates = upload.fn(str(source))assert all(update.get("visible") is False for update in updates[:4])