Commit a35aabf552
Unsigned
Layout: unified · split
Package.swift +1
| @@ -21,6 +21,7 @@ let package = Package( | ||
| 21 | 21 | dependencies: ["OrgSwift"], |
| 22 | 22 | swiftSettings: [.enableUpcomingFeature("BareSlashRegexLiterals")] |
| 23 | 23 | ), |
| 24 | .executableTarget(name: "OrgSwiftBenchmark", dependencies: ["OrgSwift"]), | |
| 24 | 25 | .testTarget(name: "OrgSwiftTests", dependencies: ["OrgSwift"]), |
| 25 | 26 | .testTarget(name: "OrgSwiftUITests", dependencies: ["OrgSwiftUI"]) |
| 26 | 27 | ] |
README.md +6
| @@ -171,3 +171,9 @@ corpus, whose golden outputs come from `orgo` (itself validated against Emacs | ||
| 171 | 171 | a semantic skeleton and compares them; the current pass/divergence state is |
| 172 | 172 | recorded per case, so a closed gap or a regression both surface as a failing |
| 173 | 173 | test. See [GAPS.md](GAPS.md) for the outstanding backlog. |
| 174 | ||
| 175 | ## Benchmark | |
| 176 | ||
| 177 | `swift run -c release OrgSwiftBenchmark [sections] [iterations]` times | |
| 178 | `OrgRenderer.renderToHTML` on a generated document of `sections` heading sections | |
| 179 | (default 2000, about 176 KB) and prints the best of `iterations` runs. | |
Sources/OrgSwift/AST/OrgAttributedStringRenderer.swift +14 −3
| @@ -33,12 +33,24 @@ public struct OrgAttributedStringRenderer: Sendable { | ||
| 33 | 33 | case .text(let text): |
| 34 | 34 | result += AttributedString(text) |
| 35 | 35 | |
| 36 | #if canImport(Darwin) | |
| 36 | 37 | case .bold(let children): |
| 37 | 38 | result += applying(.stronglyEmphasized, to: inline(children, ¬es)) |
| 38 | 39 | |
| 39 | 40 | case .italic(let children): |
| 40 | 41 | result += applying(.emphasized, to: inline(children, ¬es)) |
| 41 | 42 | |
| 43 | case .verbatim(let text), .code(let text): | |
| 44 | result += applying(.code, to: AttributedString(text)) | |
| 45 | #else | |
| 46 | // `InlinePresentationIntent` is Darwin-only; elsewhere these keep their text only. | |
| 47 | case .bold(let children), .italic(let children): | |
| 48 | result += inline(children, ¬es) | |
| 49 | ||
| 50 | case .verbatim(let text), .code(let text): | |
| 51 | result += AttributedString(text) | |
| 52 | #endif | |
| 53 | ||
| 42 | 54 | // Underline and strikethrough have no Foundation-portable attribute (the |
| 43 | 55 | // underlineStyle/strikethroughStyle keys live in the UIKit/AppKit scopes), so they |
| 44 | 56 | // travel as roles the UI layer applies. |
| @@ -48,9 +60,6 @@ public struct OrgAttributedStringRenderer: Sendable { | ||
| 48 | 60 | case .strikeThrough(let children): |
| 49 | 61 | result += applying(.strikeThrough, to: inline(children, ¬es)) |
| 50 | 62 | |
| 51 | case .verbatim(let text), .code(let text): | |
| 52 | result += applying(.code, to: AttributedString(text)) | |
| 53 | ||
| 54 | 63 | case .superscript(let children): |
| 55 | 64 | // No portable superscript attribute; mark it so a UI layer can raise it. |
| 56 | 65 | result += applying(.superscript, to: inline(children, ¬es)) |
| @@ -92,6 +101,7 @@ public struct OrgAttributedStringRenderer: Sendable { | ||
| 92 | 101 | |
| 93 | 102 | // MARK: - Nesting |
| 94 | 103 | |
| 104 | #if canImport(Darwin) | |
| 95 | 105 | /// Add an intent to every run, keeping intents the children already carry. Assigning |
| 96 | 106 | /// over the whole range instead would flatten `*bold /italic/*` to bold alone. |
| 97 | 107 | private func applying( |
| @@ -104,6 +114,7 @@ public struct OrgAttributedStringRenderer: Sendable { | ||
| 104 | 114 | } |
| 105 | 115 | return result |
| 106 | 116 | } |
| 117 | #endif | |
| 107 | 118 | |
| 108 | 119 | /// Mark runs that carry no role yet. A role is a single slot, so the innermost — the more |
| 109 | 120 | /// specific — wins; org nests two role-bearing markups (`_+both+_`) rarely enough that a |
Sources/OrgSwift/AST/OrgInlineParser.swift +20 −11
| @@ -262,10 +262,16 @@ extension OrgParser { | ||
| 262 | 262 | .contains { lower.hasSuffix($0) } |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | private static let footnoteReferencePattern = /^\[fn:([A-Za-z0-9_-]+)(?::([^\]]*))?\]/ | |
| 266 | private static let datePattern = /(\d{4}-\d{2}-\d{2})/ | |
| 267 | private static let timePattern = /(\d{2}:\d{2})(?:-(\d{2}:\d{2}))?/ | |
| 268 | private static let emailPattern = /^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}/ | |
| 269 | private static let environmentEndPattern = /\\end\{(\w+)\}/ | |
| 270 | ||
| 265 | 271 | private static func scanFootnote(_ chars: [Character], from start: Int) -> (object: OrgObject, next: Int)? { |
| 266 | 272 | guard chars[start...].starts(with: "[fn:") else { return nil } |
| 267 | 273 | let rest = String(chars[start...]) |
| 268 | guard let match = rest.firstMatch(of: /^\[fn:([A-Za-z0-9_-]+)(?::([^\]]*))?\]/) else { return nil } | |
| 274 | guard let match = rest.firstMatch(of: footnoteReferencePattern) else { return nil } | |
| 269 | 275 | let label = String(match.1) |
| 270 | 276 | // An inline footnote defines its note where it is used; parse that text as content. |
| 271 | 277 | let inline = match.2.map { parseInline(String($0)) } |
| @@ -284,12 +290,12 @@ extension OrgParser { | ||
| 284 | 290 | body.append(chars[j]); j += 1 |
| 285 | 291 | } |
| 286 | 292 | guard j < chars.count else { return nil } |
| 287 | guard let dateMatch = body.firstMatch(of: /(\d{4}-\d{2}-\d{2})/) else { return nil } | |
| 293 | guard let dateMatch = body.firstMatch(of: datePattern) else { return nil } | |
| 288 | 294 | |
| 289 | 295 | let date = String(dateMatch.1) |
| 290 | 296 | var time: String? |
| 291 | 297 | var endTime: String? |
| 292 | if let timeMatch = body.firstMatch(of: /(\d{2}:\d{2})(?:-(\d{2}:\d{2}))?/) { | |
| 298 | if let timeMatch = body.firstMatch(of: timePattern) { | |
| 293 | 299 | time = String(timeMatch.1) |
| 294 | 300 | if let end = timeMatch.2 { endTime = String(end) } |
| 295 | 301 | } |
| @@ -303,7 +309,7 @@ extension OrgParser { | ||
| 303 | 309 | var k = next + 3 |
| 304 | 310 | var second = "" |
| 305 | 311 | while k < chars.count, chars[k] != closing { second.append(chars[k]); k += 1 } |
| 306 | if k < chars.count, let endMatch = second.firstMatch(of: /(\d{4}-\d{2}-\d{2})/) { | |
| 312 | if k < chars.count, let endMatch = second.firstMatch(of: datePattern) { | |
| 307 | 313 | endDate = String(endMatch.1) |
| 308 | 314 | next = k + 1 |
| 309 | 315 | } |
| @@ -314,12 +320,15 @@ extension OrgParser { | ||
| 314 | 320 | } |
| 315 | 321 | |
| 316 | 322 | private static func scanBareURL(_ chars: [Character], from start: Int) -> (object: OrgObject, next: Int)? { |
| 317 | // Matched on the word alone, after a cheap prefix test: copying the rest of the run | |
| 318 | // at every `h` made long text quadratic. | |
| 323 | // `^https?:\/\/[^\s<>()\[\]]+` | |
| 319 | 324 | guard chars[start...].starts(with: "http") else { return nil } |
| 320 | let rest = String(chars[start..<(chars[start...].firstIndex(where: \.isWhitespace) ?? chars.count)]) | |
| 321 | guard let match = rest.firstMatch(of: /^https?:\/\/[^\s<>()\[\]]+/) else { return nil } | |
| 322 | var url = String(rest[match.range]) | |
| 325 | var j = start + 4 | |
| 326 | if j < chars.count, chars[j] == "s" { j += 1 } | |
| 327 | guard chars[j...].starts(with: "://") else { return nil } | |
| 328 | j += 3 | |
| 329 | let tail = chars[j...].prefix(while: { !$0.isWhitespace && !"<>()[]".contains($0) }) | |
| 330 | guard !tail.isEmpty else { return nil } | |
| 331 | var url = String(chars[start..<tail.endIndex]) | |
| 323 | 332 | while let last = url.last, ".,;:!?".contains(last) { url.removeLast() } |
| 324 | 333 | return (.link(OrgLink(target: .external(url), description: nil)), start + url.count) |
| 325 | 334 | } |
| @@ -336,7 +345,7 @@ extension OrgParser { | ||
| 336 | 345 | let local = chars[start...].prefix(while: { $0.isASCII && ($0.isLetter || $0.isNumber || "._%+-".contains($0)) }) |
| 337 | 346 | guard !local.isEmpty, start + local.count < chars.count, chars[start + local.count] == "@" else { return nil } |
| 338 | 347 | let rest = String(chars[start..<(chars[start...].firstIndex(where: \.isWhitespace) ?? chars.count)]) |
| 339 | guard let match = rest.firstMatch(of: /^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}/) else { | |
| 348 | guard let match = rest.firstMatch(of: emailPattern) else { | |
| 340 | 349 | return nil |
| 341 | 350 | } |
| 342 | 351 | let address = String(rest[match.range]) |
| @@ -399,7 +408,7 @@ extension OrgParser { | ||
| 399 | 408 | |
| 400 | 409 | /// Where the run's last `\end{name}` starts, in characters, and its name. |
| 401 | 410 | private static func lastEnvironmentEnd(_ text: String) -> (offset: Int, name: String)? { |
| 402 | guard text.contains(#"\end{"#), let last = text.matches(of: /\\end\{(\w+)\}/).last else { return nil } | |
| 411 | guard text.contains(#"\end{"#), let last = text.matches(of: environmentEndPattern).last else { return nil } | |
| 403 | 412 | return (text.distance(from: text.startIndex, to: last.range.lowerBound), String(last.1)) |
| 404 | 413 | } |
| 405 | 414 | |
Sources/OrgSwift/AST/OrgParser.swift +37 −12
| @@ -107,8 +107,8 @@ public enum OrgParser { | ||
| 107 | 107 | /// A heading, horizontal rule, footnote definition, or standalone image on |
| 108 | 108 | /// this line. Returns false if the line is none of those. |
| 109 | 109 | mutating func parseSingleLine(_ trimmed: String) -> Bool { |
| 110 | if let match = trimmed.firstMatch(of: /^(\*{1,6})\s+(.+)$/) { | |
| 111 | document.elements.append(.heading(OrgParser.parseHeading(stars: match.1.count, rest: String(match.2)))) | |
| 110 | if let (stars, rest) = OrgParser.headingLine(trimmed) { | |
| 111 | document.elements.append(.heading(OrgParser.parseHeading(stars: stars, rest: String(rest)))) | |
| 112 | 112 | } else if isOrgHorizontalRule(trimmed) { |
| 113 | 113 | document.elements.append(.horizontalRule) |
| 114 | 114 | } else if let def = orgFootnoteDefinition(in: trimmed) { |
| @@ -178,7 +178,8 @@ public enum OrgParser { | ||
| 178 | 178 | private static func startsNewConstruct(_ trimmed: String, raw: String) -> Bool { |
| 179 | 179 | // A keyword or a comment; `#12 fixed this` is prose. |
| 180 | 180 | if trimmed.hasPrefix("#+") || trimmed == "#" || trimmed.hasPrefix("# ") { return true } |
| 181 | if trimmed.firstMatch(of: /^\*{1,6}\s+/) != nil { return true } | |
| 181 | let stars = trimmed.prefix(while: { $0 == "*" }) | |
| 182 | if (1...6).contains(stars.count), trimmed[stars.endIndex...].first?.isWhitespace == true { return true } | |
| 182 | 183 | if isOrgHorizontalRule(trimmed) { return true } |
| 183 | 184 | if isTableLine(trimmed) { return true } |
| 184 | 185 | if orgFootnoteDefinition(in: trimmed) != nil { return true } |
| @@ -186,19 +187,31 @@ public enum OrgParser { | ||
| 186 | 187 | return false |
| 187 | 188 | } |
| 188 | 189 | |
| 190 | /// `^(\*{1,6})\s+(.+)$`: a heading's star count and the rest of its line. | |
| 191 | private static func headingLine(_ trimmed: String) -> (stars: Int, rest: Substring)? { | |
| 192 | let stars = trimmed.prefix(while: { $0 == "*" }) | |
| 193 | guard (1...6).contains(stars.count), let rest = textAfterWhitespace(trimmed[stars.endIndex...]) else { | |
| 194 | return nil | |
| 195 | } | |
| 196 | return (stars.count, rest) | |
| 197 | } | |
| 198 | ||
| 189 | 199 | // MARK: - LaTeX block |
| 190 | 200 | |
| 201 | private static let latexBeginLine = /^\\begin\{([^}]+)\}$/.ignoresCase() | |
| 202 | private static let latexEndLine = /^\\end\{([^}]+)\}$/.ignoresCase() | |
| 203 | ||
| 191 | 204 | /// The environment a `\begin{name}` line opens, as go-org's lexer reads it (any case). |
| 192 | 205 | private static func latexBegin(_ trimmed: String) -> String? { |
| 193 | 206 | guard trimmed.hasPrefix("\\") else { return nil } |
| 194 | return trimmed.firstMatch(of: /^\\begin\{([^}]+)\}$/.ignoresCase()).map { String($0.1).lowercased() } | |
| 207 | return trimmed.firstMatch(of: latexBeginLine).map { String($0.1).lowercased() } | |
| 195 | 208 | } |
| 196 | 209 | |
| 197 | 210 | /// The environment a `\end{name}` line closes. Like go-org's, it ends a paragraph |
| 198 | 211 | /// whether or not a block is open. |
| 199 | 212 | private static func latexEnd(_ trimmed: String) -> String? { |
| 200 | 213 | guard trimmed.hasPrefix("\\") else { return nil } |
| 201 | return trimmed.firstMatch(of: /^\\end\{([^}]+)\}$/.ignoresCase()).map { String($0.1).lowercased() } | |
| 214 | return trimmed.firstMatch(of: latexEndLine).map { String($0.1).lowercased() } | |
| 202 | 215 | } |
| 203 | 216 | |
| 204 | 217 | /// Each environment's `\end{name}` lines, in order, found in one pass so an opener |
| @@ -244,9 +257,12 @@ public enum OrgParser { | ||
| 244 | 257 | body = String(body.dropFirst(keyword.count)).trimmingCharacters(in: .whitespaces) |
| 245 | 258 | break |
| 246 | 259 | } |
| 247 | if let match = body.firstMatch(of: /^\[#([A-Z])\]\s*/) { | |
| 248 | priority = Character(String(match.1)) | |
| 249 | body = String(body[match.range.upperBound...]) | |
| 260 | // `^\[#([A-Z])\]\s*` | |
| 261 | let cookie = Array(body.prefix(4)) | |
| 262 | if cookie.count == 4, cookie[0] == "[", cookie[1] == "#", cookie[3] == "]", | |
| 263 | cookie[2].isASCII, cookie[2].isUppercase { | |
| 264 | priority = cookie[2] | |
| 265 | body = String(body.dropFirst(4).drop(while: \.isWhitespace)) | |
| 250 | 266 | } |
| 251 | 267 | let (title, tags) = splitHeadingTags(body) |
| 252 | 268 | return OrgHeading(level: stars, todo: todo, priority: priority, |
| @@ -410,10 +426,12 @@ public enum OrgParser { | ||
| 410 | 426 | |
| 411 | 427 | // MARK: - Helpers |
| 412 | 428 | |
| 429 | private static let attributePattern = try? NSRegularExpression( | |
| 430 | pattern: #":([A-Za-z_][A-Za-z0-9_-]*)\s+("[^"]*"|\S+)"# | |
| 431 | ) | |
| 432 | ||
| 413 | 433 | private static func parseAttributes(_ value: String) -> [(key: String, value: String)] { |
| 414 | guard let regex = try? NSRegularExpression(pattern: #":([A-Za-z_][A-Za-z0-9_-]*)\s+("[^"]*"|\S+)"#) else { | |
| 415 | return [] | |
| 416 | } | |
| 434 | guard let regex = attributePattern else { return [] } | |
| 417 | 435 | let ns = value as NSString |
| 418 | 436 | return regex.matches(in: value, range: NSRange(location: 0, length: ns.length)).map { m in |
| 419 | 437 | var raw = ns.substring(with: m.range(at: 2)) |
| @@ -425,7 +443,14 @@ public enum OrgParser { | ||
| 425 | 443 | private static func stripMarker(_ line: String) -> String { |
| 426 | 444 | let trimmed = line.trimmingCharacters(in: .whitespaces) |
| 427 | 445 | if trimmed.hasPrefix("- ") || trimmed.hasPrefix("+ ") { return String(trimmed.dropFirst(2)) } |
| 428 | if let match = trimmed.firstMatch(of: /^\d+[.)]\s+(.*)$/) { return String(match.1) } | |
| 446 | // `^\d+[.)]\s+(.*)$` | |
| 447 | let digits = trimmed.prefix(while: \.isNumber) | |
| 448 | let afterDigits = trimmed[digits.endIndex...] | |
| 449 | if !digits.isEmpty, afterDigits.first == "." || afterDigits.first == ")", | |
| 450 | afterDigits.dropFirst().first?.isWhitespace == true { | |
| 451 | let text = afterDigits.dropFirst().drop(while: \.isWhitespace) | |
| 452 | if !text.contains(where: \.isNewline) { return String(text) } | |
| 453 | } | |
| 429 | 454 | return trimmed |
| 430 | 455 | } |
| 431 | 456 | |
Sources/OrgSwift/Escaping.swift +40 −11
| @@ -62,24 +62,53 @@ private func sanitizeReadmeURLString( | ||
| 62 | 62 | return trimmedURL |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | // MARK: - Regex Helpers | |
| 65 | // MARK: - Line Scanners | |
| 66 | 66 | |
| 67 | // These run on every line, so they scan by hand rather than through a regex. Each doc comment | |
| 68 | // gives the pattern it implements; the character classes keep Swift Regex's meaning, where an | |
| 69 | // ASCII class matches only an ASCII character, `.` anything but a newline, and `\s` and `\d` | |
| 70 | // `Character.isWhitespace` and `Character.isNumber`. | |
| 71 | ||
| 72 | /// `^#\+([A-Za-z][A-Za-z0-9_-]*):\s*(.*)$`, the keyword lowercased and the value trimmed. | |
| 67 | 73 | func orgKeywordDirective(in line: String) -> (keyword: String, value: String)? { |
| 68 | guard let match = line.firstMatch(of: /^#\+([A-Za-z][A-Za-z0-9_-]*):\s*(.*)$/) else { | |
| 69 | return nil | |
| 70 | } | |
| 74 | guard line.hasPrefix("#+") else { return nil } | |
| 75 | let rest = line.dropFirst(2) | |
| 76 | guard let first = rest.first, isASCIILetter(first) else { return nil } | |
| 77 | let keyword = rest.prefix(while: { isASCIIWordCharacter($0) || $0 == "-" }) | |
| 78 | let afterKeyword = rest[keyword.endIndex...] | |
| 79 | guard afterKeyword.first == ":" else { return nil } | |
| 80 | let value = afterKeyword.dropFirst().drop(while: \.isWhitespace) | |
| 81 | guard !value.contains(where: \.isNewline) else { return nil } | |
| 71 | 82 | return ( |
| 72 | keyword: String(match.1).lowercased(), | |
| 73 | value: String(match.2).trimmingCharacters(in: .whitespaces) | |
| 83 | keyword: keyword.lowercased(), | |
| 84 | value: String(value).trimmingCharacters(in: .whitespaces) | |
| 74 | 85 | ) |
| 75 | 86 | } |
| 76 | 87 | |
| 88 | /// `^\s*-{5,}\s*$` | |
| 77 | 89 | func isOrgHorizontalRule(_ line: String) -> Bool { |
| 78 | matchesRegex(line, pattern: #"^\s*-{5,}\s*$"#) | |
| 90 | let dashes = line.drop(while: \.isWhitespace).prefix(while: { $0 == "-" }) | |
| 91 | return dashes.count >= 5 && line[dashes.endIndex...].allSatisfy(\.isWhitespace) | |
| 92 | } | |
| 93 | ||
| 94 | /// What `\s+(.+)$` captures at the start of `text`: the rest of the line after at least one | |
| 95 | /// whitespace character. A run of only whitespace gives up its last character, as the | |
| 96 | /// regex does when it backtracks. | |
| 97 | func textAfterWhitespace(_ text: Substring) -> Substring? { | |
| 98 | guard let first = text.first, first.isWhitespace else { return nil } | |
| 99 | let rest = text.drop(while: \.isWhitespace) | |
| 100 | if rest.isEmpty { | |
| 101 | guard text.count >= 2, let last = text.last, !last.isNewline else { return nil } | |
| 102 | return text.suffix(1) | |
| 103 | } | |
| 104 | return rest.contains(where: \.isNewline) ? nil : rest | |
| 105 | } | |
| 106 | ||
| 107 | func isASCIILetter(_ c: Character) -> Bool { | |
| 108 | c.isASCII && c.isLetter | |
| 79 | 109 | } |
| 80 | 110 | |
| 81 | func matchesRegex(_ text: String, pattern: String) -> Bool { | |
| 82 | guard let regex = try? NSRegularExpression(pattern: pattern) else { return false } | |
| 83 | let range = NSRange(location: 0, length: (text as NSString).length) | |
| 84 | return regex.firstMatch(in: text, range: range) != nil | |
| 111 | /// `[A-Za-z0-9_]` | |
| 112 | func isASCIIWordCharacter(_ c: Character) -> Bool { | |
| 113 | c.isASCII && (c.isLetter || c.isNumber || c == "_") | |
| 85 | 114 | } |
Sources/OrgSwift/Footnotes.swift +9 −2
| @@ -2,9 +2,16 @@ import Foundation | ||
| 2 | 2 | |
| 3 | 3 | /// Detect a reference-style footnote definition line: `[fn:label] text…`. Returns the |
| 4 | 4 | /// label and the definition text, or nil if the line is not one. |
| 5 | /// | |
| 6 | /// `^\[fn:([A-Za-z0-9_-]+)\]\s+(.+)$` | |
| 5 | 7 | func orgFootnoteDefinition(in line: String) -> (label: String, text: String)? { |
| 6 | guard let match = line.firstMatch(of: /^\[fn:([A-Za-z0-9_-]+)\]\s+(.+)$/) else { | |
| 8 | guard line.hasPrefix("[fn:") else { return nil } | |
| 9 | let rest = line.dropFirst(4) | |
| 10 | let label = rest.prefix(while: { isASCIIWordCharacter($0) || $0 == "-" }) | |
| 11 | let afterLabel = rest[label.endIndex...] | |
| 12 | guard !label.isEmpty, afterLabel.first == "]", | |
| 13 | let text = textAfterWhitespace(afterLabel.dropFirst()) else { | |
| 7 | 14 | return nil |
| 8 | 15 | } |
| 9 | return (String(match.1), String(match.2)) | |
| 16 | return (String(label), String(text)) | |
| 10 | 17 | } |
Sources/OrgSwift/Lists.swift +6 −2
| @@ -1,8 +1,12 @@ | ||
| 1 | 1 | import Foundation |
| 2 | 2 | |
| 3 | /// `^(\d+)\.\s+(.+)$` | |
| 3 | 4 | func orderedListItem(in line: String) -> String? { |
| 4 | guard let match = line.firstMatch(of: /^(\d+)\.\s+(.+)$/) else { return nil } | |
| 5 | return String(match.2) | |
| 5 | let digits = line.prefix(while: \.isNumber) | |
| 6 | let afterDigits = line[digits.endIndex...] | |
| 7 | guard !digits.isEmpty, afterDigits.first == ".", | |
| 8 | let text = textAfterWhitespace(afterDigits.dropFirst()) else { return nil } | |
| 9 | return String(text) | |
| 6 | 10 | } |
| 7 | 11 | |
| 8 | 12 | /// True if a line (already outdented to its level) starts a list item at column 0. |
Sources/OrgSwift/OrgRenderer.swift +15 −3
| @@ -129,10 +129,22 @@ public enum OrgRenderer { | ||
| 129 | 129 | /// Split an org heading's trailing `:tag1:tag2:` off its title. Tags are the final |
| 130 | 130 | /// whitespace-separated run of colon-delimited words; a heading without them returns its |
| 131 | 131 | /// text unchanged and no tags. |
| 132 | /// | |
| 133 | /// `^(.*?)\s+(:(?:[A-Za-z0-9_@#%]+:)+)$`: tags hold no whitespace, so they are the last | |
| 134 | /// whitespace-separated word, and the lazy title stops where the whitespace before it starts. | |
| 132 | 135 | func splitHeadingTags(_ heading: String) -> (title: String, tags: [String]) { |
| 133 | guard let match = heading.firstMatch(of: /^(.*?)\s+(:(?:[A-Za-z0-9_@#%]+:)+)$/) else { | |
| 136 | guard let space = heading.lastIndex(where: \.isWhitespace) else { return (heading, []) } | |
| 137 | let tagText = heading[heading.index(after: space)...] | |
| 138 | guard tagText.count >= 3, tagText.first == ":", tagText.last == ":", !tagText.contains("::"), | |
| 139 | tagText.allSatisfy({ $0 == ":" || isASCIIWordCharacter($0) || "@#%".contains($0) }) else { | |
| 134 | 140 | return (heading, []) |
| 135 | 141 | } |
| 136 | let tags = String(match.2).split(separator: ":").map(String.init).filter { !$0.isEmpty } | |
| 137 | return (String(match.1).trimmingCharacters(in: .whitespaces), tags) | |
| 142 | var titleEnd = space | |
| 143 | while titleEnd > heading.startIndex, heading[heading.index(before: titleEnd)].isWhitespace { | |
| 144 | titleEnd = heading.index(before: titleEnd) | |
| 145 | } | |
| 146 | let title = heading[..<titleEnd] | |
| 147 | guard !title.contains(where: \.isNewline) else { return (heading, []) } | |
| 148 | let tags = tagText.split(separator: ":").map(String.init) | |
| 149 | return (String(title).trimmingCharacters(in: .whitespaces), tags) | |
| 138 | 150 | } |
Sources/OrgSwiftBenchmark/main.swift added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | import Foundation | |
| 2 | import OrgSwift | |
| 3 | ||
| 4 | // Times OrgRenderer.renderToHTML on a generated document. Run in release: | |
| 5 | // swift run -c release OrgSwiftBenchmark [sections] [iterations] | |
| 6 | ||
| 7 | let arguments = CommandLine.arguments.dropFirst().compactMap { Int($0) } | |
| 8 | let sections = arguments.first ?? 2000 | |
| 9 | let iterations = arguments.dropFirst().first ?? 5 | |
| 10 | ||
| 11 | let source = (0..<sections).map { | |
| 12 | "* Heading \($0)\nParagraph with *bold* \($0) and ~code~ and https://example.com/\($0).\n- item\n" | |
| 13 | }.joined() | |
| 14 | ||
| 15 | var best = Double.infinity | |
| 16 | for _ in 0..<iterations { | |
| 17 | let start = Date() | |
| 18 | _ = OrgRenderer.renderToHTML(source) | |
| 19 | best = min(best, Date().timeIntervalSince(start)) | |
| 20 | } | |
| 21 | print(String(format: "%d sections, %d bytes: best of %d = %.1f ms", sections, source.utf8.count, iterations, best * 1000)) | |