Sources/OrgApp/ExportSettings.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Sources/OrgApp/ExportSettings.swift history · blame · raw

68 lines · 3414 bytes

7 symbols in this file
 1import Foundation
 2
 3/// A file's export choices: what the export dialog shows for it, and the scope and body-only
 4/// choice its single-format export commands follow, as `C-s` and `C-b` in Org's dispatcher.
 5/// Kept in the defaults by the file's path.
 6public struct ExportSettings: Codable, Equatable, Sendable {
 7    public var format = "html"
 8    /// The subtree at the caret, not the whole file.
 9    public var subtree = false
10    public var bodyOnly = false
11    public var openAfter = false
12
13    public static let key = "exportSettings"
14    /// Shown when body only is on and the format is a whole document.
15    public static let noBodyOnly = "This format has no body-only export. Turn body-only export off for this file first."
16
17    public init() {}
18
19    /// PDF, Word, RTF, plain text, ODT and iCalendar are whole documents.
20    public static func allowsBodyOnly(_ format: String) -> Bool {
21        !["pdf", "print-pdf", "odt", "docx", "rtf", "plain-text", "icalendar"].contains(format)
22    }
23
24    /// The settings kept for the file at `path`. Without any: the format and open-after choice
25    /// last exported with, the whole file, and the whole document.
26    public static func saved(for path: String, defaults: UserDefaults = .standard) -> ExportSettings {
27        if let kept = all(defaults)[path] { return kept }
28        var settings = ExportSettings()
29        settings.format = defaults.string(forKey: "exportFormat") ?? settings.format
30        settings.openAfter = defaults.bool(forKey: "exportOpenAfter")
31        return settings
32    }
33
34    /// Keeps the settings for the file at `path`; with `lastUsed`, the format and open-after
35    /// choice also become what a file without settings starts with.
36    public func save(for path: String, lastUsed: Bool = false, defaults: UserDefaults = .standard) {
37        var all = Self.all(defaults)
38        all[path] = self
39        if let data = try? JSONEncoder().encode(all) { defaults.set(data, forKey: Self.key) }
40        guard lastUsed else { return }
41        defaults.set(format, forKey: "exportFormat")
42        defaults.set(openAfter, forKey: "exportOpenAfter")
43    }
44
45    /// The scope and body-only choice a single-format export command follows for `format`. Nil
46    /// when body only is on and the format has none. The agenda files' calendars are not the
47    /// file's export, so they follow neither.
48    public func commandOptions(_ format: String) -> (subtree: Bool, bodyOnly: Bool)? {
49        if format.hasPrefix("icalendar-") { return (false, false) }
50        guard !bodyOnly || Self.allowsBodyOnly(format == "html-open" ? "html" : format) else { return nil }
51        return (subtree, bodyOnly)
52    }
53
54    /// The message after an export to the file named `name`, saying what was exported when it
55    /// was not the whole file as a whole document.
56    public static func exported(_ name: String, subtree: Bool, bodyOnly: Bool) -> String {
57        switch (subtree, bodyOnly) {
58        case (true, true): "Exported the subtree's body to \(name)"
59        case (true, false): "Exported the subtree to \(name)"
60        case (false, true): "Exported the body to \(name)"
61        case (false, false): "Exported to \(name)"
62        }
63    }
64
65    private static func all(_ defaults: UserDefaults) -> [String: ExportSettings] {
66        defaults.data(forKey: key).flatMap { try? JSONDecoder().decode([String: ExportSettings].self, from: $0) } ?? [:]
67    }
68}