Sources/OrgApp/DenoteCapture.swift

56 lines · 3472 bytes

 1import Foundation
 2import OrgCore
 3
 4/// `denote-org-capture`: a capture template with `denote = true` first asks for a new note's
 5/// title and the questions `denote-prompts` lists, then files the entry in that note.
 6public struct DenoteCapture: Sendable {
 7    /// The questions, in order: `title`, then `keywords`, `subdirectory`, `date`, `template`
 8    /// and `signature` as `denote-prompts` orders them.
 9    public let questions: [Capture.Question]
10    let steps: [String]
11    let roots: [String]
12    let folders: [(label: String, path: String)]
13    let templates: [(key: String, text: String)]
14
15    /// With `paths`, the files whose keywords are offered.
16    public init(roots: [String], paths: [String], defaults: UserDefaults = .standard) {
17        let folders = DenoteNote.directories(roots: roots, defaults: defaults)
18        let choices = DenoteNote.folderChoices(folders)
19        let templates = UserConfig.denoteTemplates(defaults)
20        let keywords = Denote.keywords(paths.flatMap { Denote.name(ofFile: $0).keywords })
21        var steps = ["title"], questions = [Capture.Question(prompt: "New file TITLE", defaultValue: "", choices: [])]
22        for prompt in OrgPreferences.denotePrompts(defaults) where !steps.contains(prompt) {
23            switch prompt {
24            case "keywords": questions.append(Capture.Question(prompt: "New file KEYWORDS", defaultValue: "", choices: keywords))
25            case "subdirectory" where choices.count > 1: questions.append(Capture.Question(prompt: "Select SUBDIRECTORY", defaultValue: ".", choices: choices.map(\.label)))
26            case "date": questions.append(Capture.Question(prompt: "DATE and TIME for note", defaultValue: "", choices: [], kind: .date(withTime: true, inactive: false)))
27            case "template" where !templates.isEmpty: questions.append(Capture.Question(prompt: "Select TEMPLATE key", defaultValue: templates[0].key, choices: templates.map(\.key)))
28            case "signature": questions.append(Capture.Question(prompt: "New file SIGNATURE", defaultValue: "", choices: []))
29            default: continue
30            }
31            steps.append(prompt)
32        }
33        self.steps = steps
34        self.questions = questions
35        self.roots = folders
36        self.folders = choices
37        self.templates = templates
38    }
39
40    /// Makes the note from the first `questions.count` answers and returns its path.
41    public func create(_ answers: [String], now: Date = Date()) throws -> String {
42        guard let root = roots.first else { throw Dailies.Failure("No folder for new notes") }
43        var title = "", keywords: [String] = [], folder = root, date = now, template = "", signature = ""
44        for (step, answer) in zip(steps, answers) {
45            switch step {
46            case "title": title = answer.trimmingCharacters(in: .whitespaces)
47            case "keywords": keywords = answer.split(whereSeparator: { $0 == "," || $0 == ":" || $0.isWhitespace }).map(String.init)
48            case "subdirectory": folder = folders.first { $0.label == answer }?.path ?? root
49            case "date": date = answer.isEmpty ? now : RoamActions.date(answer, now: now) ?? now
50            case "template": template = templates.first { $0.key == answer }?.text ?? ""
51            default: signature = answer
52            }
53        }
54        return try DenoteNote.create(title: title, keywords: keywords, in: folder, roots: roots, now: date, signature: signature, template: template).path
55    }
56}