Gemini parser cleanup
This commit is contained in:
parent
1a4be887d3
commit
229c6478a5
|
@ -14,28 +14,24 @@ public struct GeminiParser {
|
||||||
public static func parse(text: String, baseURL: URL) -> Document {
|
public static func parse(text: String, baseURL: URL) -> Document {
|
||||||
var doc = Document(url: baseURL)
|
var doc = Document(url: baseURL)
|
||||||
|
|
||||||
var preformattingState = PreformattingState.off
|
var inPreformattingBlock = false
|
||||||
text.enumerateLines { (line, stop) in
|
text.enumerateLines { (line, stop) in
|
||||||
if line.starts(with: "```") {
|
if line.starts(with: "```") {
|
||||||
switch preformattingState {
|
if inPreformattingBlock {
|
||||||
case .off:
|
inPreformattingBlock = false
|
||||||
|
// todo: should the toggle off line be a separate line type?
|
||||||
|
doc.lines.append(.preformattedToggle(alt: nil))
|
||||||
|
} else {
|
||||||
let alt: String?
|
let alt: String?
|
||||||
if line.count > 3 {
|
if line.count > 3 {
|
||||||
alt = String(line[line.index(line.startIndex, offsetBy: 3)...])
|
alt = String(line[line.index(line.startIndex, offsetBy: 3)...])
|
||||||
} else {
|
} else {
|
||||||
alt = nil
|
alt = nil
|
||||||
}
|
}
|
||||||
preformattingState = .on(alt)
|
inPreformattingBlock = true
|
||||||
doc.lines.append(.preformattedToggle(alt: alt))
|
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 inPreformattingBlock {
|
||||||
|
|
||||||
}
|
|
||||||
} else if case .on(_) = preformattingState {
|
|
||||||
doc.lines.append(.preformattedText(line))
|
doc.lines.append(.preformattedText(line))
|
||||||
} else if line.starts(with: "=>") {
|
} else if line.starts(with: "=>") {
|
||||||
// Link line
|
// Link line
|
||||||
|
@ -85,13 +81,6 @@ public struct GeminiParser {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate extension GeminiParser {
|
|
||||||
enum PreformattingState {
|
|
||||||
case off
|
|
||||||
case on(_ alt: String?)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fileprivate extension String {
|
fileprivate extension String {
|
||||||
func firstNonWhitespaceIndex(after index: String.Index) -> String.Index {
|
func firstNonWhitespaceIndex(after index: String.Index) -> String.Index {
|
||||||
var index = index
|
var index = index
|
||||||
|
|
Loading…
Reference in New Issue