Refactor page content generators
This commit is contained in:
@ -1,86 +0,0 @@
|
||||
|
||||
enum ContentBlock: String, CaseIterable {
|
||||
|
||||
case audio
|
||||
|
||||
case swift
|
||||
|
||||
var processor: BlockProcessor.Type {
|
||||
switch self {
|
||||
case .audio: return AudioBlockProcessor.self
|
||||
case .swift: return SwiftBlockProcessor.self
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protocol BlockProcessor {
|
||||
|
||||
static var blockId: ContentBlock { get }
|
||||
|
||||
var results: PageGenerationResults { get }
|
||||
|
||||
init(content: Content, results: PageGenerationResults, language: ContentLanguage)
|
||||
|
||||
func process(_ markdown: Substring) -> String
|
||||
}
|
||||
|
||||
extension BlockProcessor {
|
||||
|
||||
func invalid(_ markdown: Substring) {
|
||||
results.invalid(block: Self.blockId, markdown)
|
||||
}
|
||||
}
|
||||
|
||||
protocol BlockLineProcessor: BlockProcessor {
|
||||
|
||||
func process(_ lines: [String], markdown: Substring) -> String
|
||||
}
|
||||
|
||||
extension BlockLineProcessor {
|
||||
|
||||
func process(_ markdown: Substring) -> String {
|
||||
let lines = markdown
|
||||
.between("```\(Self.blockId.self)", and: "```")
|
||||
.components(separatedBy: "\n")
|
||||
return process(lines, markdown: markdown)
|
||||
}
|
||||
}
|
||||
|
||||
protocol OrderedKeyBlockProcessor: BlockLineProcessor {
|
||||
|
||||
associatedtype Key: Hashable, RawRepresentable where Key.RawValue == String
|
||||
|
||||
func process(_ arguments: [(key: Key, value: String)], markdown: Substring) -> String
|
||||
}
|
||||
|
||||
extension OrderedKeyBlockProcessor {
|
||||
|
||||
func process(_ lines: [String], markdown: Substring) -> String {
|
||||
let result: [(key: Key, value: String)] = lines.compactMap { line in
|
||||
guard line.trimmed != "" else {
|
||||
return nil
|
||||
}
|
||||
let (rawKey, rawValue) = line.splitAtFirst(":")
|
||||
guard let key = Key(rawValue: rawKey.trimmed) else {
|
||||
print("Invalid key \(rawKey)")
|
||||
invalid(markdown)
|
||||
return nil
|
||||
}
|
||||
return (key, rawValue.trimmed)
|
||||
}
|
||||
return process(result, markdown: markdown)
|
||||
}
|
||||
}
|
||||
|
||||
protocol KeyedBlockProcessor: OrderedKeyBlockProcessor {
|
||||
|
||||
func process(_ arguments: [Key : String], markdown: Substring) -> String
|
||||
}
|
||||
|
||||
extension KeyedBlockProcessor {
|
||||
|
||||
func process(_ arguments: [(key: Key, value: String)], markdown: Substring) -> String {
|
||||
let result = arguments.reduce(into: [:]) { $0[$1.key] = $1.value }
|
||||
return process(result, markdown: markdown)
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
|
||||
struct CodeBlockProcessor {
|
||||
|
||||
private let codeHighlightFooter = "<script>hljs.highlightAll();</script>"
|
||||
|
||||
let results: PageGenerationResults
|
||||
|
||||
private let blocks: [ContentBlock : BlockProcessor]
|
||||
|
||||
private let other: OtherCodeProcessor
|
||||
|
||||
init(content: Content, results: PageGenerationResults, language: ContentLanguage) {
|
||||
self.results = results
|
||||
self.other = .init(results: results)
|
||||
|
||||
self.blocks = ContentBlock.allCases.reduce(into: [:]) { blocks, block in
|
||||
blocks[block] = block.processor.init(content: content, results: results, language: language)
|
||||
}
|
||||
}
|
||||
|
||||
func process(_ html: String, markdown: Substring) -> String {
|
||||
let input = String(markdown)
|
||||
let rawBlockId = input.dropAfterFirst("\n").dropBeforeFirst("```").trimmed
|
||||
guard let blockId = ContentBlock(rawValue: rawBlockId) else {
|
||||
return other.process(html: html)
|
||||
}
|
||||
guard let processor = self.blocks[blockId] else {
|
||||
results.invalid(block: blockId, markdown)
|
||||
return ""
|
||||
}
|
||||
return processor.process(markdown)
|
||||
}
|
||||
}
|
14
CHDataManagement/Generator/Blocks/ContentBlock.swift
Normal file
14
CHDataManagement/Generator/Blocks/ContentBlock.swift
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
enum ContentBlock: String, CaseIterable {
|
||||
|
||||
case audio
|
||||
|
||||
case swift
|
||||
|
||||
var processor: BlockProcessor.Type {
|
||||
switch self {
|
||||
case .audio: return AudioBlockProcessor.self
|
||||
case .swift: return SwiftBlockProcessor.self
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
|
||||
protocol BlockLineProcessor: BlockProcessor {
|
||||
|
||||
func process(_ lines: [String], markdown: Substring) -> String
|
||||
}
|
||||
|
||||
extension BlockLineProcessor {
|
||||
|
||||
func process(_ markdown: Substring) -> String {
|
||||
let lines = markdown
|
||||
.between("```\(Self.blockId.self)", and: "```")
|
||||
.components(separatedBy: "\n")
|
||||
return process(lines, markdown: markdown)
|
||||
}
|
||||
}
|
18
CHDataManagement/Generator/Blocks/Types/BlockProcessor.swift
Normal file
18
CHDataManagement/Generator/Blocks/Types/BlockProcessor.swift
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
protocol BlockProcessor {
|
||||
|
||||
static var blockId: ContentBlock { get }
|
||||
|
||||
var results: PageGenerationResults { get }
|
||||
|
||||
init(content: Content, results: PageGenerationResults, language: ContentLanguage)
|
||||
|
||||
func process(_ markdown: Substring) -> String
|
||||
}
|
||||
|
||||
extension BlockProcessor {
|
||||
|
||||
func invalid(_ markdown: Substring) {
|
||||
results.invalid(block: Self.blockId, markdown)
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
|
||||
protocol KeyedBlockProcessor: OrderedKeyBlockProcessor {
|
||||
|
||||
func process(_ arguments: [Key : String], markdown: Substring) -> String
|
||||
}
|
||||
|
||||
extension KeyedBlockProcessor {
|
||||
|
||||
func process(_ arguments: [(key: Key, value: String)], markdown: Substring) -> String {
|
||||
let result = arguments.reduce(into: [:]) { $0[$1.key] = $1.value }
|
||||
return process(result, markdown: markdown)
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
|
||||
protocol OrderedKeyBlockProcessor: BlockLineProcessor {
|
||||
|
||||
associatedtype Key: Hashable, RawRepresentable where Key.RawValue == String
|
||||
|
||||
func process(_ arguments: [(key: Key, value: String)], markdown: Substring) -> String
|
||||
}
|
||||
|
||||
extension OrderedKeyBlockProcessor {
|
||||
|
||||
func process(_ lines: [String], markdown: Substring) -> String {
|
||||
let result: [(key: Key, value: String)] = lines.compactMap { line in
|
||||
guard line.trimmed != "" else {
|
||||
return nil
|
||||
}
|
||||
let (rawKey, rawValue) = line.splitAtFirst(":")
|
||||
guard let key = Key(rawValue: rawKey.trimmed) else {
|
||||
print("Invalid key \(rawKey)")
|
||||
invalid(markdown)
|
||||
return nil
|
||||
}
|
||||
return (key, rawValue.trimmed)
|
||||
}
|
||||
return process(result, markdown: markdown)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user