swiftgemtext

[ACTIVE] gemtext parsing in swift
git clone git://git.figbert.com/swiftgemtext.git
Log | Files | Refs | README

commit 296ba858ff9f528c1f35c693da841ee380224ee8
parent b4038f0c19234081e354b05111194c6da9c2e754
Author: FIGBERT <figbert@figbert.com>
Date:   Tue, 20 Apr 2021 11:23:24 -0700

Bring link line parsing more in line with spec

Diffstat:
MSources/SwiftGemtext/SwiftGemtext.swift | 17++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/Sources/SwiftGemtext/SwiftGemtext.swift b/Sources/SwiftGemtext/SwiftGemtext.swift @@ -43,14 +43,21 @@ 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 delim = urlAndCaption.firstIndex(of: " ") - if delim != nil { - let url = URL(string: String(urlAndCaption[..<delim!]))! - let caption = String(urlAndCaption[delim!...]).trimmingCharacters(in: .whitespacesAndNewlines) - return LineType.Link(url, caption) + let spaceDelim = urlAndCaption.firstIndex(of: " ") + let tabDelim = urlAndCaption.firstIndex(of: "\t") + 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) } else if line.starts(with: "* ") { // Unordered List Line return LineType.UnorderedList(line.dropFirst(2).trimmingCharacters(in: .whitespacesAndNewlines)) } else if line.starts(with: ">") { // Quote Line