diff --git a/README.md b/README.md index 43805cb..55c00c6 100644 --- a/README.md +++ b/README.md @@ -111,11 +111,11 @@ The format ring for a given file type. Every target below is wired in `src-tauri | Audio | `M4A` `WAV` `MP3` | FFmpeg | MP3 falls back to `lame` if FFmpeg lacks `libmp3lame` | | PDF | `TXT` `MD` | `lopdf` | Text-layer extraction; errors clearly when a PDF is scanned | | PDF | `DOCX` | Page rendering + native DOCX packaging | PDFKit on macOS, Poppler on Windows/Linux; preserves images, diagrams and page layout without Microsoft Word | -| PDF | `RTF` `ODT` | `lopdf` + Pandoc | Text layer becomes the document body; needs Pandoc | +| PDF | `RTF` | `lopdf` + Pandoc | Text layer becomes the document body; needs Pandoc | | PDF | `HEIC` | PDFKit / Poppler + `sips` | Pages render as PNG then transcode; needs a system HEIC codec | | PDF | `PNG` `JPG` `TIFF` | PDFKit / Poppler | One file per page into a `… pages` folder | | Document `docx` `rtf` `odt` `txt` `md` `html` | `PDF` | AppKit renderer (macOS) · Pandoc elsewhere | macOS path needs no LaTeX or wkhtmltopdf | -| Document `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` `ODT` | Pandoc | The source format is omitted; Markdown writes as GFM, HTML and RTF as standalone documents | +| Document `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` | Pandoc | The source format is omitted; Markdown writes as GFM. HTML is a self-contained file with embedded images | | Presentation `.pptx` | `MD` `PDF` | Pure Rust + Pandoc | Per-slide headings, bullet text and **tables as real Markdown tables**; PDF is a text summary, not a layout render | | Spreadsheet | `MD` `PDF` | `calamine` / Rust | One Markdown table per sheet; `csv`/`tsv` parsed directly | @@ -265,7 +265,7 @@ Core conversion, radial menu, capture, and OCR work with nothing installed. Thes | Tool | Needed for | macOS | Windows | Linux | | --- | --- | --- | --- | --- | | FFmpeg | media convert/compress, 720p, mute, frame, mono; recording off-macOS | optional | **required** for recording | **required** | -| Pandoc | document ↔ DOCX/RTF/ODT/HTML/TXT/MD | optional | needed | needed | +| Pandoc | document ↔ DOCX/RTF/HTML/TXT/MD (ODT input only) | optional | needed | needed | | Typst | Pandoc document-to-PDF engine | not needed (AppKit) | needed | needed | | Poppler `pdftoppm` | PDF → images, PDF → Word, scanned-PDF OCR | not needed (PDFKit) | needed | needed | | ImageMagick `magick` | HEIC/HEIF/AVIF input plus HEIC output | not needed (`sips`) | needed | needed | diff --git a/README.zh-CN.md b/README.zh-CN.md index 90efa2f..208164d 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -111,11 +111,11 @@ Shift 常驻菜单栏(macOS)与通知区域(Windows),同时也有普 | 音频 | `M4A` `WAV` `MP3` | FFmpeg | 若 FFmpeg 缺少 `libmp3lame`,MP3 回退到 `lame` | | PDF | `TXT` `MD` | `lopdf` | 抽取文字层;扫描件会明确报错并引导使用文字识别 | | PDF | `DOCX` | 页面渲染 + 原生 DOCX 封装 | macOS 用 PDFKit,Windows/Linux 用 Poppler;保留图片、图表和页面版式,不依赖 Microsoft Word | -| PDF | `RTF` `ODT` | `lopdf` + Pandoc | 以文字层作为正文;需要 Pandoc | +| PDF | `RTF` | `lopdf` + Pandoc | 以文字层作为正文;需要 Pandoc | | PDF | `HEIC` | PDFKit / Poppler + `sips` | 先渲染为 PNG 再转码;需要系统 HEIC 编码器 | | PDF | `PNG` `JPG` `TIFF` | PDFKit / Poppler | 每页一个文件,输出到 `… pages` 文件夹 | | 文档 `docx` `rtf` `odt` `txt` `md` `html` | `PDF` | AppKit 渲染器(macOS)· 其他系统 Pandoc | macOS 路径不需要 LaTeX 或 wkhtmltopdf | -| 文档 `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` `ODT` | Pandoc | 自动省略源格式;Markdown 以 GFM 写出,HTML 与 RTF 写出完整独立文档 | +| 文档 `docx` `rtf` `odt` `txt` `md` `html` | `DOCX` `RTF` `HTML` `TXT` `MD` | Pandoc | 自动省略源格式;Markdown 以 GFM 写出。HTML 为图片内嵌的独立单文件 | | 演示文稿 `.pptx` | `MD` `PDF` | 纯 Rust + Pandoc | 按页生成标题、要点文字,**表格会输出为真正的 Markdown 表格**;PDF 仍是文字摘要版,非版面还原 | | 电子表格 | `MD` `PDF` | `calamine` / 纯 Rust | 每个工作表一张 Markdown 表;`csv`/`tsv` 直接解析 | @@ -267,7 +267,7 @@ Shift 会持续维护局域网内其他 Shift 实例的列表,并据此提供 | 工具 | 用于 | macOS | Windows | Linux | | --- | --- | --- | --- | --- | | FFmpeg | 音视频转换/压缩、720p、静音、截帧、单声道;非 macOS 的录屏 | 可选 | 录屏**必需** | **必需** | -| Pandoc | 文档 ↔ DOCX/RTF/ODT/HTML/TXT/MD | 可选 | 需要 | 需要 | +| Pandoc | 文档 ↔ DOCX/RTF/HTML/TXT/MD(仅支持 ODT 输入) | 可选 | 需要 | 需要 | | Typst | Pandoc 文档转 PDF 引擎 | 不需要(AppKit) | 需要 | 需要 | | Poppler `pdftoppm` | PDF 转图片、PDF 转 Word、扫描件 OCR | 不需要(PDFKit) | 需要 | 需要 | | ImageMagick `magick` | HEIC/HEIF/AVIF 输入与 HEIC 输出 | 不需要(`sips`) | 需要 | 需要 | diff --git a/src-tauri/src/files.rs b/src-tauri/src/files.rs index 46fdf4a..808f7be 100644 --- a/src-tauri/src/files.rs +++ b/src-tauri/src/files.rs @@ -997,7 +997,7 @@ fn native_document_pdf( /// Converts through the built-in macOS text converter. /// /// Pandoc is an optional tool, and machines without it used to fail every -/// Word/RTF/ODT request. `textutil` ships with macOS and writes the same +/// Word/RTF request. `textutil` ships with macOS and writes the same /// formats, so Markdown is rendered to HTML first and handed over. #[cfg(target_os = "macos")] fn textutil_convert( @@ -1008,7 +1008,6 @@ fn textutil_convert( let writer = match target.to_ascii_lowercase().as_str() { "docx" => "docx", "rtf" => "rtf", - "odt" => "odt", "html" | "htm" => "html", "txt" => "txt", _ => return Ok(None), @@ -1130,7 +1129,6 @@ fn convert_document( "docx" => ("docx", None), "rtf" => ("rtf", None), "pdf" => ("pdf", None), - "odt" => ("odt", None), other => return Err(format!("Unsupported document format: {other}")), }; let pandoc = match resolve_tool("pandoc") { @@ -1155,6 +1153,11 @@ fn convert_document( if matches!(target.as_str(), "html" | "rtf") { command.arg("--standalone"); } + if target == "html" { + // DOCX images otherwise become references to a media folder that + // Pandoc never writes. Inline them so the single HTML file is portable. + command.arg("--self-contained"); + } if let Some(engine) = pdf_engine { command.arg("--pdf-engine").arg(engine); } @@ -1620,7 +1623,7 @@ pub fn perform_action( .unwrap_or_else(|| directory_of(input)); convert_document(input, "docx", Some(&target_dir), on_progress)? } - "convert" if is_pdf && matches!(target, Some("rtf" | "odt")) => { + "convert" if is_pdf && matches!(target, Some("rtf")) => { // The text layer becomes a Markdown intermediate, then the same // document writer produces the requested container. let text = pdf::text_of(input)?; @@ -1638,10 +1641,12 @@ pub fn perform_action( let _ = fs::remove_file(&temporary); result? } - "convert" if is_pdf => return Err( - "Pick a supported target for this PDF: an image format, TXT, MD, DOCX, RTF or ODT." - .into(), - ), + "convert" if is_pdf => { + return Err( + "Pick a supported target for this PDF: an image format, TXT, MD, DOCX or RTF." + .into(), + ) + } "convert" if is_image && target @@ -2284,7 +2289,7 @@ mod tests { #[cfg(target_os = "macos")] #[test] fn textutil_fallback_converts_markdown_to_word_formats() { - for target in ["docx", "rtf", "odt"] { + for target in ["docx", "rtf"] { let dir = scratch_dir(); let input = dir.join("note.md"); fs::write(&input, "# Shift\n\nLocal **documents**.\n").unwrap(); @@ -2451,37 +2456,50 @@ mod tests { } #[test] - fn converts_pdf_text_layer_to_rtf_and_odt_when_pandoc_is_available() { + fn converts_pdf_text_layer_to_rtf_when_pandoc_is_available() { if which::which("pandoc").is_err() { return; } - for target in ["rtf", "odt"] { - let dir = scratch_dir(); - let input = dir.join("report.pdf"); - write_text_pdf(&input, "Quarterly numbers"); + let dir = scratch_dir(); + let input = dir.join("report.pdf"); + write_text_pdf(&input, "Quarterly numbers"); - let outcome = perform_action( - "convert", - &[input.to_string_lossy().to_string()], - Some(target), - None, - None, - ) - .unwrap_or_else(|error| panic!("PDF to {target} failed: {error}")); - assert_eq!(outcome.outputs.len(), 1); - assert!( - outcome.outputs[0].ends_with(&format!("report.{target}")), - "output must follow the requested format: {:?}", - outcome.outputs[0] - ); - assert!(Path::new(&outcome.outputs[0]).exists()); - if target == "rtf" { - assert!(fs::read_to_string(&outcome.outputs[0]) - .unwrap() - .starts_with("{\\rtf1")); - } - fs::remove_dir_all(dir).unwrap(); - } + let outcome = perform_action( + "convert", + &[input.to_string_lossy().to_string()], + Some("rtf"), + None, + None, + ) + .expect("PDF to RTF failed"); + assert_eq!(outcome.outputs.len(), 1); + assert!( + outcome.outputs[0].ends_with("report.rtf"), + "output must follow the requested format: {:?}", + outcome.outputs[0] + ); + assert!(fs::read_to_string(&outcome.outputs[0]) + .unwrap() + .starts_with("{\\rtf1")); + fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn pdf_to_odt_is_rejected() { + let dir = scratch_dir(); + let input = dir.join("report.pdf"); + write_text_pdf(&input, "Quarterly numbers"); + + let error = perform_action( + "convert", + &[input.to_string_lossy().to_string()], + Some("odt"), + None, + None, + ) + .unwrap_err(); + assert!(error.contains("DOCX or RTF"), "{error}"); + fs::remove_dir_all(dir).unwrap(); } #[test] @@ -2573,6 +2591,33 @@ mod tests { fs::remove_dir_all(dir).unwrap(); } + #[test] + fn image_bearing_docx_to_html_embeds_images() { + if which::which("pandoc").is_err() { + return; + } + let dir = scratch_dir(); + let image = dir.join("sample.png"); + sample_image(&image); + let source = dir.join("sample.md"); + fs::write(&source, "![Sample](sample.png)\n").unwrap(); + let docx = dir.join("sample.docx"); + let status = Command::new("pandoc") + .current_dir(&dir) + .arg("sample.md") + .args(["-o", "sample.docx"]) + .status() + .unwrap(); + assert!(status.success()); + + let output = convert_document(&docx, "html", None, None).unwrap(); + let html = fs::read_to_string(output).unwrap(); + assert!(html.contains(" = Vec::new(); diff --git a/src-tauri/src/runtime.rs b/src-tauri/src/runtime.rs index 6cb0940..5728ff6 100644 --- a/src-tauri/src/runtime.rs +++ b/src-tauri/src/runtime.rs @@ -232,7 +232,7 @@ fn pdf_to_word_component() -> RuntimeComponent { "pdf-to-word", "Built-in PDF text -> Word", "documents", - "PDF text layer -> DOCX / RTF / ODT", + "PDF text layer -> DOCX / RTF", "bundled", true, None, @@ -486,7 +486,7 @@ fn machine_info_sync() -> RuntimeInfo { "pandoc", "Pandoc", "documents", - "DOCX / RTF / ODT / MD / HTML conversion", + "DOCX / RTF / TXT / MD / HTML conversion", "pandoc", ), component( @@ -519,7 +519,7 @@ fn machine_info_sync() -> RuntimeInfo { "textutil", "macOS textutil", "documents", - "DOCX / RTF / ODT / HTML fallback", + "DOCX / RTF / HTML fallback", "system", Path::new("/usr/bin/textutil").is_file(), None, diff --git a/src/lib/conversionFormats.test.ts b/src/lib/conversionFormats.test.ts index aac541c..67ab1a5 100644 --- a/src/lib/conversionFormats.test.ts +++ b/src/lib/conversionFormats.test.ts @@ -5,13 +5,13 @@ import { conversionFormats } from "./conversionFormats.ts"; test("document conversion never offers the source format again", () => { const cases = { - docx: ["PDF", "MD", "TXT", "HTML", "RTF", "ODT"], - rtf: ["PDF", "DOCX", "MD", "TXT", "HTML", "ODT"], + docx: ["PDF", "MD", "TXT", "HTML", "RTF"], + rtf: ["PDF", "DOCX", "MD", "TXT", "HTML"], odt: ["PDF", "DOCX", "MD", "TXT", "HTML", "RTF"], - txt: ["PDF", "DOCX", "MD", "HTML", "RTF", "ODT"], - md: ["PDF", "DOCX", "TXT", "HTML", "RTF", "ODT"], - html: ["PDF", "DOCX", "MD", "TXT", "RTF", "ODT"], - htm: ["PDF", "DOCX", "MD", "TXT", "RTF", "ODT"], + txt: ["PDF", "DOCX", "MD", "HTML", "RTF"], + md: ["PDF", "DOCX", "TXT", "HTML", "RTF"], + html: ["PDF", "DOCX", "MD", "TXT", "RTF"], + htm: ["PDF", "DOCX", "MD", "TXT", "RTF"], }; for (const [extension, expected] of Object.entries(cases)) { @@ -19,6 +19,19 @@ test("document conversion never offers the source format again", () => { } }); +test("ODT is never offered as an output format", () => { + for (const [kind, extension] of [ + ["document", "docx"], + ["document", "rtf"], + ["document", "txt"], + ["document", "md"], + ["document", "html"], + ["pdf", "pdf"], + ] as const) { + assert.ok(!conversionFormats(kind, extension).includes("ODT"), extension); + } +}); + test("aliases of the source format are not offered", () => { assert.ok(!conversionFormats("image", "jpeg").includes("JPG")); assert.ok(!conversionFormats("image", "tif").includes("TIFF")); @@ -55,7 +68,6 @@ test("PDF conversion includes every implemented document target", () => { "MD", "DOCX", "RTF", - "ODT", ]); }); diff --git a/src/lib/conversionFormats.ts b/src/lib/conversionFormats.ts index 375b622..f7c4e31 100644 --- a/src/lib/conversionFormats.ts +++ b/src/lib/conversionFormats.ts @@ -9,8 +9,8 @@ const SPREADSHEET_SOURCES = new Set(["xlsx", "xls", "xlsb", "ods", "csv", "tsv"] const IMAGE_FORMATS = ["PNG", "JPG", "WEBP", "HEIC", "TIFF", "BMP", "GIF", "PDF"]; const VIDEO_FORMATS = ["MP4", "MOV", "GIF", "M4A"]; const AUDIO_FORMATS = ["M4A", "WAV", "MP3"]; -const PDF_FORMATS = ["PNG", "JPG", "TIFF", "HEIC", "TXT", "MD", "DOCX", "RTF", "ODT"]; -const DOCUMENT_FORMATS = ["PDF", "DOCX", "MD", "TXT", "HTML", "RTF", "ODT"]; +const PDF_FORMATS = ["PNG", "JPG", "TIFF", "HEIC", "TXT", "MD", "DOCX", "RTF"]; +const DOCUMENT_FORMATS = ["PDF", "DOCX", "MD", "TXT", "HTML", "RTF"]; const FORMAT_ALIASES: Record = { jpeg: "jpg",