krz/org-swift

A dependency-free Swift library that renders org-mode to sanitized HTML. html library org-mode swift

Sources/OrgSwiftUI/OrgLayout.swift

v0.4.0
org-swift/Sources/OrgSwiftUI/OrgLayout.swift history · blame · raw

86 lines · 2706 bytes

 1import OrgSwift
 2import SwiftUI
 3
 4/// A document prepared for display: inline runs already styled, footnotes already numbered,
 5/// image URLs already resolved, code already highlighted.
 6///
 7/// The walk happens once, ahead of `body`, because three things it does must not happen per
 8/// layout pass — footnote numbering is sequential and document-wide, syntax highlighting is
 9/// expensive, and both would otherwise re-run every time SwiftUI recomputes a view.
10struct OrgLayout {
11    var blocks: [OrgBlock] = []
12    var footnotes: [OrgFootnote] = []
13}
14
15struct OrgFootnote: Identifiable {
16    var number: Int
17    var text: AttributedString
18    var id: Int { number }
19}
20
21/// A block ready to render. Mirrors ``OrgElement`` minus the cases that resolve away:
22/// footnote definitions move to ``OrgLayout/footnotes``, and an export block for another
23/// backend drops entirely.
24indirect enum OrgBlock: Identifiable {
25    case heading(text: AttributedString, level: Int)
26    case paragraph(AttributedString)
27    case code(AttributedString)
28    case quote([OrgBlock])
29    case center([OrgBlock])
30    case verse([AttributedString])
31    case special(name: String, blocks: [OrgBlock])
32    case list(OrgListLayout)
33    case table(OrgTableLayout)
34    case image(source: URL, alt: String, caption: AttributedString?)
35    case badges([OrgBadge])
36    case captioned(caption: AttributedString?, blocks: [OrgBlock])
37    case rule
38
39    /// Positional identity. Blocks are rebuilt wholesale when the source changes, so their
40    /// index in the layout is stable for as long as the layout is.
41    var id: String { String(describing: self).prefix(64).description }
42}
43
44/// One image in a badge row, with the link it stands for when it has one.
45struct OrgBadge: Identifiable {
46    var source: URL
47    var alt: String
48    var link: URL?
49    var id: URL { source }
50}
51
52struct OrgListLayout {
53    var kind: OrgListKind
54    var items: [Item]
55
56    struct Item: Identifiable {
57        var checkbox: OrgCheckbox?
58        var term: AttributedString?
59        var paragraphs: [AttributedString]
60        var sublist: OrgListLayout?
61        var id = UUID()
62    }
63}
64
65struct OrgTableLayout {
66    /// Cell rows only; org's `|---|` rule rows are structure, carried by ``headerRowCount``.
67    var rows: [Row]
68    var headerRowCount: Int
69    var alignments: [OrgAlignment?]
70
71    struct Row: Identifiable {
72        var cells: [Cell]
73        var id = UUID()
74    }
75
76    struct Cell: Identifiable {
77        var text: AttributedString
78        var id = UUID()
79    }
80
81    var columnCount: Int { rows.map(\.cells.count).max() ?? 0 }
82
83    func alignment(column: Int) -> OrgAlignment? {
84        column < alignments.count ? alignments[column] : nil
85    }
86}