Commit 3f4fa24f74

3f4fa24f747576cf3a39ed29b65628848347c546

parent: 32027d8982

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-20 16:35 UTC

Export and publish ODT natively in every build

Layout: unified · split

Sources/OrgApp/AppStoreCommands.swift +1 −1
@@ -4,7 +4,7 @@ import Foundation
4/// Emacs, and attaching by link, which the sandbox doesn't allow. 4/// Emacs, and attaching by link, which the sandbox doesn't allow.
5public enum AppStoreCommands { 5public enum AppStoreCommands {
6 public static let hidden: Set<String> = [ 6 public static let hidden: Set<String> = [
7 "app.export.pdf", "app.export.odt", "app.export.text", 7 "app.export.pdf", "app.export.text",
8 "app.attach.link", "app.attach.symlink", 8 "app.attach.link", "app.attach.symlink",
9 ] 9 ]
10 10
Sources/OrgApp/CommandTitles.swift +1 −1
@@ -90,7 +90,7 @@ enum CommandTitles {
90 String(localized: "Export to HTML and Open", comment: "Command title"), 90 String(localized: "Export to HTML and Open", comment: "Command title"),
91 String(localized: "Export to LaTeX", comment: "Command title"), 91 String(localized: "Export to LaTeX", comment: "Command title"),
92 String(localized: "Export to Markdown", comment: "Command title"), 92 String(localized: "Export to Markdown", comment: "Command title"),
93 String(localized: "Export to ODT with Emacs", comment: "Command title"), 93 String(localized: "Export to ODT", comment: "Command title"),
94 String(localized: "Export to PDF", comment: "Command title"), 94 String(localized: "Export to PDF", comment: "Command title"),
95 String(localized: "Export to PDF with Emacs", comment: "Command title"), 95 String(localized: "Export to PDF with Emacs", comment: "Command title"),
96 String(localized: "Export to Plain Text", comment: "Command title"), 96 String(localized: "Export to Plain Text", comment: "Command title"),
Sources/OrgApp/Publish.swift +11 −5
@@ -14,8 +14,8 @@ import OrgCore
14/// 14///
15/// Other keys: `base-extension` (an Emacs regexp, or `any`), `exclude` (a regexp on the path 15/// Other keys: `base-extension` (an Emacs regexp, or `any`), `exclude` (a regexp on the path
16/// under the base directory), `include` (file names separated by spaces), `publishing-function` 16/// under the base directory), `include` (file names separated by spaces), `publishing-function`
17/// (`org-html-publish-to-html`, `org-md-publish-to-md`, `org-publish-attachment`, separated by 17/// (`org-html-publish-to-html`, `org-md-publish-to-md`, `org-odt-publish-to-odt`,
18/// spaces), `sitemap-filename`, `sitemap-title`, `sitemap-style` (`tree` or `list`), 18/// `org-publish-attachment`, separated by spaces), `sitemap-filename`, `sitemap-title`, `sitemap-style` (`tree` or `list`),
19/// `sitemap-sort-files` (`alphabetically`, `chronologically`, `anti-chronologically`, `nil`), 19/// `sitemap-sort-files` (`alphabetically`, `chronologically`, `anti-chronologically`, `nil`),
20/// `sitemap-sort-folders` (`ignore`, `first`, `last`, `nil`), `sitemap-ignore-case`, 20/// `sitemap-sort-folders` (`ignore`, `first`, `last`, `nil`), `sitemap-ignore-case`,
21/// `body-only`, and `components` (project names separated by spaces) for a project of projects. 21/// `body-only`, and `components` (project names separated by spaces) for a project of projects.
@@ -40,7 +40,7 @@ public struct PublishProject: Sendable, Equatable {
40 40
41 public init(name: String) { self.name = name } 41 public init(name: String) { self.name = name }
42 42
43 public static let functions = ["org-html-publish-to-html", "org-md-publish-to-md", "org-latex-publish-to-latex", "org-publish-attachment"] 43 public static let functions = ["org-html-publish-to-html", "org-md-publish-to-md", "org-latex-publish-to-latex", "org-odt-publish-to-odt", "org-publish-attachment"]
44} 44}
45 45
46public enum Publish { 46public enum Publish {
@@ -290,13 +290,14 @@ public enum Publish {
290 if directory((file as NSString).deletingLastPathComponent) == outputDirectory { return nil } 290 if directory((file as NSString).deletingLastPathComponent) == outputDirectory { return nil }
291 return outputDirectory + (file as NSString).lastPathComponent 291 return outputDirectory + (file as NSString).lastPathComponent
292 } 292 }
293 let ext = ["org-md-publish-to-md": "md", "org-latex-publish-to-latex": "tex"][function] ?? "html" 293 let ext = ["org-md-publish-to-md": "md", "org-latex-publish-to-latex": "tex", "org-odt-publish-to-odt": "odt"][function] ?? "html"
294 guard let text = read(file) else { throw Failure(description: "Can't read \(file)") } 294 guard let text = read(file) else { throw Failure(description: "Can't read \(file)") }
295 return ExportScope.output(for: URL(fileURLWithPath: outputDirectory + (file as NSString).lastPathComponent), 295 return ExportScope.output(for: URL(fileURLWithPath: outputDirectory + (file as NSString).lastPathComponent),
296 name: ExportScope.fileName(text), extension: ext).path 296 name: ExportScope.fileName(text), extension: ext).path
297 } 297 }
298 298
299 /// Copies an attachment to `output`, or writes what `function` exports from `file`. 299 /// Copies an attachment to `output`, or writes what `function` exports from `file`; ODT is
300 /// exported here, with the preferences' settings.
300 private static func write( 301 private static func write(
301 _ file: String, to output: String, function: String, bodyOnly: Bool, 302 _ file: String, to output: String, function: String, bodyOnly: Bool,
302 read: (String) -> String?, export: (_ function: String, _ text: String, _ path: String, _ bodyOnly: Bool) throws -> String 303 read: (String) -> String?, export: (_ function: String, _ text: String, _ path: String, _ bodyOnly: Bool) throws -> String
@@ -305,6 +306,11 @@ public enum Publish {
305 if function == "org-publish-attachment" { 306 if function == "org-publish-attachment" {
306 if fm.fileExists(atPath: output) { try fm.removeItem(atPath: output) } 307 if fm.fileExists(atPath: output) { try fm.removeItem(atPath: output) }
307 try fm.copyItem(atPath: file, toPath: output) 308 try fm.copyItem(atPath: file, toPath: output)
309 } else if function == "org-odt-publish-to-odt" {
310 let text = read(file) ?? ""
311 let settings = OrgPreferences.orgSettings().resolvingSetupFiles(in: text, path: file, read: OrgSettings.readFile)
312 try ODTExport.document(text, settings: settings, path: file, author: NSFullUserName(), language: OrgPreferences.exportLanguage())
313 .write(to: URL(fileURLWithPath: output), options: .atomic)
308 } else { 314 } else {
309 try export(function, read(file) ?? "", file, bodyOnly).write(toFile: output, atomically: true, encoding: .utf8) 315 try export(function, read(file) ?? "", file, bodyOnly).write(toFile: output, atomically: true, encoding: .utf8)
310 } 316 }
Sources/OrgEditorAppKit/EditorCommands.swift +1 −1
@@ -174,7 +174,7 @@ public enum EditorCommands {
174 EditorCommand(id: "app.export.plain-text", title: "Export to Plain Text", action: .app), 174 EditorCommand(id: "app.export.plain-text", title: "Export to Plain Text", action: .app),
175 EditorCommand(id: "app.copy-rich-text", title: "Copy as Rich Text", action: .app), 175 EditorCommand(id: "app.copy-rich-text", title: "Copy as Rich Text", action: .app),
176 EditorCommand(id: "app.export.pdf", title: "Export to PDF with Emacs", action: .app), 176 EditorCommand(id: "app.export.pdf", title: "Export to PDF with Emacs", action: .app),
177 EditorCommand(id: "app.export.odt", title: "Export to ODT with Emacs", action: .app), 177 EditorCommand(id: "app.export.odt", title: "Export to ODT", action: .app),
178 EditorCommand(id: "app.export.latex", title: "Export to LaTeX", action: .app), 178 EditorCommand(id: "app.export.latex", title: "Export to LaTeX", action: .app),
179 EditorCommand(id: "app.export.text", title: "Export to Plain Text with Emacs", action: .app), 179 EditorCommand(id: "app.export.text", title: "Export to Plain Text with Emacs", action: .app),
180 EditorCommand(id: "app.export.icalendar", title: "Export to iCalendar", action: .app), 180 EditorCommand(id: "app.export.icalendar", title: "Export to iCalendar", action: .app),
Sources/Orgstar/ContentView.swift +15 −1
@@ -630,6 +630,20 @@ struct ContentView: View {
630 } 630 }
631 case "latex" where exportLaTeX(text, whole: whole, url: url, to: destination, open: open, subtree: subtree ? caret : nil, bodyOnly: bodyOnly, settings: defaults): 631 case "latex" where exportLaTeX(text, whole: whole, url: url, to: destination, open: open, subtree: subtree ? caret : nil, bodyOnly: bodyOnly, settings: defaults):
632 break 632 break
633 case "odt":
634 let name = ExportScope.fileName(whole, subtreeAt: subtree ? caret : nil, settings: defaults)
635 guard let target = destination ?? writable(ExportScope.output(for: url, name: name, extension: "odt")) else { return }
636 // A missing image stops the export, so offer access to them first.
637 _ = reach(ODTExport.imageFiles(text, settings: defaults, path: path).filter(WorkspaceModel.isThere))
638 do {
639 let data = try ODTExport.document(text, settings: defaults, path: path, author: NSFullUserName(), language: OrgPreferences.exportLanguage())
640 try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)
641 try data.write(to: target, options: .atomic)
642 commands.show("Exported to \(target.lastPathComponent)")
643 if open { NSWorkspace.shared.open(target) }
644 } catch {
645 commands.show("Can't export \(target.lastPathComponent): \(error.localizedDescription)")
646 }
633 case "print-pdf", "docx", "rtf", "plain-text": 647 case "print-pdf", "docx", "rtf", "plain-text":
634 let ext = ["print-pdf": "pdf", "plain-text": "txt"][format] ?? format 648 let ext = ["print-pdf": "pdf", "plain-text": "txt"][format] ?? format
635 let name = ExportScope.fileName(whole, subtreeAt: subtree ? caret : nil, settings: defaults) 649 let name = ExportScope.fileName(whole, subtreeAt: subtree ? caret : nil, settings: defaults)
@@ -661,7 +675,7 @@ struct ContentView: View {
661 } 675 }
662 default: 676 default:
663 #if !APPSTORE 677 #if !APPSTORE
664 let functions = ["pdf": "org-latex-export-to-pdf", "odt": "org-odt-export-to-odt", "latex": "org-latex-export-to-latex", "text": "org-ascii-export-to-ascii"] 678 let functions = ["pdf": "org-latex-export-to-pdf", "latex": "org-latex-export-to-latex", "text": "org-ascii-export-to-ascii"]
665 guard let function = functions[format] else { return } 679 guard let function = functions[format] else { return }
666 commands.show("Exporting with Emacs…") 680 commands.show("Exporting with Emacs…")
667 commands.runInEmacs { 681 commands.runInEmacs {
Sources/Orgstar/ExportSheet.swift +3 −3
@@ -24,14 +24,14 @@ struct ExportSheet: View {
24 #if APPSTORE 24 #if APPSTORE
25 static let formats: [(id: String, title: String, ext: String)] = [ 25 static let formats: [(id: String, title: String, ext: String)] = [
26 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"), 26 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
27 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("icalendar", "iCalendar", "ics"), 27 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("odt", "ODT", "odt"),
28 ("latex", "LaTeX", "tex"), 28 ("icalendar", "iCalendar", "ics"), ("latex", "LaTeX", "tex"),
29 ] 29 ]
30 #else 30 #else
31 static let formats: [(id: String, title: String, ext: String)] = [ 31 static let formats: [(id: String, title: String, ext: String)] = [
32 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"), 32 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
33 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("pdf", "PDF via LaTeX (Emacs)", "pdf"), 33 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("pdf", "PDF via LaTeX (Emacs)", "pdf"),
34 ("odt", "ODT (Emacs)", "odt"), ("latex", "LaTeX", "tex"), ("text", "Plain text, ASCII (Emacs)", "txt"), 34 ("odt", "ODT", "odt"), ("latex", "LaTeX", "tex"), ("text", "Plain text, ASCII (Emacs)", "txt"),
35 ("icalendar", "iCalendar", "ics"), 35 ("icalendar", "iCalendar", "ics"),
36 ] 36 ]
37 #endif 37 #endif
Sources/OrgstarMobile/ExportMenu.swift +11 −4
@@ -6,11 +6,11 @@ import OrgPresentation
6import SwiftUI 6import SwiftUI
7import UniformTypeIdentifiers 7import UniformTypeIdentifiers
8 8
9/// A file exported as HTML, Markdown, PDF, Word, RTF, plain text, iCalendar or LaTeX, written when the 9/// A file exported as HTML, Markdown, PDF, Word, RTF, plain text, ODT, iCalendar or LaTeX, written
10/// share sheet takes it. 10/// when the share sheet takes it.
11struct OrgExport: Transferable { 11struct OrgExport: Transferable {
12 enum Format: CaseIterable { 12 enum Format: CaseIterable {
13 case html, markdown, pdf, docx, rtf, plainText, icalendar 13 case html, markdown, pdf, docx, rtf, plainText, odt, icalendar
14 case latex 14 case latex
15 15
16 /// Whether it needs Pro now: every format but HTML and Markdown, until Pro is unlocked. 16 /// Whether it needs Pro now: every format but HTML and Markdown, until Pro is unlocked.
@@ -24,6 +24,7 @@ struct OrgExport: Transferable {
24 case .docx: "Word" 24 case .docx: "Word"
25 case .rtf: "Rich Text (RTF)" 25 case .rtf: "Rich Text (RTF)"
26 case .plainText: "Plain Text" 26 case .plainText: "Plain Text"
27 case .odt: "ODT"
27 case .icalendar: "iCalendar" 28 case .icalendar: "iCalendar"
28 case .latex: "LaTeX" 29 case .latex: "LaTeX"
29 } 30 }
@@ -37,6 +38,7 @@ struct OrgExport: Transferable {
37 case .docx: "docx" 38 case .docx: "docx"
38 case .rtf: "rtf" 39 case .rtf: "rtf"
39 case .plainText: "txt" 40 case .plainText: "txt"
41 case .odt: "odt"
40 case .icalendar: "ics" 42 case .icalendar: "ics"
41 case .latex: "tex" 43 case .latex: "tex"
42 } 44 }
@@ -77,6 +79,8 @@ struct OrgExport: Transferable {
77 .exportingCondition { $0.format == .rtf } 79 .exportingCondition { $0.format == .rtf }
78 FileRepresentation(exportedContentType: .plainText) { try await $0.write() } 80 FileRepresentation(exportedContentType: .plainText) { try await $0.write() }
79 .exportingCondition { $0.format == .plainText } 81 .exportingCondition { $0.format == .plainText }
82 FileRepresentation(exportedContentType: UTType(filenameExtension: "odt") ?? .data) { try await $0.write() }
83 .exportingCondition { $0.format == .odt }
80 FileRepresentation(exportedContentType: .calendarEvent) { try await $0.write() } 84 FileRepresentation(exportedContentType: .calendarEvent) { try await $0.write() }
81 .exportingCondition { $0.format == .icalendar } 85 .exportingCondition { $0.format == .icalendar }
82 FileRepresentation(exportedContentType: UTType(filenameExtension: "tex") ?? .plainText) { try await $0.write() } 86 FileRepresentation(exportedContentType: UTType(filenameExtension: "tex") ?? .plainText) { try await $0.write() }
@@ -106,6 +110,9 @@ struct OrgExport: Transferable {
106 throw error 110 throw error
107 } 111 }
108 try content.write(to: url, atomically: true, encoding: .utf8) 112 try content.write(to: url, atomically: true, encoding: .utf8)
113 case .odt:
114 try ODTExport.document(text, settings: settings, path: path, author: NSFullUserName(), language: OrgPreferences.exportLanguage())
115 .write(to: url, options: .atomic)
109 case .icalendar: 116 case .icalendar:
110 try ICalendarFiles.export(self.text, path: path, settings: settings, options: OrgPreferences.icalendarOptions(), 117 try ICalendarFiles.export(self.text, path: path, settings: settings, options: OrgPreferences.icalendarOptions(),
111 subtree: subtree, destination: url) 118 subtree: subtree, destination: url)
@@ -126,7 +133,7 @@ struct OrgExport: Transferable {
126 } 133 }
127} 134}
128 135
129/// Sharing the file as HTML, Markdown, PDF, Word, RTF, plain text, iCalendar or LaTeX: to another app, or saved with 136/// Sharing the file as HTML, Markdown, PDF, Word, RTF, plain text, ODT, iCalendar or LaTeX: to another app, or saved with
130/// Save to Files; or copying it as rich text. 137/// Save to Files; or copying it as rich text.
131struct ExportMenu: View { 138struct ExportMenu: View {
132 let path: String 139 let path: String
Tests/OrgAppTests/PublishTests.swift +16
@@ -195,6 +195,22 @@ struct PublishTests {
195 } 195 }
196 } 196 }
197 197
198 /// `org-odt-publish-to-odt` writes `.odt` files, which the export closure doesn't see.
199 @Test func publishesODT() throws {
200 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-publish-\(UUID().uuidString)")
201 defer { try? FileManager.default.removeItem(at: folder) }
202 try Self.tree(folder)
203 let config = "[[publish]]\nname = \"odt\"\nbase-directory = \"\(folder.path)/src\"\npublishing-directory = \"\(folder.path)/odt\"\npublishing-function = \"org-odt-publish-to-odt\"\n"
204 let loaded = Publish.projects(fromConfig: config, root: nil)
205 #expect(loaded.problems.isEmpty)
206 let read = { (path: String) in try? String(contentsOfFile: path, encoding: .utf8) }
207 let report = try Publish.publish(loaded.projects, all: loaded.projects, read: read) { _, _, _, _ in Issue.record("exported as text"); return "" }
208 #expect(report.published.count == 3)
209 let data = try Data(contentsOf: folder.appendingPathComponent("odt/a.odt"))
210 #expect(data.prefix(4) == Data([0x50, 0x4b, 0x03, 0x04]))
211 #expect(String(decoding: data[30..<38], as: UTF8.self) == "mimetype")
212 }
213
198 @Test func skipsUpToDateFilesAndFindsProjects() throws { 214 @Test func skipsUpToDateFilesAndFindsProjects() throws {
199 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-publish-\(UUID().uuidString)").resolvingSymlinksInPath() 215 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-publish-\(UUID().uuidString)").resolvingSymlinksInPath()
200 defer { try? FileManager.default.removeItem(at: folder) } 216 defer { try? FileManager.default.removeItem(at: folder) }
l10n/Localizable.xcstrings +6 −6
@@ -11714,37 +11714,37 @@
11714 } 11714 }
11715 } 11715 }
11716 }, 11716 },
11717 "Export to ODT with Emacs": { 11717 "Export to ODT": {
11718 "comment": "Command title", 11718 "comment": "Command title",
11719 "localizations": { 11719 "localizations": {
11720 "de": { 11720 "de": {
11721 "stringUnit": { 11721 "stringUnit": {
11722 "state": "translated", 11722 "state": "translated",
11723 "value": "Mit Emacs als ODT exportieren" 11723 "value": "Als ODT exportieren"
11724 } 11724 }
11725 }, 11725 },
11726 "es": { 11726 "es": {
11727 "stringUnit": { 11727 "stringUnit": {
11728 "state": "translated", 11728 "state": "translated",
11729 "value": "Exportar a ODT con Emacs" 11729 "value": "Exportar a ODT"
11730 } 11730 }
11731 }, 11731 },
11732 "fr": { 11732 "fr": {
11733 "stringUnit": { 11733 "stringUnit": {
11734 "state": "translated", 11734 "state": "translated",
11735 "value": "Exporter en ODT avec Emacs" 11735 "value": "Exporter en ODT"
11736 } 11736 }
11737 }, 11737 },
11738 "ja": { 11738 "ja": {
11739 "stringUnit": { 11739 "stringUnit": {
11740 "state": "translated", 11740 "state": "translated",
11741 "value": "EmacsでODTに書き出す" 11741 "value": "ODTに書き出す"
11742 } 11742 }
11743 }, 11743 },
11744 "pt-BR": { 11744 "pt-BR": {
11745 "stringUnit": { 11745 "stringUnit": {
11746 "state": "translated", 11746 "state": "translated",
11747 "value": "Exportar para ODT com o Emacs" 11747 "value": "Exportar para ODT"
11748 } 11748 }
11749 } 11749 }
11750 } 11750 }