| @@ -7,11 +7,26 @@ import Foundation |
| 7 | 7 | /// not a second parser. It stays Foundation-only (no SwiftUI), so it is usable anywhere; a |
| 8 | 8 | /// SwiftUI block renderer would sit on top, using this for the inline runs inside each block. |
| 9 | 9 | public struct OrgAttributedStringRenderer: Sendable { |
| 10 | /// Resolution options, so repository-relative links resolve exactly as they do in HTML. |
| 11 | public var options: OrgRenderOptions |
| 10 | 12 | |
| 11 | | public init() {} |
| 13 | public init(options: OrgRenderOptions = .init()) { |
| 14 | self.options = options |
| 15 | } |
| 16 | |
| 17 | private var resolver: OrgURLResolver { OrgURLResolver(options: options) } |
| 12 | 18 | |
| 13 | 19 | /// Render one run of inline objects, carrying intents a UI layer can style. |
| 20 | /// |
| 21 | /// Footnote numbering is document-wide, so a caller rendering a whole document threads |
| 22 | /// its own ``OrgFootnotes`` through ``inline(_:_:)``; this overload numbers within the |
| 23 | /// run it is given. |
| 14 | 24 | public func inline(_ objects: [OrgObject]) -> AttributedString { |
| 25 | var notes = OrgFootnotes(document: OrgDocument()) |
| 26 | return inline(objects, ¬es) |
| 27 | } |
| 28 | |
| 29 | package func inline(_ objects: [OrgObject], _ notes: inout OrgFootnotes) -> AttributedString { |
| 15 | 30 | var result = AttributedString() |
| 16 | 31 | for object in objects { |
| 17 | 32 | switch object { |
| @@ -19,12 +34,12 @@ public struct OrgAttributedStringRenderer: Sendable { |
| 19 | 34 | result += AttributedString(text) |
| 20 | 35 | |
| 21 | 36 | case .bold(let children): |
| 22 | | var part = inline(children) |
| 37 | var part = inline(children, ¬es) |
| 23 | 38 | part.inlinePresentationIntent = .stronglyEmphasized |
| 24 | 39 | result += part |
| 25 | 40 | |
| 26 | 41 | case .italic(let children): |
| 27 | | var part = inline(children) |
| 42 | var part = inline(children, ¬es) |
| 28 | 43 | part.inlinePresentationIntent = .emphasized |
| 29 | 44 | result += part |
| 30 | 45 | |
| @@ -32,12 +47,12 @@ public struct OrgAttributedStringRenderer: Sendable { |
| 32 | 47 | // underlineStyle/strikethroughStyle keys live in the UIKit/AppKit scopes), so they |
| 33 | 48 | // travel as roles the UI layer applies. |
| 34 | 49 | case .underline(let children): |
| 35 | | var part = inline(children) |
| 50 | var part = inline(children, ¬es) |
| 36 | 51 | part.orgRole = .underline |
| 37 | 52 | result += part |
| 38 | 53 | |
| 39 | 54 | case .strikeThrough(let children): |
| 40 | | var part = inline(children) |
| 55 | var part = inline(children, ¬es) |
| 41 | 56 | part.orgRole = .strikeThrough |
| 42 | 57 | result += part |
| 43 | 58 | |
| @@ -48,7 +63,7 @@ public struct OrgAttributedStringRenderer: Sendable { |
| 48 | 63 | |
| 49 | 64 | case .superscript(let children): |
| 50 | 65 | // No portable superscript attribute; mark it so a UI layer can raise it. |
| 51 | | var part = inline(children) |
| 66 | var part = inline(children, ¬es) |
| 52 | 67 | part.orgRole = .superscript |
| 53 | 68 | result += part |
| 54 | 69 | |
| @@ -60,8 +75,8 @@ public struct OrgAttributedStringRenderer: Sendable { |
| 60 | 75 | part.orgRole = .timestamp |
| 61 | 76 | result += part |
| 62 | 77 | |
| 63 | | case .footnoteRef(let label, _): |
| 64 | | var part = AttributedString("[\(label)]") |
| 78 | case .footnoteRef(let label, let inlineNote): |
| 79 | var part = AttributedString("[\(notes.number(for: label, inline: inlineNote))]") |
| 65 | 80 | part.orgRole = .footnoteReference |
| 66 | 81 | result += part |
| 67 | 82 | |
| @@ -71,8 +86,9 @@ public struct OrgAttributedStringRenderer: Sendable { |
| 71 | 86 | result += part |
| 72 | 87 | |
| 73 | 88 | case .link(let link): |
| 74 | | var part = link.description.map { inline($0) } ?? AttributedString(displayValue(link.target)) |
| 75 | | if let url = URL(string: hrefValue(link.target)) { |
| 89 | var part = link.description.map { inline($0, ¬es) } |
| 90 | ?? AttributedString(resolver.displayValue(link.target)) |
| 91 | if let href = resolver.href(for: link.target), let url = URL(string: href) { |
| 76 | 92 | part.link = url |
| 77 | 93 | } |
| 78 | 94 | result += part |
| @@ -93,21 +109,6 @@ public struct OrgAttributedStringRenderer: Sendable { |
| 93 | 109 | } |
| 94 | 110 | } |
| 95 | 111 | |
| 96 | | private func hrefValue(_ target: OrgLinkTarget) -> String { |
| 97 | | switch target { |
| 98 | | case .external(let url): return url |
| 99 | | case .file(let path): return path |
| 100 | | case .id(let identifier): return "#\(identifier)" |
| 101 | | } |
| 102 | | } |
| 103 | | |
| 104 | | private func displayValue(_ target: OrgLinkTarget) -> String { |
| 105 | | switch target { |
| 106 | | case .external(let url): return url |
| 107 | | case .file(let path): return path |
| 108 | | case .id(let identifier): return identifier |
| 109 | | } |
| 110 | | } |
| 111 | 112 | } |
| 112 | 113 | |
| 113 | 114 | // MARK: - Custom attribute |