refactor: unify document sessions close policy and resource parsing

This commit is contained in:
2026-10-29 19:18:19 +08:00
parent de362536f5
commit f27482857a
11 changed files with 519 additions and 305 deletions
+9 -18
View File
@@ -4,8 +4,8 @@ from __future__ import annotations
import re
from dataclasses import dataclass
from pathlib import Path
from urllib.parse import unquote, urlsplit
from urllib.request import url2pathname
from urllib.parse import urlsplit
from .document_resources import analyze_document, resolve_local_reference
@dataclass(frozen=True)
@@ -15,8 +15,6 @@ class ExportIssue:
def check_export(source: str, path: Path, kind: str, css_source: str = "") -> list[ExportIssue]:
from .markdown_renderer import MarkdownRenderer
from html.parser import HTMLParser
issues: list[ExportIssue] = []
seen: set[tuple[int, str]] = set()
def add(line: int, message: str) -> None:
@@ -32,12 +30,15 @@ def check_export(source: str, path: Path, kind: str, css_source: str = "") -> li
return
if parsed.scheme not in {"", "file"} or not parsed.path:
return
local = Path(url2pathname(unquote(parsed.path))) if parsed.scheme == "file" else path.parent / unquote(parsed.path)
if not local.exists():
local = resolve_local_reference(path, value)
if local is not None and not local.exists():
add(line, "本地图片不存在:" + value if image else "本地链接目标不存在:" + value)
if image and basic:
add(line, "此 Word 导出方式不嵌入图片,建议选择 Pandoc。")
tokens = MarkdownRenderer(path.parent).markdown.parse(source)
analysis = analyze_document(source)
for reference in analysis.references:
target(reference.url, reference.line, reference.image)
tokens = analysis.tokens
list_depth = 0
for token in tokens:
line = token.map[0] + 1 if token.map else 0
@@ -54,22 +55,12 @@ def check_export(source: str, path: Path, kind: str, css_source: str = "") -> li
if basic and token.type == "math_block":
add(line, "此 Word 导出方式不转换公式,建议选择 Pandoc。")
for child in token.children or []:
if child.type == "image":
target(child.attrGet("src") or "", line, True)
elif child.type == "link_open":
target(child.attrGet("href") or "", line, False)
elif basic and child.type == "math_inline":
if basic and child.type == "math_inline":
add(line, "此 Word 导出方式不转换公式,建议选择 Pandoc。")
html_tokens = [token] if token.type == "html_block" else [c for c in token.children or [] if c.type == "html_inline"]
for raw in html_tokens:
if word:
add(line, "含有内嵌 HTML,Word 可能无法保留其内容或布局,请核对导出结果。")
class Links(HTMLParser):
def handle_starttag(self, tag, attrs):
for name, value in attrs:
if name in {"src", "href"} and value:
target(value, line, tag.lower() == "img")
Links().feed(raw.content)
if word and css_source.strip():
from .style_sync import analyze_css, CORE_PROPERTIES
import tinycss2