From 82f5f5a63a3fa6a319582fa832f616d7da6b1807 Mon Sep 17 00:00:00 2001 From: shen Date: Thu, 29 Oct 2026 21:37:30 +0800 Subject: [PATCH] feat: batch and combine Word and PDF exports transactionally --- docs/architecture.md | 2 + mdhelper/batch_documents.py | 150 +++++++++++++++++++++++ mdhelper/batch_export_dialog.py | 206 ++++++++++++++++++++++++++++++++ mdhelper/export_workflow.py | 4 +- mdhelper/main_window.py | 20 ++++ mdhelper/markdown_renderer.py | 4 + mdhelper/pdf_export_task.py | 18 ++- tests/test_batch_export.py | 88 ++++++++++++++ 8 files changed, 489 insertions(+), 3 deletions(-) create mode 100644 mdhelper/batch_documents.py create mode 100644 mdhelper/batch_export_dialog.py create mode 100644 tests/test_batch_export.py diff --git a/docs/architecture.md b/docs/architecture.md index 5191177..76a23f7 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -58,3 +58,5 @@ MHTML 的浏览器加载与归档转换分开。`mhtml_archive.py` 分别处理 `save_conflict.py` 将逐段合并算法与保存冲突对话框封装在一起。编辑器保存流程传入原始、编辑和磁盘版本;确认后再次核对磁盘字节,再交给现有带历史版本的原子保存流程。取消或二次冲突均保留编辑区。 +`batch_documents.py` 负责跨文件资源路径、章节锚点命名和成组输出事务。`batch_export_dialog.py` 持有不可变的文档/样式快照,在工作线程执行 Word 转换,在 Qt 主线程串行驱动已有 PDF 控制器。只有全队列完成后才发布输出;取消和失败可恢复原有目标。Word 合并采用显式章节标识,PDF 使用同一组标识。 + diff --git a/mdhelper/batch_documents.py b/mdhelper/batch_documents.py new file mode 100644 index 0000000..8984c5b --- /dev/null +++ b/mdhelper/batch_documents.py @@ -0,0 +1,150 @@ +"""Immutable multi-document composition and transactional publication.""" +from __future__ import annotations +import hashlib +import html +import re +import shutil +from dataclasses import dataclass +from pathlib import Path +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 .markdown_utils import Heading + + +@dataclass(frozen=True) +class BatchDocument: + path: Path + source: str + + +@dataclass +class CombinedDocument: + word_source: str + html_body: str + headings: list[Heading] + + +def combine_documents(documents: list[BatchDocument], renderer, cancel=None) -> CombinedDocument: + documents = [BatchDocument(d.path.resolve(), d.source) for d in documents] + mappings, analyses = {}, {} + for index, document in enumerate(documents, 1): + check_cancelled(cancel) + analysis = analyses[document.path] = analyze_document(document.source) + mapping = {"": f"mdh{index}"} + for number, token in enumerate(t for t in analysis.tokens if t.type == "heading_open"): + if token.attrGet("id"): + mapping[token.attrGet("id")] = f"mdh{index}h{number + 1}" + for number, anchor in enumerate(sorted(document_anchors(analysis) - mapping.keys()), 1): + mapping[anchor] = f"mdh{index}a{number}" + mappings[document.path] = mapping + word, bodies, headings = [], [], [] + for document in documents: + check_cancelled(cancel) + mapping = mappings[document.path] + edits, word_resource_edits = [], [] + for reference in analyses[document.path].references: + parsed = urlsplit(reference.url) + target = resolve_local_reference(document.path, reference.url) + local = parsed.scheme in {"", "file"} and (not parsed.netloc or parsed.scheme == "file") + if local and not parsed.path: + target = document.path + new_url = reference.url + if not reference.image and target in mappings and unquote(parsed.fragment) in mappings[target]: + new_url = "#" + mappings[target][unquote(parsed.fragment)] + elif target is not None and parsed.path: + new_url = urlunsplit(urlsplit(target.as_uri())._replace(query=parsed.query, fragment=parsed.fragment)) + # Expand reference-style links before joining documents with identical definitions. + if new_url != reference.url or reference.syntax == "reference": + edits.append(reference.edit_url(new_url)) + word_url = new_url + if reference.image and target is not None: + # Pandoc does not decode file-URI escapes on Windows. Angle-bracket + # destinations preserve literal Unicode, spaces and parentheses. + word_url = target.as_posix() + if reference.syntax != 'html' and document.source[reference.start - 1:reference.start] != '<': + word_url = '<' + word_url + '>' + word_resource_edits.append(reference.edit_url(word_url)) + source = apply_source_edits(document.source, edits) + word_source = apply_source_edits(document.source, word_resource_edits) + env = {"path": str(document.path), "headings": []} + tokens = renderer.markdown.parse(source, env) + offsets = [0] + for line in word_source.splitlines(keepends=True): offsets.append(offsets[-1] + len(line)) + word_tokens = renderer.markdown.parse(word_source, {}) + word_heading_texts = iter(word_tokens[i + 1].content for i, token in enumerate(word_tokens) if token.type == 'heading_open') + word_edits = [] + heading_number = 0 + for index, token in enumerate(tokens): + if token.type == "heading_open": + heading_number += 1 + anchor = f"{mapping['']}h{heading_number}" + token.attrSet("id", anchor) + level = min(6, int(token.tag[1:]) + 1) + token.tag = f"h{level}" + 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() + 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)}" + for raw in [token, *(token.children or [])]: + if raw.type in {"html_inline", "html_block"}: + raw.content = re.sub(r'\b(id|name)\s*=\s*([\"\x27])([^\"\x27]+)\2', + lambda m: f'{m[1]}={m[2]}{mapping.get(html.unescape(m[3]), m[3])}{m[2]}', raw.content) + title = document.path.stem + chapter = Heading(1, title, mapping[""]) + first = len(headings) - sum(1 for token in tokens if token.type == "heading_open") + headings.insert(first, chapter) + safe_title = re.sub(r'([\\`*_{}\[\]<>#])', r'\\\1', title) + word.append(f"# {safe_title} {{#{mapping['']}}}\n\n" + apply_source_edits(word_source, word_edits)) + bodies.append(f'

{html.escape(title)}

' + + renderer.markdown.renderer.render(tokens, renderer.markdown.options, env) + '
') + 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) + originals = {} + for index, destination in enumerate(outputs): + check_cancelled(cancel) + if fingerprint(destination) != expected[destination]: + raise OSError(f"导出期间目标文件发生变化,已停止替换:{destination}") + if destination.exists(): + copy = backup / f"previous-{index}" + shutil.copyfile(destination, copy) + originals[destination] = copy + else: + originals[destination] = None + written = [] + try: + for destination, staged in outputs.items(): + check_cancelled(cancel) + atomic_write(destination, staged.read_bytes()) + written.append(destination) + check_cancelled(cancel) + except Exception as error: + failures = [] + for destination in reversed(written): + try: + if originals[destination] is None: destination.unlink(missing_ok=True) + else: atomic_write(destination, originals[destination].read_bytes()) + except OSError as rollback: + failures.append(str(rollback)) + if failures: + raise OSError(f"输出恢复失败,备份位于 {backup}:{';'.join(failures)}") from error + raise + return list(outputs) diff --git a/mdhelper/batch_export_dialog.py b/mdhelper/batch_export_dialog.py new file mode 100644 index 0000000..7e1af3b --- /dev/null +++ b/mdhelper/batch_export_dialog.py @@ -0,0 +1,206 @@ +"""On-demand export queue; only final publication can touch requested output paths.""" +from __future__ import annotations +import copy +import shutil +import tempfile +import threading +from pathlib import Path + +from PySide6.QtCore import Qt, QThreadPool, Slot, QTimer +from PySide6.QtWidgets import (QCheckBox, QComboBox, QDialog, QFileDialog, QHBoxLayout, QLabel, + QListWidget, QListWidgetItem, QMessageBox, QPushButton, QVBoxLayout) +from .batch_documents import BatchDocument, combine_documents, fingerprint, publish_outputs +from .document_tree import discover_documents +from .docx_exporter import export_markdown_to_docx +from .export_check import check_export, confirm_export, ExportIssue +from .export_jobs import ExportJob, check_cancelled +from .export_workflow import ExportSnapshot +from .pdf_export_task import PdfExportController + + +class BatchExportDialog(QDialog): + def __init__(self, window): + super().__init__(window) + self.window = window + self.job = self.pdf = None + self.directory = None + self.cancel_event = threading.Event() + self.running = False + self.setWindowTitle("批量与合并导出") + self.resize(750, 650) + layout = QVBoxLayout(self) + layout.addWidget(QLabel("勾选文件并调整顺序;导出使用当前未保存的编辑内容和当前样式。")) + self.files = QListWidget() + for entry in discover_documents(window.project_root, ("markdown",)): + if entry.path.suffix.lower() not in {".md", ".markdown"}: continue + item = QListWidgetItem(entry.relative_path) + item.setData(Qt.ItemDataRole.UserRole, entry.path) + item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable) + item.setCheckState(Qt.CheckState.Checked if entry.path == window.current_file else Qt.CheckState.Unchecked) + self.files.addItem(item) + layout.addWidget(self.files) + order = QHBoxLayout() + for title, callback in (("上移", lambda: self.move(-1)), ("下移", lambda: self.move(1)), + ("全选", lambda: self.select_all(True)), ("全不选", lambda: self.select_all(False))): + button = QPushButton(title); button.clicked.connect(callback); order.addWidget(button) + layout.addLayout(order) + self.format = QComboBox(); self.format.addItem("Word(Pandoc)", "docx"); self.format.addItem("PDF", "pdf") + self.combined = QCheckBox("合并为一份文档(按文件分章,统一目录和页码)") + layout.addWidget(self.format); layout.addWidget(self.combined) + self.status = QLabel("尚未开始"); self.status.setWordWrap(True); layout.addWidget(self.status) + actions = QHBoxLayout() + self.start_button = QPushButton("选择输出位置并导出") + self.stop_button = QPushButton("取消导出"); self.stop_button.setEnabled(False) + self.start_button.clicked.connect(self.start); self.stop_button.clicked.connect(self.cancel) + close = QPushButton("关闭"); close.clicked.connect(self.close) + for button in (self.start_button, self.stop_button, close): actions.addWidget(button) + layout.addLayout(actions) + + def move(self, delta): + if self.running: return + row = self.files.currentRow(); new = row + delta + if row >= 0 and 0 <= new < self.files.count(): + self.files.insertItem(new, self.files.takeItem(row)); self.files.setCurrentRow(new) + + def select_all(self, checked): + if self.running: return + for index in range(self.files.count()): + self.files.item(index).setCheckState(Qt.CheckState.Checked if checked else Qt.CheckState.Unchecked) + + def start(self): + paths = [self.files.item(i).data(Qt.ItemDataRole.UserRole) for i in range(self.files.count()) + if self.files.item(i).checkState() == Qt.CheckState.Checked] + if self.running or not paths: return + settings = self.window.exports._choose_page_settings() + if settings is None: return + suffix = self.format.currentData() + if self.combined.isChecked(): + filename, _ = QFileDialog.getSaveFileName(self, "合并导出", str(self.window.project_root / f"合并文档.{suffix}"), f"{suffix.upper()} (*.{suffix})") + if not filename: return + destinations = [Path(filename).with_suffix('.' + suffix)] + else: + folder = QFileDialog.getExistingDirectory(self, "选择批量导出文件夹") + if not folder: return + destinations = [Path(folder) / f"{index:03d}-{path.stem}.{suffix}" for index, path in enumerate(paths, 1)] + existing = [p.name for p in destinations if p.exists()] + if existing and QMessageBox.question(self, "替换已有导出文件?", '\n'.join(existing), + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, QMessageBox.StandardButton.No) != QMessageBox.StandardButton.Yes: + return + try: + buffers = self.window._editor_sources() + documents = [BatchDocument(path, buffers[path] if path in buffers else path.read_text(encoding='utf-8-sig')) for path in paths] + issues = [] + style = copy.deepcopy(self.window.custom_document_styles.get(self.window.document_style_id, {})) + for document in documents: + issues.extend(ExportIssue(issue.line, f"{document.path.name}:{issue.message}") for issue in + check_export(document.source, document.path, 'pandoc' if suffix == 'docx' else 'pdf', style.get('css', ''))) + if not confirm_export(self, issues): return + snapshot = ExportSnapshot(paths[0], documents[0].source, style, self.window.document_font_family, + self.window.document_font_size, self.window.document_style_id, self.window._render_options()) + self.begin(documents, destinations, suffix, self.combined.isChecked(), snapshot, settings) + except (OSError, ValueError) as exc: + QMessageBox.warning(self, "无法导出", str(exc)) + + def begin(self, documents, destinations, kind, combined, snapshot, settings): + if self.window.exports._pdf_controller is not None: + raise ValueError("请先完成或取消当前 PDF 导出。") + self.expected = {path: fingerprint(path) for path in destinations} + self.directory = Path(tempfile.mkdtemp(prefix='mdhelper-batch-')) + self.outputs = {path: self.directory / f'{i}.{kind}' for i, path in enumerate(destinations)} + self.documents, self.snapshot, self.page_settings = documents, snapshot, settings + self.cancel_event.clear(); self.running = True + self.start_button.setEnabled(False); self.stop_button.setEnabled(True) + self.files.setEnabled(False); self.format.setEnabled(False); self.combined.setEnabled(False) + self.status.setText("正在准备文档…") + self.pdf_queue = [] + from .markdown_renderer import MarkdownRenderer + renderer = MarkdownRenderer(self.window.app_root) + def prepare(cancel, progress): + composition = combine_documents(documents, renderer, cancel) if combined else None + if kind == 'docx': + jobs = [(snapshot.path, composition.word_source)] if composition else [(d.path, d.source) for d in documents] + for index, ((source_path, source), staged) in enumerate(zip(jobs, self.outputs.values()), 1): + check_cancelled(cancel); progress(f"正在导出 Word:{index}/{len(jobs)}") + export_markdown_to_docx(source, staged, engine='pandoc', source_path=source_path, + style_rules=snapshot.style.get('rules', {}), document_font_family=snapshot.font, + document_font_size=snapshot.font_size, preset=snapshot.preset, + page_settings=settings, cancel=cancel) + return ('publish', None) + queue = [] + if composition: + content = renderer._wrap_html(body=composition.html_body, title='合并文档', **snapshot.rendering.kwargs()) + queue.append((next(iter(self.outputs.values())), content, composition.headings, documents[0].path)) + else: + from .markdown_utils import Heading + for document, staged in zip(documents, self.outputs.values()): + check_cancelled(cancel) + env = {'path': str(document.path), 'headings': []} + renderer.markdown.parse(document.source, env) + content = renderer.render_source(document.source, document.path, **snapshot.rendering.kwargs()) + queue.append((staged, content, [Heading(**h) for h in env['headings']], document.path)) + return ('pdf', queue) + self.run_job(prepare) + + def run_job(self, callback): + self.job = ExportJob(callback) + self.job.cancel = self.cancel_event + self.job.signals.progress.connect(self.status.setText) + self.job.signals.finished.connect(self.job_finished) + QThreadPool.globalInstance().start(self.job) + + @Slot(object, str) + def job_finished(self, result, error): + self.job = None + if result and result[0] == 'complete' and not error: + self.finish('', success=True); return + if error or self.cancel_event.is_set() or result is None: + self.finish(error); return + stage, value = result + if stage == 'pdf': + self.pdf_queue = value + self.next_pdf() + elif stage == 'publish': + self.publish() + else: + self.finish('', success=True) + + def next_pdf(self): + if self.cancel_event.is_set(): self.finish(''); return + if not self.pdf_queue: self.publish(); return + output, content, headings, source = self.pdf_queue.pop(0) + self.status.setText(f"正在生成 PDF:{source.name},剩余 {len(self.pdf_queue)} 份") + self.pdf = PdfExportController(self.window.exports, output, content, headings, self.page_settings, source, self.pdf_finished) + self.window.exports._pdf_controller = self.pdf + self.pdf.start() + + def pdf_finished(self, result, error): + self.pdf = None + if result is None or error or self.cancel_event.is_set(): self.finish(error) + else: QTimer.singleShot(0, self, self.next_pdf) + + def publish(self): + self.status.setText("全部文档转换完成,正在保存输出…") + def commit(cancel, progress): + publish_outputs(self.outputs, self.expected, self.directory, cancel) + return ('complete', None) + self.run_job(commit) + + def finish(self, error, success=False): + if self.directory and '输出恢复失败' not in error: + shutil.rmtree(self.directory, ignore_errors=True) + self.directory = None + self.running = False + self.start_button.setEnabled(True); self.stop_button.setEnabled(False) + self.files.setEnabled(True); self.format.setEnabled(True); self.combined.setEnabled(True) + self.status.setText(f"已导出 {len(self.outputs)} 份文件:\n" + '\n'.join(str(p) for p in self.outputs) if success else + (f"导出失败:{error}" if error else "已取消,原有输出文件保持不变。")) + + def cancel(self): + self.cancel_event.set() + if self.pdf: self.pdf.cancel() + + def closeEvent(self, event): + if self.running: + self.cancel(); self.status.setText("正在取消并清理临时文件…") + event.ignore() + else: super().closeEvent(event) diff --git a/mdhelper/export_workflow.py b/mdhelper/export_workflow.py index 42fd6fb..31bb3bd 100644 --- a/mdhelper/export_workflow.py +++ b/mdhelper/export_workflow.py @@ -93,7 +93,7 @@ class ExportController: for name in ("top", "bottom", "left", "right"): self.context.settings.setValue(f"pdf/{name}_margin_mm", getattr(margins, name)) - def _start_export_job(self, title, callback, completed=None): + def _start_export_job(self, title, callback, completed=None, outcome=None): job = ExportJob(callback) progress = QProgressDialog(title, "取消", 0, 0, self.context.parent) progress.setWindowTitle("导出文档") @@ -115,6 +115,8 @@ class ExportController: self.context.status("已取消导出,原有输出文件保持不变") if completed: completed() + if outcome: + outcome(result, error, job.cancel.is_set()) job.signals.finished.connect(finished) progress.show() QThreadPool.globalInstance().start(job) diff --git a/mdhelper/main_window.py b/mdhelper/main_window.py index 2b7765d..08a3e47 100644 --- a/mdhelper/main_window.py +++ b/mdhelper/main_window.py @@ -375,6 +375,10 @@ class MainWindow(QMainWindow): self.export_docx_action.triggered.connect(self.export_current_docx) file_menu.addAction(self.export_docx_action) + self.batch_export_action = QAction("批量与合并导出…", self) + self.batch_export_action.triggered.connect(self.show_batch_export) + file_menu.addAction(self.batch_export_action) + self.export_mhtml_action = QAction("导出 MHTML", self) self.export_mhtml_action.triggered.connect(self.export_current_mhtml) file_menu.addAction(self.export_mhtml_action) @@ -3855,6 +3859,18 @@ class MainWindow(QMainWindow): for index in range(self.tabs.count()) if isinstance((widget := self.tabs.widget(index)), RichMarkdownEditorDialog)} + def show_batch_export(self) -> None: + if not self.has_project: + return + existing = getattr(self, "_batch_export_dialog", None) + if existing is not None and existing.isVisible(): + existing.raise_() + existing.activateWindow() + return + from .batch_export_dialog import BatchExportDialog + self._batch_export_dialog = BatchExportDialog(self) + self._batch_export_dialog.show() + def show_document_check(self) -> None: if not self.has_project: return @@ -3909,6 +3925,7 @@ class MainWindow(QMainWindow): self.back_action.setEnabled(history.can_go_back) self.forward_action.setEnabled(history.can_go_forward) self.check_documents_action.setEnabled(self.has_project) + self.batch_export_action.setEnabled(self.has_project) if hasattr(self, "new_file_action"): self.new_file_action.setEnabled(self.has_project) if hasattr(self, "edit_current_action"): @@ -4349,6 +4366,9 @@ class MainWindow(QMainWindow): if self.has_project and not self._restoring_workspace: self.workspaces.save(self.project_root, *self._workspace_snapshot()) self.exports.cancel_all() + batch_dialog = getattr(self, "_batch_export_dialog", None) + if batch_dialog is not None: + batch_dialog.cancel() check_dialog = getattr(self, "_document_check_dialog", None) if check_dialog is not None: check_dialog.close() diff --git a/mdhelper/markdown_renderer.py b/mdhelper/markdown_renderer.py index 7f95df9..bd0f422 100644 --- a/mdhelper/markdown_renderer.py +++ b/mdhelper/markdown_renderer.py @@ -46,6 +46,10 @@ class MarkdownRenderer: "breaks": False, }, ).enable(["table", "strikethrough"]).use(cjk_strong_plugin) + # Local documents and combined exports use file URIs for images. Keep + # MarkdownIt's protection against executable schemes while allowing these. + validate_link = self.markdown.validateLink + self.markdown.validateLink = lambda url: urlsplit(url).scheme.lower() == "file" or validate_link(url) self.markdown.renderer.rules["fence"] = self._render_fence self.markdown.renderer.rules["code_block"] = self._render_code_block diff --git a/mdhelper/pdf_export_task.py b/mdhelper/pdf_export_task.py index f6a7680..5e50cce 100644 --- a/mdhelper/pdf_export_task.py +++ b/mdhelper/pdf_export_task.py @@ -10,7 +10,7 @@ from .file_versions import atomic_write from .pdf_export import build_pdf_export_html, build_pdf_page_layout, extract_heading_page_numbers class PdfExportController: - def __init__(self, owner, output, content, headings, settings, source_path): + def __init__(self, owner, output, content, headings, settings, source_path, completed=None): self.owner, self.output, self.content, self.settings = owner, output, content, settings self.headings = [h for h in headings if h.level <= settings.toc_depth] if settings.include_toc else [] self.base_url = QUrl.fromLocalFile(str(source_path.parent.resolve()) + os.sep) @@ -20,6 +20,7 @@ class PdfExportController: self.lifecycle = ExportLifecycle(owner.context.parent, "导出 PDF", "正在准备 PDF 页面…", self.cancel, self.timeout) self.progress = self.lifecycle.progress self.job = None + self.completed, self.result, self.error = completed, None, "" def start(self): from PySide6.QtWebEngineCore import QWebEnginePage @@ -90,13 +91,20 @@ class PdfExportController: check_cancelled(cancel) atomic_write(self.output, result) return self.output - self.job = self.owner._start_export_job("正在完成 PDF…", decorate, self.cleanup) + self.job = self.owner._start_export_job("正在完成 PDF…", decorate, outcome=self.job_finished) + + def job_finished(self, result, error, cancelled): + self.result = result if not cancelled else None + self.error = error + self.job = None + self.cleanup() def timeout(self): if self.page is not None and not self.cancelled: self.fail("PDF 导出超时。") def fail(self, message): + self.error = message self.cancel() QMessageBox.warning(self.owner.context.parent, "导出失败", message) @@ -110,11 +118,17 @@ class PdfExportController: self.page.deleteLater(); self.page = None if getattr(self.owner, "_pdf_controller", None) is self: self.owner._pdf_controller = None + if self.completed: + callback, self.completed = self.completed, None + callback(self.result, self.error) def cancel(self): if self.cancelled: return if self.job: + self.cancelled = True self.job.cancel.set() + self.lifecycle.close() + return self.owner.context.status("已取消 PDF 导出,原有输出文件保持不变") self.cleanup() diff --git a/tests/test_batch_export.py b/tests/test_batch_export.py new file mode 100644 index 0000000..a9324cc --- /dev/null +++ b/tests/test_batch_export.py @@ -0,0 +1,88 @@ +import threading +import time +import zipfile +from pathlib import Path +import pytest +from PIL import Image +from PySide6.QtCore import QSettings +from mdhelper.batch_documents import BatchDocument, combine_documents, fingerprint, publish_outputs +from mdhelper.batch_export_dialog import BatchExportDialog +from mdhelper.docx_exporter import export_markdown_to_docx +from mdhelper.export_jobs import ExportCancelled +from mdhelper.export_settings import PageSettings +from mdhelper.markdown_renderer import MarkdownRenderer +from mdhelper import main_window, batch_documents + + +def documents(tmp_path): + first, second = tmp_path / 'first.md', tmp_path / 'sub' / 'second.md' + second.parent.mkdir() + Image.new('RGB', (16, 16), 'red').save(second.parent / '图.png') + first.write_text('# Shared\n\n[chapter](sub/second.md)\n\n[other](sub/second.md#shared)\n\n[ref][same]\n\n[same]: sub/second.md#shared', encoding='utf-8') + second.write_text('# Shared\n\n![图](图.png)\n\n[ref][same]\n\n[same]: ../first.md#shared\n\n```\n[code](../first.md)\n```', encoding='utf-8') + return [BatchDocument(path, path.read_text(encoding='utf-8')) for path in (first, second)] + + +def test_combined_resources_duplicate_headings_and_real_docx(tmp_path): + docs = documents(tmp_path) + combined = combine_documents(docs, MarkdownRenderer(tmp_path)) + assert [h.slug for h in combined.headings] == ['mdh1', 'mdh1h1', 'mdh2', 'mdh2h1'] + assert 'href="#mdh2h1"' in combined.html_body and 'href="#mdh1h1"' in combined.html_body + assert 'id="mdh2h1"' in combined.html_body and '(#mdh2)' in combined.word_source + assert '[code](../first.md)' in combined.word_source + output = tmp_path / 'combined.docx' + export_markdown_to_docx(combined.word_source, output, engine='pandoc', source_path=docs[0].path, + page_settings=PageSettings(header='Batch header', footer='Batch footer')) + with zipfile.ZipFile(output) as archive: + xml = archive.read('word/document.xml').decode() + assert 'w:anchor="mdh2h1"' in xml and 'w:name="mdh2h1"' in xml + assert any(name.startswith('word/media/') for name in archive.namelist()) + assert 'TOC ' in xml + assert 'Batch header' in archive.read('word/header1.xml').decode() + + +def test_publication_cancellation_race_and_rollback(tmp_path, monkeypatch): + destinations = [tmp_path / f'{i}.out' for i in range(2)] + staged = [tmp_path / f'{i}.stage' for i in range(2)] + for path in destinations: path.write_bytes(b'old') + for path in staged: path.write_bytes(b'new') + outputs = dict(zip(destinations, staged)); expected = {p: fingerprint(p) for p in destinations} + cancel = threading.Event(); cancel.set() + with pytest.raises(ExportCancelled): publish_outputs(outputs, expected, tmp_path, cancel) + destinations[0].write_bytes(b'external') + with pytest.raises(OSError, match='发生变化'): publish_outputs(outputs, expected, tmp_path) + destinations[0].write_bytes(b'old') + real_write = batch_documents.atomic_write + def fail(path, data): + if path == destinations[1] and data == b'new': raise OSError('disk full') + real_write(path, data) + monkeypatch.setattr(batch_documents, 'atomic_write', fail) + with pytest.raises(OSError, match='disk full'): publish_outputs(outputs, expected, tmp_path) + assert all(path.read_bytes() == b'old' for path in destinations) + + +@pytest.mark.parametrize('kind,combined', [('docx', False), ('pdf', True), ('pdf', False)]) +def test_queue_generates_complete_outputs(qt_application, tmp_path, monkeypatch, kind, combined): + docs = documents(tmp_path) + settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat) + monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings) + errors = [] + monkeypatch.setattr(main_window.QMessageBox, 'warning', lambda *args: errors.append(str(args[2]))) + window = main_window.MainWindow(tmp_path) + panel = BatchExportDialog(window) + try: + window.open_document(docs[0].path) + output = [tmp_path / f'export-{i}.{kind}' for i in range(1 if combined else 2)] + panel.begin(docs, output, kind, combined, window._export_snapshot(), PageSettings(header='Batch', include_toc=True)) + deadline = time.monotonic() + 60 + while panel.running and time.monotonic() < deadline: + qt_application.processEvents(); time.sleep(.02) + assert not panel.running, panel.status.text() + assert not errors and all(path.exists() for path in output), (panel.status.text(), errors) + if kind == 'pdf': + from pypdf import PdfReader + text = '\n'.join(page.extract_text() for path in output for page in PdfReader(path).pages) + assert 'Shared' in text and 'Batch' in text + assert sum(len(page.images) for path in output for page in PdfReader(path).pages) >= 1 + finally: + panel.cancel(); window.close()