From 229c6478a523ed9ab6fda637a2130a5e03aae64e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 13 Jul 2020 00:12:31 -0400 Subject: [PATCH] Gemini parser cleanup --- GeminiFormat/GeminiParser.swift | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/GeminiFormat/GeminiParser.swift b/GeminiFormat/GeminiParser.swift index 2df74fc..41167e4 100644 --- a/GeminiFormat/GeminiParser.swift +++ b/GeminiFormat/GeminiParser.swift @@ -14,28 +14,24 @@ public struct GeminiParser { public static func parse(text: String, baseURL: URL) -> Document { var doc = Document(url: baseURL) - var preformattingState = PreformattingState.off + var inPreformattingBlock = false text.enumerateLines { (line, stop) in if line.starts(with: "```") { - switch preformattingState { - case .off: + if inPreformattingBlock { + inPreformattingBlock = false + // todo: should the toggle off line be a separate line type? + doc.lines.append(.preformattedToggle(alt: nil)) + } else { let alt: String? if line.count > 3 { alt = String(line[line.index(line.startIndex, offsetBy: 3)...]) } else { alt = nil } - preformattingState = .on(alt) + inPreformattingBlock = true doc.lines.append(.preformattedToggle(alt: alt)) - case let .on(alt): - preformattingState = .off - // todo: should the toggle off line be a separate line type? - doc.lines.append(.preformattedToggle(alt: nil)) } - if case .off = preformattingState { - - } - } else if case .on(_) = preformattingState { + } else if inPreformattingBlock { doc.lines.append(.preformattedText(line)) } else if line.starts(with: "=>") { // Link line @@ -85,13 +81,6 @@ public struct GeminiParser { } -fileprivate extension GeminiParser { - enum PreformattingState { - case off - case on(_ alt: String?) - } -} - fileprivate extension String { func firstNonWhitespaceIndex(after index: String.Index) -> String.Index { var index = index