Gemini/GeminiRenderer/RenderingBlock.swift

57 lines
1.7 KiB
Swift
Raw Normal View History

2020-07-13 22:12:04 +00:00
//
// RenderingBlock.swift
// GeminiRenderer
//
// Created by Shadowfacts on 7/12/20.
//
import Foundation
import GeminiFormat
enum RenderingBlock: Equatable {
case text(String)
case link(URL, text: String?)
case preformatted(String, alt: String?)
case heading(String, level: Document.HeadingLevel)
case unorderedListItem(String)
case quote(String)
}
extension Document {
var renderingBlocks: [RenderingBlock] {
var blocks = [RenderingBlock]()
var currentPreformatted: (text: String, alt: String?)?
for line in self.lines {
switch line {
case let .preformattedToggle(alt: alt):
if let (text, alt) = currentPreformatted {
// drop last trailing newline
let realText = String(text.dropLast(1))
blocks.append(.preformatted(realText, alt: alt))
currentPreformatted = nil
} else {
currentPreformatted = ("", alt: alt)
}
case let .preformattedText(text):
currentPreformatted!.text += text
currentPreformatted!.text += "\n"
case let .text(text):
blocks.append(.text(text))
case let .link(url, text: text):
blocks.append(.link(url, text: text))
case let .heading(text, level: level):
blocks.append(.heading(text, level: level))
case let .unorderedListItem(text):
blocks.append(.unorderedListItem(text))
case let .quote(text):
blocks.append(.quote(text))
}
}
return blocks
}
}