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
44/// Emacs, and attaching by link, which the sandbox doesn't allow.
55public enum AppStoreCommands {
66 public static let hidden: Set<String> = [
7 "app.export.pdf", "app.export.odt", "app.export.text",
7 "app.export.pdf", "app.export.text",
88 "app.attach.link", "app.attach.symlink",
99 ]
1010
Sources/OrgApp/CommandTitles.swift +1 −1
@@ -90,7 +90,7 @@ enum CommandTitles {
9090 String(localized: "Export to HTML and Open", comment: "Command title"),
9191 String(localized: "Export to LaTeX", comment: "Command title"),
9292 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"),
9494 String(localized: "Export to PDF", comment: "Command title"),
9595 String(localized: "Export to PDF with Emacs", comment: "Command title"),
9696 String(localized: "Export to Plain Text", comment: "Command title"),
Sources/OrgApp/Publish.swift +11 −5
@@ -14,8 +14,8 @@ import OrgCore
1414///
1515/// Other keys: `base-extension` (an Emacs regexp, or `any`), `exclude` (a regexp on the path
1616/// 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
18/// spaces), `sitemap-filename`, `sitemap-title`, `sitemap-style` (`tree` or `list`),
17/// (`org-html-publish-to-html`, `org-md-publish-to-md`, `org-odt-publish-to-odt`,
18/// `org-publish-attachment`, separated by spaces), `sitemap-filename`, `sitemap-title`, `sitemap-style` (`tree` or `list`),
1919/// `sitemap-sort-files` (`alphabetically`, `chronologically`, `anti-chronologically`, `nil`),
2020/// `sitemap-sort-folders` (`ignore`, `first`, `last`, `nil`), `sitemap-ignore-case`,
2121/// `body-only`, and `components` (project names separated by spaces) for a project of projects.
@@ -40,7 +40,7 @@ public struct PublishProject: Sendable, Equatable {
4040
4141 public init(name: String) { self.name = name }
4242
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"]
4444}
4545
4646public enum Publish {
@@ -290,13 +290,14 @@ public enum Publish {
290290 if directory((file as NSString).deletingLastPathComponent) == outputDirectory { return nil }
291291 return outputDirectory + (file as NSString).lastPathComponent
292292 }
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"
294294 guard let text = read(file) else { throw Failure(description: "Can't read \(file)") }
295295 return ExportScope.output(for: URL(fileURLWithPath: outputDirectory + (file as NSString).lastPathComponent),
296296 name: ExportScope.fileName(text), extension: ext).path
297297 }
298298
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.
300301 private static func write(
301302 _ file: String, to output: String, function: String, bodyOnly: Bool,
302303 read: (String) -> String?, export: (_ function: String, _ text: String, _ path: String, _ bodyOnly: Bool) throws -> String
@@ -305,6 +306,11 @@ public enum Publish {
305306 if function == "org-publish-attachment" {
306307 if fm.fileExists(atPath: output) { try fm.removeItem(atPath: output) }
307308 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)
308314 } else {
309315 try export(function, read(file) ?? "", file, bodyOnly).write(toFile: output, atomically: true, encoding: .utf8)
310316 }
Sources/OrgEditorAppKit/EditorCommands.swift +1 −1
@@ -174,7 +174,7 @@ public enum EditorCommands {
174174 EditorCommand(id: "app.export.plain-text", title: "Export to Plain Text", action: .app),
175175 EditorCommand(id: "app.copy-rich-text", title: "Copy as Rich Text", action: .app),
176176 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),
178178 EditorCommand(id: "app.export.latex", title: "Export to LaTeX", action: .app),
179179 EditorCommand(id: "app.export.text", title: "Export to Plain Text with Emacs", action: .app),
180180 EditorCommand(id: "app.export.icalendar", title: "Export to iCalendar", action: .app),
Sources/Orgstar/ContentView.swift +15 −1
@@ -630,6 +630,20 @@ struct ContentView: View {
630630 }
631631 case "latex" where exportLaTeX(text, whole: whole, url: url, to: destination, open: open, subtree: subtree ? caret : nil, bodyOnly: bodyOnly, settings: defaults):
632632 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 }
633647 case "print-pdf", "docx", "rtf", "plain-text":
634648 let ext = ["print-pdf": "pdf", "plain-text": "txt"][format] ?? format
635649 let name = ExportScope.fileName(whole, subtreeAt: subtree ? caret : nil, settings: defaults)
@@ -661,7 +675,7 @@ struct ContentView: View {
661675 }
662676 default:
663677 #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"]
665679 guard let function = functions[format] else { return }
666680 commands.show("Exporting with Emacs…")
667681 commands.runInEmacs {
Sources/Orgstar/ExportSheet.swift +3 −3
@@ -24,14 +24,14 @@ struct ExportSheet: View {
2424 #if APPSTORE
2525 static let formats: [(id: String, title: String, ext: String)] = [
2626 ("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"),
28 ("latex", "LaTeX", "tex"),
27 ("rtf", "Rich Text (RTF)", "rtf"), ("plain-text", "Plain text", "txt"), ("odt", "ODT", "odt"),
28 ("icalendar", "iCalendar", "ics"), ("latex", "LaTeX", "tex"),
2929 ]
3030 #else
3131 static let formats: [(id: String, title: String, ext: String)] = [
3232 ("html", "HTML", "html"), ("markdown", "Markdown", "md"), ("print-pdf", "PDF", "pdf"), ("docx", "Word", "docx"),
3333 ("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"),
3535 ("icalendar", "iCalendar", "ics"),
3636 ]
3737 #endif
Sources/OrgstarMobile/ExportMenu.swift +11 −4
@@ -6,11 +6,11 @@ import OrgPresentation
66import SwiftUI
77import UniformTypeIdentifiers
88
9/// A file exported as HTML, Markdown, PDF, Word, RTF, plain text, iCalendar or LaTeX, written when the
10/// share sheet takes it.
9/// A file exported as HTML, Markdown, PDF, Word, RTF, plain text, ODT, iCalendar or LaTeX, written
10/// when the share sheet takes it.
1111struct OrgExport: Transferable {
1212 enum Format: CaseIterable {
13 case html, markdown, pdf, docx, rtf, plainText, icalendar
13 case html, markdown, pdf, docx, rtf, plainText, odt, icalendar
1414 case latex
1515
1616 /// Whether it needs Pro now: every format but HTML and Markdown, until Pro is unlocked.
@@ -24,6 +24,7 @@ struct OrgExport: Transferable {
2424 case .docx: "Word"
2525 case .rtf: "Rich Text (RTF)"
2626 case .plainText: "Plain Text"
27 case .odt: "ODT"
2728 case .icalendar: "iCalendar"
2829 case .latex: "LaTeX"
2930 }
@@ -37,6 +38,7 @@ struct OrgExport: Transferable {
3738 case .docx: "docx"
3839 case .rtf: "rtf"
3940 case .plainText: "txt"
41 case .odt: "odt"
4042 case .icalendar: "ics"
4143 case .latex: "tex"
4244 }
@@ -77,6 +79,8 @@ struct OrgExport: Transferable {
7779 .exportingCondition { $0.format == .rtf }
7880 FileRepresentation(exportedContentType: .plainText) { try await $0.write() }
7981 .exportingCondition { $0.format == .plainText }
82 FileRepresentation(exportedContentType: UTType(filenameExtension: "odt") ?? .data) { try await $0.write() }
83 .exportingCondition { $0.format == .odt }
8084 FileRepresentation(exportedContentType: .calendarEvent) { try await $0.write() }
8185 .exportingCondition { $0.format == .icalendar }
8286 FileRepresentation(exportedContentType: UTType(filenameExtension: "tex") ?? .plainText) { try await $0.write() }
@@ -106,6 +110,9 @@ struct OrgExport: Transferable {
106110 throw error
107111 }
108112 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)
109116 case .icalendar:
110117 try ICalendarFiles.export(self.text, path: path, settings: settings, options: OrgPreferences.icalendarOptions(),
111118 subtree: subtree, destination: url)
@@ -126,7 +133,7 @@ struct OrgExport: Transferable {
126133 }
127134}
128135
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
130137/// Save to Files; or copying it as rich text.
131138struct ExportMenu: View {
132139 let path: String
Tests/OrgAppTests/PublishTests.swift +16
@@ -195,6 +195,22 @@ struct PublishTests {
195195 }
196196 }
197197
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
198214 @Test func skipsUpToDateFilesAndFindsProjects() throws {
199215 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-publish-\(UUID().uuidString)").resolvingSymlinksInPath()
200216 defer { try? FileManager.default.removeItem(at: folder) }
l10n/Localizable.xcstrings +6 −6
@@ -11714,37 +11714,37 @@
1171411714 }
1171511715 }
1171611716 },
11717 "Export to ODT with Emacs": {
11717 "Export to ODT": {
1171811718 "comment": "Command title",
1171911719 "localizations": {
1172011720 "de": {
1172111721 "stringUnit": {
1172211722 "state": "translated",
11723 "value": "Mit Emacs als ODT exportieren"
11723 "value": "Als ODT exportieren"
1172411724 }
1172511725 },
1172611726 "es": {
1172711727 "stringUnit": {
1172811728 "state": "translated",
11729 "value": "Exportar a ODT con Emacs"
11729 "value": "Exportar a ODT"
1173011730 }
1173111731 },
1173211732 "fr": {
1173311733 "stringUnit": {
1173411734 "state": "translated",
11735 "value": "Exporter en ODT avec Emacs"
11735 "value": "Exporter en ODT"
1173611736 }
1173711737 },
1173811738 "ja": {
1173911739 "stringUnit": {
1174011740 "state": "translated",
11741 "value": "EmacsでODTに書き出す"
11741 "value": "ODTに書き出す"
1174211742 }
1174311743 },
1174411744 "pt-BR": {
1174511745 "stringUnit": {
1174611746 "state": "translated",
11747 "value": "Exportar para ODT com o Emacs"
11747 "value": "Exportar para ODT"
1174811748 }
1174911749 }
1175011750 }