Repository navigation
Expand file tree
/
Copy pathHTMLFormatter.swift
More file actions
323 lines (261 loc) · 10 KB
/
Copy pathHTMLFormatter.swift
File metadata and controls
323 lines (261 loc) · 10 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
This source file is part of the Swift.org open source project
Copyright (c) 2023 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
/// Options given to the ``HTMLFormatter``.
public struct HTMLFormatterOptions: OptionSet {
public var rawValue: UInt
public init(rawValue: UInt) {
self.rawValue = rawValue
}
/// Attempt to parse blockquotes as asides.
///
/// If a blockquote is found to begin with an aside marker, e.g. "`Remark:`" then the
/// corresponding HTML will be an `<aside>` tag instead of a `<blockquote>` tag, with the aside
/// kind given in the `data-kind` attribute.
///
/// - Note: To prevent false positives, the aside checking will only look for a single-word
/// aside marker, i.e. the following blockquote will not parse as an aside:
///
/// ```markdown
/// > This is a compound sentence: It contains two clauses separated by a colon.
/// ```
public static let parseAsides = HTMLFormatterOptions(rawValue: 1 << 0)
/// Parse inline attributes as JSON and use the `"class"` property as the resulting span's `class`.
public static let parseInlineAttributeClass = HTMLFormatterOptions(rawValue: 1 << 1)
}
/// A ``MarkupWalker`` that prints rendered HTML for a given ``Markup`` tree.
public struct HTMLFormatter: MarkupWalker {
/// The resulting HTML built up after printing.
public private(set) var result = ""
let options: HTMLFormatterOptions
var inTableHead = false
var tableColumnAlignments: [Table.ColumnAlignment?]? = nil
var currentTableColumn = 0
public init(options: HTMLFormatterOptions = []) {
self.options = options
}
/// Format HTML for the given markup tree.
public static func format(_ markup: Markup, options: HTMLFormatterOptions = []) -> String {
var walker = HTMLFormatter(options: options)
walker.visit(markup)
return walker.result
}
/// Format HTML for the given input text.
public static func format(_ inputString: String, options: HTMLFormatterOptions = []) -> String {
let document = Document(parsing: inputString)
return format(document, options: options)
}
// MARK: Block elements
public mutating func visitBlockQuote(_ blockQuote: BlockQuote) -> () {
if self.options.contains(.parseAsides), let aside = Aside(blockQuote, tagRequirement: .requireSingleWordTag) {
result += "<aside data-kind=\"\(aside.kind.rawValue)\">\n"
for child in aside.content {
visit(child)
}
result += "</aside>\n"
} else {
result += "<blockquote>\n"
descendInto(blockQuote)
result += "</blockquote>\n"
}
}
public mutating func visitCodeBlock(_ codeBlock: CodeBlock) -> () {
let languageAttr: String
if let language = codeBlock.language {
languageAttr = " class=\"language-\(language)\""
} else {
languageAttr = ""
}
result += "<pre><code\(languageAttr)>\(codeBlock.code)</code></pre>\n"
}
public mutating func visitHeading(_ heading: Heading) -> () {
result += "<h\(heading.level)>\(heading.plainText)</h\(heading.level)>\n"
}
public mutating func visitThematicBreak(_ thematicBreak: ThematicBreak) -> () {
result += "<hr />\n"
}
public mutating func visitHTMLBlock(_ html: HTMLBlock) -> () {
result += html.rawHTML
}
public mutating func visitListItem(_ listItem: ListItem) -> () {
result += "<li>"
if let checkbox = listItem.checkbox {
result += "<input type=\"checkbox\" disabled=\"\""
if checkbox == .checked {
result += " checked=\"\""
}
result += " /> "
}
descendInto(listItem)
result += "</li>\n"
}
public mutating func visitOrderedList(_ orderedList: OrderedList) -> () {
let start: String
if orderedList.startIndex != 1 {
start = " start=\"\(orderedList.startIndex)\""
} else {
start = ""
}
result += "<ol\(start)>\n"
descendInto(orderedList)
result += "</ol>\n"
}
public mutating func visitUnorderedList(_ unorderedList: UnorderedList) -> () {
result += "<ul>\n"
descendInto(unorderedList)
result += "</ul>\n"
}
public mutating func visitParagraph(_ paragraph: Paragraph) -> () {
result += "<p>"
descendInto(paragraph)
result += "</p>\n"
}
public mutating func visitTable(_ table: Table) -> () {
result += "<table>\n"
tableColumnAlignments = table.columnAlignments
descendInto(table)
tableColumnAlignments = nil
result += "</table>\n"
}
public mutating func visitTableHead(_ tableHead: Table.Head) -> () {
result += "<thead>\n"
result += "<tr>\n"
inTableHead = true
currentTableColumn = 0
descendInto(tableHead)
inTableHead = false
result += "</tr>\n"
result += "</thead>\n"
}
public mutating func visitTableBody(_ tableBody: Table.Body) -> () {
if !tableBody.isEmpty {
result += "<tbody>\n"
descendInto(tableBody)
result += "</tbody>\n"
}
}
public mutating func visitTableRow(_ tableRow: Table.Row) -> () {
result += "<tr>\n"
currentTableColumn = 0
descendInto(tableRow)
result += "</tr>\n"
}
public mutating func visitTableCell(_ tableCell: Table.Cell) -> () {
guard let alignments = tableColumnAlignments, currentTableColumn < alignments.count else { return }
guard tableCell.colspan > 0 && tableCell.rowspan > 0 else { return }
let element: String
if inTableHead {
element = "th"
} else {
element = "td"
}
if inTableHead {
result += "<\(element)"
} else {
result += "<\(element)"
}
if let alignment = alignments[currentTableColumn] {
result += " align=\"\(alignment)\""
}
currentTableColumn += 1
if tableCell.rowspan > 1 {
result += " rowspan=\"\(tableCell.rowspan)\""
}
if tableCell.colspan > 1 {
result += " colspan=\"\(tableCell.colspan)\""
}
result += ">"
descendInto(tableCell)
result += "</\(element)>\n"
}
// MARK: Inline elements
mutating func printInline(tag: String, _ content: Markup) {
result += "<\(tag)>"
descendInto(content)
result += "</\(tag)>"
}
public mutating func visitInlineCode(_ inlineCode: InlineCode) -> () {
result += "<code>\(inlineCode.code)</code>"
}
public mutating func visitEmphasis(_ emphasis: Emphasis) -> () {
printInline(tag: "em", emphasis)
}
public mutating func visitStrong(_ strong: Strong) -> () {
printInline(tag: "strong", strong)
}
public mutating func visitImage(_ image: Image) -> () {
result += "<img"
if let source = image.source, !source.isEmpty {
result += " src=\"\(source)\""
}
if let title = image.title, !title.isEmpty {
result += " title=\"\(title)\""
}
result += " />"
}
public mutating func visitInlineHTML(_ inlineHTML: InlineHTML) -> () {
result += inlineHTML.rawHTML
}
public mutating func visitLineBreak(_ lineBreak: LineBreak) -> () {
result += "<br />\n"
}
public mutating func visitSoftBreak(_ softBreak: SoftBreak) -> () {
result += "\n"
}
public mutating func visitLink(_ link: Link) -> () {
result += "<a"
if let destination = link.destination {
result += " href=\"\(destination)\""
}
result += ">"
descendInto(link)
result += "</a>"
}
public mutating func visitText(_ text: Text) -> () {
result += text.string
}
public mutating func visitStrikethrough(_ strikethrough: Strikethrough) -> () {
printInline(tag: "del", strikethrough)
}
public mutating func visitSymbolLink(_ symbolLink: SymbolLink) -> () {
if let destination = symbolLink.destination {
result += "<code>\(destination)</code>"
}
}
public mutating func visitInlineAttributes(_ attributes: InlineAttributes) -> () {
result += "<span data-attributes=\"\(attributes.attributes.replacingOccurrences(of: "\"", with: "\\\""))\""
let wrappedAttributes = "{\(attributes.attributes)}"
if options.contains(.parseInlineAttributeClass),
let attributesData = wrappedAttributes.data(using: .utf8)
{
struct ParsedAttributes: Decodable {
var `class`: String
}
let decoder = JSONDecoder()
// JSON5 parsing is available in Apple Foundation as of macOS 12 et al, or in Swift
// Foundation as of Swift 6.0
// Note: We don't turn on `.assumesTopLevelDictionary` to allow parsing to work on older
// compilers and OSs. If/when Swift-Markdown assumes a minimum Swift version of 6.0, we
// can clean this up to always use JSON5 and top-level dictionaries.
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
if #available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) {
decoder.allowsJSON5 = true
}
#elseif compiler(>=6.0)
decoder.allowsJSON5 = true
#endif
let parsedAttributes = try? decoder.decode(ParsedAttributes.self, from: attributesData)
if let parsedAttributes = parsedAttributes {
result += " class=\"\(parsedAttributes.class)\""
}
}
result += ">"
descendInto(attributes)
result += "</span>"
}
}