CHGenerator/Sources/Generator/Templates/Elements/PageVideoTemplate.swift

40 lines
1.0 KiB
Swift
Raw Permalink Normal View History

import Foundation
struct PageVideoTemplate: Template {
typealias VideoSource = (url: String, type: VideoType)
enum Key: String, CaseIterable {
case options = "OPTIONS"
case sources = "SOURCES"
}
enum VideoOption: String {
case controls
case autoplay
case muted
case loop
case playsinline
case poster
case preload
}
static let templateName = "video.html"
let raw: String
2022-12-02 10:25:54 +01:00
let results: GenerationResultsHandler
func generate<T>(sources: [VideoSource], options: T) -> String where T: Sequence, T.Element == VideoOption {
let sourcesCode = sources.map(makeSource).joined(separator: "\n")
let optionCode = options.map { $0.rawValue }.joined(separator: " ")
return generate([.sources: sourcesCode, .options: optionCode])
}
private func makeSource(_ source: VideoSource) -> String {
"""
<source src="\(source.url)" type="\(source.type.htmlType)">
"""
}
}