1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
import Foundation
import Highlightr
import SwiftUI
import UIKit
enum SyntaxHighlightTheme {
case light
case dark
init(userInterfaceStyle: UIUserInterfaceStyle) {
self = userInterfaceStyle == .dark ? .dark : .light
}
init(colorScheme: ColorScheme) {
self = colorScheme == .dark ? .dark : .light
}
/// highlight.js theme names bundled with Highlightr. Xcode-like in light,
/// a muted dark palette in dark, so highlighting reads as native on both.
var highlightrName: String {
switch self {
case .light: "xcode"
case .dark: "atom-one-dark"
}
}
}
/// Multi-language syntax highlighting backed by Highlightr (highlight.js).
///
/// Highlightr wraps a JavaScriptCore context and is not thread-safe, so each
/// instance must stay on the thread/task that created it. Callers that fail to
/// resolve a language — or hit an unavailable engine — get `nil` and should
/// fall back to plain, escaped text.
final class SyntaxHighlighter {
private let highlightr: Highlightr?
private let supportedLanguages: Set<String>
init(theme: SyntaxHighlightTheme) {
let engine = Highlightr()
engine?.setTheme(to: theme.highlightrName)
highlightr = engine
supportedLanguages = Set(engine?.supportedLanguages() ?? [])
}
/// Full-document highlighted string, or `nil` when the language is unknown
/// or the engine is unavailable.
func attributedText(for code: String, language: String?, font: UIFont) -> NSAttributedString? {
guard let highlightr, let language = resolvedLanguage(language) else { return nil }
highlightr.theme.setCodeFont(font)
return highlightr.highlight(code, as: language, fastRender: true)
}
/// Inner HTML for a `<code>` element — color-styled `<span>`s — or `nil` to
/// fall back. Colors are inlined, so the caller must regenerate when the
/// theme changes.
func highlightedHTML(for code: String, language: String?) -> String? {
guard let attributed = attributedText(for: code, language: language, font: Self.htmlMeasurementFont) else {
return nil
}
return Self.html(from: attributed)
}
private static let htmlMeasurementFont = UIFont.monospacedSystemFont(ofSize: 12, weight: .regular)
private func resolvedLanguage(_ raw: String?) -> String? {
guard let trimmed = raw?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased(),
!trimmed.isEmpty else {
return nil
}
let mapped = Self.languageAliases[trimmed] ?? trimmed
return supportedLanguages.contains(mapped) ? mapped : nil
}
/// Resolves a filename to a highlight.js language identifier, or `nil` when
/// there is no confident match (so we render plain rather than mis-highlight).
static func language(forFileName fileName: String) -> String? {
let base = (fileName as NSString).lastPathComponent.lowercased()
if let byName = fileNameLanguages[base] {
return byName
}
let ext = (fileName as NSString).pathExtension.lowercased()
guard !ext.isEmpty else { return nil }
return extensionLanguages[ext]
}
private static func html(from attributed: NSAttributedString) -> String {
var html = ""
let range = NSRange(location: 0, length: attributed.length)
attributed.enumerateAttribute(.foregroundColor, in: range, options: []) { value, subrange, _ in
let fragment = (attributed.string as NSString).substring(with: subrange)
let escaped = escapeHTML(fragment)
if let color = value as? UIColor, let hex = color.hexRGBString {
html += "<span style=\"color:\(hex)\">\(escaped)</span>"
} else {
html += escaped
}
}
return html
}
/// Fence tags / short names that differ from highlight.js identifiers.
private static let languageAliases: [String: String] = [
"js": "javascript",
"jsx": "javascript",
"ts": "typescript",
"tsx": "typescript",
"py": "python",
"rb": "ruby",
"sh": "bash",
"shell": "bash",
"zsh": "bash",
"yml": "yaml",
"c++": "cpp",
"cc": "cpp",
"h": "cpp",
"hpp": "cpp",
"cs": "csharp",
"objc": "objectivec",
"objective-c": "objectivec",
"obj-c": "objectivec",
"html": "xml",
"htm": "xml",
"kt": "kotlin",
"rs": "rust",
"golang": "go",
"md": "markdown",
"ps1": "powershell",
"yaml": "yaml"
]
/// Filenames without a useful extension.
private static let fileNameLanguages: [String: String] = [
"dockerfile": "dockerfile",
"makefile": "makefile",
"gnumakefile": "makefile",
"cmakelists.txt": "cmake",
"gemfile": "ruby",
"rakefile": "ruby",
"podfile": "ruby",
"package.swift": "swift"
]
private static let extensionLanguages: [String: String] = [
"swift": "swift",
"js": "javascript",
"mjs": "javascript",
"cjs": "javascript",
"jsx": "javascript",
"ts": "typescript",
"tsx": "typescript",
"py": "python",
"rb": "ruby",
"go": "go",
"rs": "rust",
"c": "c",
"h": "c",
"cpp": "cpp",
"cxx": "cpp",
"cc": "cpp",
"hpp": "cpp",
"hxx": "cpp",
"m": "objectivec",
"mm": "objectivec",
"cs": "csharp",
"java": "java",
"kt": "kotlin",
"kts": "kotlin",
"scala": "scala",
"php": "php",
"pl": "perl",
"pm": "perl",
"lua": "lua",
"r": "r",
"dart": "dart",
"groovy": "groovy",
"hs": "haskell",
"ex": "elixir",
"exs": "elixir",
"erl": "erlang",
"clj": "clojure",
"sh": "bash",
"bash": "bash",
"zsh": "bash",
"fish": "bash",
"ps1": "powershell",
"json": "json",
"yaml": "yaml",
"yml": "yaml",
"toml": "ini",
"ini": "ini",
"cfg": "ini",
"conf": "ini",
"xml": "xml",
"html": "xml",
"htm": "xml",
"css": "css",
"scss": "scss",
"sass": "scss",
"less": "less",
"sql": "sql",
"md": "markdown",
"markdown": "markdown",
"diff": "diff",
"patch": "diff",
"cmake": "cmake",
"gradle": "groovy",
"vim": "vim"
]
}
private extension UIColor {
/// `#rrggbb` for HTML inline styles, or `nil` if the color isn't RGB-convertible.
var hexRGBString: String? {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
guard getRed(&red, green: &green, blue: &blue, alpha: &alpha) else { return nil }
let clamp: (CGFloat) -> Int = { Int((max(0, min(1, $0)) * 255).rounded()) }
return String(format: "#%02x%02x%02x", clamp(red), clamp(green), clamp(blue))
}
}
|