commit 51dea98544e75035f72b38a800f0f2caddc96d87
parent cd31cb58eb9b2eddf8e940d81fd555962cddea05
Author: FIGBERT <figbert@figbert.com>
Date: Wed, 20 Jul 2022 18:39:32 -0700
Simplify link parsing
Diffstat:
1 file changed, 4 insertions(+), 17 deletions(-)
diff --git a/Sources/SwiftGemtext/SwiftGemtext.swift b/Sources/SwiftGemtext/SwiftGemtext.swift
@@ -44,24 +44,11 @@ struct Gemtext {
}
return LineType.Heading(level, line.dropFirst(level).trimmingCharacters(in: .whitespacesAndNewlines))
} else if line.starts(with: "=>") { // Link Line
- let urlAndCaption = line.dropFirst(2).trimmingCharacters(in: .whitespacesAndNewlines)
- let spaceDelim = urlAndCaption.firstIndex(of: " ")
- let tabDelim = urlAndCaption.firstIndex(of: "\t")
+ let base = line.dropFirst(2).trimmingCharacters(in: .whitespacesAndNewlines)
+ let url = base.components(separatedBy: .whitespaces).first!
+ let caption = base.dropFirst(url.count).trimmingCharacters(in: .whitespaces)
- var delim: String.Index
- if spaceDelim != nil && tabDelim != nil {
- delim = min(spaceDelim!, tabDelim!)
- } else if spaceDelim != nil && tabDelim == nil {
- delim = spaceDelim!
- } else if spaceDelim == nil && tabDelim != nil {
- delim = tabDelim!
- } else {
- return LineType.Link(URL(string: urlAndCaption)!, nil)
- }
-
- let url = URL(string: String(urlAndCaption[..<delim]))!
- let caption = String(urlAndCaption[delim...]).trimmingCharacters(in: .whitespacesAndNewlines)
- return LineType.Link(url, caption)
+ return LineType.Link(URL(string: url)!, caption.isEmpty ? nil : caption)
} else if line.starts(with: "* ") { // Unordered List Line
return LineType.UnorderedList(line.dropFirst(2).trimmingCharacters(in: .whitespacesAndNewlines))
} else if line.starts(with: ">") { // Quote Line