Sources/OrgApp/TangleWriter.swift
27 lines · 1036 bytes
2 symbols in this file
1import Foundation
2import OrgConfig
3import OrgCore
4
5extension DocumentSession {
6 /// The files `tangle` would write.
7 public func tangleFiles(_ scope: Tangle.Scope) -> [String] {
8 guard let path, let document, case .success(let output) = Tangle.run(text: document.text, path: path, scope: scope, tree: document.tree) else { return [] }
9 return output.files.map(\.path)
10 }
11
12 /// Tangles the open file: its text as it is now, written beside it.
13 public func tangle(_ scope: Tangle.Scope) -> String {
14 guard let path, let document else { return "No file is open" }
15 switch Tangle.run(text: document.text, path: path, scope: scope, tree: document.tree) {
16 case .failure(.message(let message)):
17 return message
18 case .success(let output):
19 do {
20 try TangleWriter.write(output, source: path)
21 return TangleWriter.message(output, source: path)
22 } catch {
23 return error.message
24 }
25 }
26 }
27}