Tusker/Tusker/Screens/Compose/StatusFormat.swift

75 lines
2.0 KiB
Swift

//
// StatusFormat.swift
// Tusker
//
// Created by Shadowfacts on 1/12/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import Foundation
import Pachyderm
enum StatusFormat: CaseIterable {
case italics, bold, strikethrough, code
var insertionResult: FormatInsertionResult? {
switch Preferences.shared.statusContentType {
case .plain:
return nil
case .markdown:
return Markdown.format(self)
case .html:
return HTML.format(self)
}
}
var title: (String, [NSAttributedString.Key: Any]) {
switch self {
case .italics:
return ("I", [.font: UIFont.italicSystemFont(ofSize: 17)])
case .bold:
return ("B", [.font: UIFont.boldSystemFont(ofSize: 17)])
case .strikethrough:
return ("S", [.strikethroughStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)])
case .code:
return ("</>", [.font: UIFont(name: "Menlo", size: 17)!])
}
}
}
typealias FormatInsertionResult = (prefix: String, suffix: String, insertionPoint: Int)
protocol FormatType {
static func format(_ format: StatusFormat) -> FormatInsertionResult
}
extension StatusFormat {
struct Markdown: FormatType {
static var formats: [StatusFormat: String] = [
.italics: "_",
.bold: "**",
.strikethrough: "~~",
.code: "`"
]
static func format(_ format: StatusFormat) -> FormatInsertionResult {
let str = formats[format]!
return (str, str, str.count)
}
}
struct HTML: FormatType {
static var tags: [StatusFormat: String] = [
.italics: "em",
.bold: "strong",
.strikethrough: "del",
.code: "code"
]
static func format(_ format: StatusFormat) -> FormatInsertionResult {
let tag = tags[format]!
return ("<\(tag)>", "</\(tag)>", tag.count + 2)
}
}
}