fix: retain conflicting saves and preserve list content in Word

This commit is contained in:
2026-11-05 11:57:06 +08:00
parent e605c2f8d3
commit b3a0907e1a
11 changed files with 283 additions and 27 deletions
+5 -13
View File
@@ -11,7 +11,7 @@ from urllib.parse import unquote, urlsplit, urlunsplit
from .document_resources import analyze_document, apply_source_edits, resolve_local_reference, SourceEdit
from .doc_check import document_anchors
from .export_jobs import check_cancelled
from .file_versions import atomic_write
from .file_versions import atomic_write, fingerprint, checked_remove
from .markdown_utils import Heading
@@ -88,7 +88,7 @@ def combine_documents(documents: list[BatchDocument], renderer, cancel=None) ->
content = tokens[index + 1].content
headings.append(Heading(level, content, anchor))
start, end = token.map
prefix = re.match(r"[ \t]*(?:>[ \t]*)*", word_source[offsets[start]:]).group()
prefix = re.match(r"[ \t]*(?:>[ \t]*|(?:[-+*]|\d+[.)])[ \t]+)*", word_source[offsets[start]:]).group()
word_edits.append(SourceEdit(offsets[start], offsets[end], f"{prefix}{'#' * level} {next(word_heading_texts)} {{#{anchor}}}\n"))
elif token.type == "heading_close":
token.tag = f"h{min(6, int(token.tag[1:]) + 1)}"
@@ -107,14 +107,6 @@ def combine_documents(documents: list[BatchDocument], renderer, cancel=None) ->
return CombinedDocument("\n\n".join(word), "\n".join(bodies), headings)
def fingerprint(path: Path):
if not path.exists(): return None
digest = hashlib.sha256()
with path.open('rb') as stream:
for block in iter(lambda: stream.read(1024 * 1024), b''): digest.update(block)
return digest.hexdigest()
def publish_outputs(outputs: dict[Path, Path], expected: dict, backup: Path, cancel=None):
"""Publish only after all conversions succeed; restore earlier outputs on any failure."""
check_cancelled(cancel)
@@ -140,7 +132,7 @@ def publish_outputs(outputs: dict[Path, Path], expected: dict, backup: Path, can
# destination again immediately before publishing this particular file.
if fingerprint(destination) != expected[destination]:
raise OSError(f"导出期间目标文件发生变化,已停止替换:{destination}")
atomic_write(destination, data)
atomic_write(destination, data, expected=expected[destination])
written.append((destination, hashlib.sha256(data).hexdigest()))
check_cancelled(cancel)
except Exception as error:
@@ -149,8 +141,8 @@ def publish_outputs(outputs: dict[Path, Path], expected: dict, backup: Path, can
try:
if fingerprint(destination) != published:
raise OSError(f"目标文件已被外部修改,保留当前内容:{destination}")
if originals[destination] is None: destination.unlink(missing_ok=True)
else: atomic_write(destination, originals[destination].read_bytes())
if originals[destination] is None: checked_remove(destination, published)
else: atomic_write(destination, originals[destination].read_bytes(), expected=published)
except OSError as rollback:
failures.append(str(rollback))
if failures: