diff --git a/README.md b/README.md index 0d88082..e98a8cd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # MDHelper -当前版本:`0.3.1` +当前版本:`0.4.0` MDHelper 是一个本地 Markdown / 代码文档阅读器,使用 PySide6 构建桌面窗口,用 Qt WebEngine 展示由 Markdown 转换得到的 HTML 页面。它面向本地项目文档、技术手册、源码说明和离线帮助文件,重点是离线可用、多标签阅读、全文搜索、样式管理和导出。 @@ -15,6 +15,12 @@ MDHelper 是一个本地 Markdown / 代码文档阅读器,使用 PySide6 构 - 代码块和代码文件使用 Pygments 高亮,代码块支持行号、复制、展开/收起。 - Python 文件可提取类/函数作为目录;C/C++ 文件可提取基础函数、类、结构体目录;Fortran 文件可提取 program、module、type、subroutine、function 目录。 - 多标签页阅读,支持后退/前进、书签、最近文件和恢复滚动位置。 +- 按项目恢复上次标签顺序、当前文件、编辑模式和分屏状态;后台标签在点击后加载。“文件”菜单可关闭启动恢复。 +- “工具 → 检查文档链接与图片”后台检查项目中的 Markdown/HTML 本地引用及章节锚点;双击结果定位源码,可随时取消。 +- 文件树重命名会预览并同步更新 Markdown/HTML 引用、已打开标签和导航记录;操作前保留备份,失败时恢复。 +- 保存发生外部冲突时,可对照磁盘与编辑内容,逐段选择并编辑合并结果;取消不会丢失编辑内容。 +- “文件 → 批量与合并导出”支持排序后导出多份 Word/PDF,或合成带统一目录、页码和样式的文档;合并时处理图片路径和 Markdown 章节链接。 +- “工具 → 项目样式与导出预设”保存各项目的字体、段落、高级 CSS 和页面设置;切换项目自动恢复,模板可导入、导出。 - 左侧文件树支持按用户选择的文件类型过滤,搜索支持标题、文件名和正文全文搜索。 - 当前页查找支持上一个/下一个命中并显示命中数量。 - 自动刷新当前文档、已打开文档和引用资源;外部修改后会重新读取并渲染。 @@ -64,6 +70,15 @@ MDHelper 是一个本地 Markdown / 代码文档阅读器,使用 PySide6 构 - 全部替换和历史版本恢复可撤销;安全保存保留最近 20 个保存前版本。 - 四种导出均使用当前编辑内容。Word / PDF 共用纸张、方向、页边距、目录及页眉页脚设置,并提供导出前检查和可取消的生成流程。 +## 项目设置与批量导出 + +- 工作现场在正常退出或切换项目时保存;未保存内容仍由原有草稿恢复流程保护。指定启动文件时优先打开该文件。 +- 启用“以当前设置启用项目默认”后,字体、段落和页面修改自动保存在当前项目;未启用的项目使用全局默认。模板是独立副本,修改一个项目不会改动其他项目。 +- 样式预设 JSON 包含字体、核心样式/高级 CSS 和页面设置,不包含项目路径、书签或草稿。高级 CSS 在 Word 中仍受其排版能力限制,导出前会提示不支持的效果。 +- 批量导出使用当前编辑内容;分别导出时文件名带顺序编号。全部转换成功后才发布输出,目标文件在导出期间发生变化会停止替换。 +- 合并导出按文件分章,保留同名标题的独立目标,图片按各自原文档目录解析。Word 目录需要在 Word 中“更新域”以计算最终页码。内嵌 HTML、Mermaid 等限制会继续出现在导出前检查中。 +- 重命名备份位于 `%LOCALAPPDATA%\MDHelper\rename-backups`;每次操作的 `manifest.json` 记录原路径,编号 `.bak` 文件保存原始内容。 + ## 快捷键 | 快捷键 | 功能 | @@ -114,10 +129,10 @@ MDHelper 是一个本地 Markdown / 代码文档阅读器,使用 PySide6 构 一键生成 Windows 安装程序(推荐): ```powershell -.\pack\_installer.cmd +.\pack_installer.cmd ``` -也可直接双击 `pack\_installer.cmd`。脚本自动检查依赖、运行回归测试、生成程序目录并制作安装 EXE。 +也可直接双击根目录的 `pack_installer.cmd`。脚本自动检查依赖、运行回归测试、生成程序目录并制作安装 EXE;原有 `pack\_installer.cmd` 入口仍可使用。 首次缺少 Inno Setup 时会从官方来源下载、校验发布者签名并安装到 `pack\.tools`;后续构建复用。 需要可用的 64 位 Python 3.10+;兼容当前项目的两种 `mdhelper_env` 目录布局。 diff --git a/docs/user_manual.md b/docs/user_manual.md index 3d62aee..538b909 100644 --- a/docs/user_manual.md +++ b/docs/user_manual.md @@ -278,3 +278,12 @@ MDHelper 会保存: .\dist\MDHelper\MDHelper.exe --check --project-root . ``` +# 0.4 新增工作流 + +- **恢复工作现场**:正常退出后,再次打开同一文件夹会恢复标签顺序、当前文档、编辑模式和分屏状态。后台标签在点击时加载;可在“文件 → 启动时恢复工作现场”中关闭。未保存草稿仍按原有方式提示恢复。 +- **文档检查**:在“工具 → 检查文档链接与图片”中扫描当前项目,检查本地链接、图片和章节锚点。检查使用当前编辑内容,双击结果可定位 Markdown 编辑行或查看 HTML 源码。 +- **安全重命名**:在文件树重命名时,若存在需要同步修改的引用,会显示差异预览。确认后统一保存这些修改,并更新标签、最近文件和书签。备份位于本地应用数据目录的 `MDHelper/rename-backups`。 +- **保存冲突对比**:磁盘被其他程序修改或删除时,保存前展示两份内容。勾选差异段落采用磁盘版本,也可以编辑合并结果;取消会保留当前编辑内容。 +- **批量与合并导出**:在“文件 → 批量与合并导出”中勾选 Markdown 文件,调整顺序并选择 Word/PDF。默认分别输出,也可勾选合并为一份文档。使用当前样式和编辑内容,全部转换成功后才保存输出。 +- **项目样式与导出预设**:在“工具 → 项目样式与导出预设”中启用项目默认,或应用预设模板。启用后,在主窗口调整字体、段落和样式,会自动记入当前项目;页面参数与 Word/PDF 共用。模板可独立导入和导出,切换项目会恢复各自的设置。 + diff --git a/mdhelper/__init__.py b/mdhelper/__init__.py index fd6ffd5..c0f411c 100644 --- a/mdhelper/__init__.py +++ b/mdhelper/__init__.py @@ -1,3 +1,3 @@ """MDHelper package.""" -__version__ = "0.3.1" +__version__ = "0.4.0" diff --git a/mdhelper/batch_export_dialog.py b/mdhelper/batch_export_dialog.py index 7e1af3b..42ec636 100644 --- a/mdhelper/batch_export_dialog.py +++ b/mdhelper/batch_export_dialog.py @@ -75,17 +75,18 @@ class BatchExportDialog(QDialog): 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})") + filename, _ = QFileDialog.getSaveFileName(self, "合并导出", str(self.window.project_root / f"合并文档.{suffix}"), + f"{suffix.upper()} (*.{suffix})", options=QFileDialog.Option.DontConfirmOverwrite) 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 + existing = [str(p) 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] diff --git a/mdhelper/main_window.py b/mdhelper/main_window.py index 5e2c560..217b454 100644 --- a/mdhelper/main_window.py +++ b/mdhelper/main_window.py @@ -2115,8 +2115,10 @@ class MainWindow(QMainWindow): ) -> bool: original_path = editor.session.path target_path = target_path.resolve() + disk_source = None try: - unchanged = target_path == original_path and source == self._read_markdown_source(original_path) + disk_source = self._read_markdown_source(original_path) + unchanged = target_path == original_path and source == disk_source except (OSError, UnicodeError): unchanged = False if unchanged: @@ -2126,7 +2128,8 @@ class MainWindow(QMainWindow): return True reviewed_disk = None - if target_path == original_path and expected_signature != self._file_signature(target_path): + if target_path == original_path and (expected_signature != self._file_signature(target_path) + or disk_source != editor.session.saved_source): from .save_conflict import resolve_save_conflict try: disk_bytes = target_path.read_bytes() if target_path.exists() else None @@ -2706,7 +2709,8 @@ class MainWindow(QMainWindow): QMessageBox.warning(self, "导入失败", "配置文件格式不正确。") return - self._apply_configuration(payload) + if self._apply_configuration(payload) is False: + return self._save_preferences() self._update_recent_menu() self._update_bookmarks_menu() @@ -2760,13 +2764,14 @@ class MainWindow(QMainWindow): def _configuration_snapshot(self) -> dict[str, object]: return self._configuration_data().to_payload(__version__) - def _apply_configuration(self, payload: dict[str, object]) -> None: + def _apply_configuration(self, payload: dict[str, object]) -> bool: config = ApplicationConfiguration.from_payload(payload, self._configuration_data()) if config.project_root and Path(config.project_root).exists(): if not self.set_project_root(Path(config.project_root), save=True): - return + return False self._apply_configuration_data(config) self._set_project_page_settings(config.page_settings) + return True def _is_int_like(self, value: object) -> bool: try: @@ -3935,6 +3940,8 @@ class MainWindow(QMainWindow): self.tabs.setCurrentIndex(index) else: self.open_document(path, new_tab=True) + if document_path(self.tabs.currentWidget()) != path.resolve(): + return if not isinstance(self.tabs.currentWidget(), RichMarkdownEditorDialog): self.edit_current_markdown() widget = self.tabs.currentWidget() diff --git a/pack/README.md b/pack/README.md index a28b8da..8339e44 100644 --- a/pack/README.md +++ b/pack/README.md @@ -1,6 +1,6 @@ # Windows 安装版构建 -双击本目录的 `_installer.cmd`,或在任意工作目录调用它的完整路径。 +双击项目根目录的 `pack_installer.cmd`,或在任意工作目录调用它的完整路径。本目录的 `_installer.cmd` 作为原有入口仍可使用。 成功、失败都会显示结果并保留窗口;失败返回非零退出码,并停止后续打包。 ## 产物 @@ -32,8 +32,8 @@ ## 自动化和自定义 ```bat -pack\_installer.cmd -NoPause -pack\_installer.cmd -NoPause -SkipTests +pack_installer.cmd -NoPause +pack_installer.cmd -NoPause -SkipTests ``` `-SkipTests` 只用于明确需要跳过已验证测试的重复构建。默认仍会执行测试。 @@ -44,7 +44,7 @@ pack\_installer.cmd -NoPause -SkipTests ```bat set "MDHELPER_PYTHON=C:\Path\To\python.exe" set "MDHELPER_ISCC=C:\Path\To\ISCC.exe" -pack\_installer.cmd +pack_installer.cmd ``` 如果指定的是系统 Python,脚本先在 `pack/.venv` 创建独立构建环境。 diff --git a/pack_installer.cmd b/pack_installer.cmd new file mode 100644 index 0000000..20e54b2 --- /dev/null +++ b/pack_installer.cmd @@ -0,0 +1,4 @@ +@echo off +setlocal +call "%~dp0pack\_installer.cmd" %* +exit /b %ERRORLEVEL% diff --git a/tests/test_batch_export.py b/tests/test_batch_export.py index a9324cc..b770820 100644 --- a/tests/test_batch_export.py +++ b/tests/test_batch_export.py @@ -4,7 +4,7 @@ import zipfile from pathlib import Path import pytest from PIL import Image -from PySide6.QtCore import QSettings +from PySide6.QtCore import QSettings, Qt 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 @@ -61,6 +61,26 @@ def test_publication_cancellation_race_and_rollback(tmp_path, monkeypatch): assert all(path.read_bytes() == b'old' for path in destinations) +def test_combined_export_confirms_final_filename_after_extension_change(qt_application, tmp_path, monkeypatch): + from mdhelper import batch_export_dialog as ui + docs = documents(tmp_path) + settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat) + monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings) + window = main_window.MainWindow(tmp_path) + panel = BatchExportDialog(window) + output = tmp_path / 'chosen.docx'; output.write_bytes(b'existing export') + questions = [] + monkeypatch.setattr(window.exports, '_choose_page_settings', lambda: PageSettings()) + monkeypatch.setattr(ui.QFileDialog, 'getSaveFileName', lambda *args, **kwargs: (str(tmp_path / 'chosen.txt'), '')) + monkeypatch.setattr(ui.QMessageBox, 'question', lambda *args: questions.append(args[2]) or ui.QMessageBox.StandardButton.No) + try: + panel.files.item(0).setCheckState(Qt.CheckState.Checked) + panel.combined.setChecked(True) + panel.start() + assert str(output) in questions[0] and not panel.running and output.read_bytes() == b'existing export' + finally: panel.close(); window.close() + + @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) diff --git a/tests/test_save_conflict.py b/tests/test_save_conflict.py index 8c04b72..a2bb10e 100644 --- a/tests/test_save_conflict.py +++ b/tests/test_save_conflict.py @@ -21,7 +21,7 @@ def test_comparison_dialog_uses_selected_disk_chunk(qt_application, tmp_path): assert save_conflict.resolve_save_conflict(None, tmp_path / 'note.md', 'a\nlocal\n', 'a\nremote\n', 'a\nbase\n') == 'a\nremote\n' -@pytest.mark.parametrize('decision', ['cancel', 'merge', 'race', 'deleted']) +@pytest.mark.parametrize('decision', ['cancel', 'merge', 'race', 'deleted', 'same_metadata']) def test_editor_conflict_protects_both_versions(qt_application, tmp_path, monkeypatch, decision): settings = QSettings(str(tmp_path / 'settings.ini'), QSettings.Format.IniFormat) monkeypatch.setattr(main_window, 'QSettings', lambda *_: settings) @@ -33,6 +33,11 @@ def test_editor_conflict_protects_both_versions(qt_application, tmp_path, monkey window.open_document(path); window.edit_current_markdown() editor = window.tabs.currentWidget(); editor.replace_source_undoable('local') if decision == 'deleted': path.unlink() + elif decision == 'same_metadata': + import os + stamp = path.stat() + path.write_text('disk') + os.utime(path, ns=(stamp.st_atime_ns, stamp.st_mtime_ns)) else: path.write_text('external modification') def choose(*args, **kwargs): if decision == 'cancel': return None @@ -40,7 +45,7 @@ def test_editor_conflict_protects_both_versions(qt_application, tmp_path, monkey return 'merged result' monkeypatch.setattr(save_conflict, 'resolve_save_conflict', choose) saved = window._save_editor_tab(editor, path, editor.source_text(), editor.session.signature) - assert saved == (decision in {'merge', 'deleted'}) + assert saved == (decision in {'merge', 'deleted', 'same_metadata'}) if saved: assert path.read_text() == editor.source_text() == editor.session.saved_source == 'merged result' assert not editor.has_unsaved_changes()