oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1// Copied from Equipo's EquipoKit (oficina/equipo 00d09aa7,
2// native/apple/Sources/EquipoKit/Markdown/MarkdownDraft.swift). Base's changes are
3// marked "Base:"; keep the rest in step with Equipo's.
4import Foundation
5
6/// The web's steady/open-link rules. Unmatched delimiters are removed only
7/// from text leaves in the final open paragraph, after CommonMark parsing.
8enum MarkdownDraft {
9 static func matches(_ pattern: String, _ text: String) -> Bool {
10 text.range(of: pattern, options: .regularExpression) != nil
11 }
12 static func entities(_ value: String) -> String { MarkdownEntities.link(value) }
13 static func decodeText(_ value: String) -> String {
14 let bytes = Array(value.utf8)
15 var out: [UInt8] = [], at = 0
16 while at < bytes.count {
17 if bytes[at] == 92, at + 1 < bytes.count, (33...47).contains(bytes[at+1]) || (58...64).contains(bytes[at+1]) || (91...96).contains(bytes[at+1]) || (123...126).contains(bytes[at+1]) { at += 1 }
18 out.append(bytes[at]); at += 1
19 }
20 return MarkdownEntities.text(String(decoding: out, as: UTF8.self))
21 }
22 static func steady(_ source: String) -> String {
23 // CRLF is one Swift Character, but the web splits on the LF byte.
24 let bytes = Array(source.utf8)
25 let start = bytes.lastIndex(of: 10).map { $0 + 1 } ?? 0
26 let tail = Array(bytes[start...])
27 return markerOnly(tail) || fenceLine(tail) ? String(decoding: bytes[..<start], as: UTF8.self) : source
28 }
29
30 private static func space(_ c: UInt8) -> Bool { c == 32 || c == 9 }
31 private static func suffix(_ b: [UInt8], _ start: Int) -> Bool {
32 var i = start
33 while i < b.count, space(b[i]) { i += 1 }
34 while i < b.count, [42, 95, 96].contains(b[i]) { i += 1 }
35 return i == b.count
36 }
37 // Each alternative is scanned once. No backtracking through whitespace.
38 static func markerOnly(_ b: [UInt8]) -> Bool {
39 var i = 0
40 while i < b.count, space(b[i]) { i += 1 }
41 if suffix(b, i) { return true }
42 guard i < b.count else { return true }
43 if [45, 43, 42, 62].contains(b[i]), suffix(b, i + 1) { return true }
44 var j = i
45 while j < b.count, (48...57).contains(b[j]), j - i < 9 { j += 1 }
46 if j > i {
47 if j < b.count, [46, 41].contains(b[j]) { j += 1 }
48 if suffix(b, j) { return true }
49 }
50 j = i
51 while j < b.count, b[j] == 35, j - i < 6 { j += 1 }
52 if j > i, suffix(b, j) { return true }
53 j = i
54 while j < b.count, [96, 126].contains(b[j]) { j += 1 }
55 if j > i, suffix(b, j) { return true }
56 j = i
57 while j < b.count, [45, 61, 95, 42, 32, 9].contains(b[j]) { j += 1 }
58 return j > i && suffix(b, j)
59 }
60 static func fenceLine(_ b: [UInt8]) -> Bool {
61 var i = 0
62 while i < b.count, space(b[i]) || b[i] == 62 { i += 1 }
63 func fence(_ j: Int) -> Bool {
64 j + 2 < b.count && (b[j] == 96 || b[j] == 126) && b[j+1] == b[j] && b[j+2] == b[j]
65 }
66 if fence(i) { return true }
67 guard i < b.count else { return false }
68 if [45, 43, 42].contains(b[i]) { i += 1 }
69 else {
70 let start = i
71 while i < b.count, (48...57).contains(b[i]), i - start < 9 { i += 1 }
72 guard i > start, i < b.count, [46, 41].contains(b[i]) else { return false }
73 i += 1
74 }
75 guard i < b.count, space(b[i]) else { return false }
76 while i < b.count, space(b[i]) { i += 1 }
77 return fence(i)
78 }
79}
80