import Foundation /// The colours the server gave a Markdown text's blocks of code, read from the /// HTML it made of them, by each block's text: a block this app's Markdown reads /// differently from the server's just goes without. enum MarkdownFences { static func ranges(_ html: String) -> [String: String] { var out: [String: String] = [:] for found in html.matches(of: #/
(.*?)/#.dotMatchesNewlines()) {
var text = String(found.output.2)
if text.hasSuffix("\n") { text.removeLast() }
out[unescaped(text)] = String(found.output.1)
}
return out
}
static func unescaped(_ text: String) -> String {
// `&` last, so that an escaped `<` stays one.
text.replacing("<", with: "<").replacing(">", with: ">")
.replacing(""", with: "\"").replacing("'", with: "'").replacing("'", with: "'")
.replacing("&", with: "&")
}
}