CHGenerator/WebsiteGenerator/Generators/ThumbnailListGenerator.swift
2022-08-16 12:26:45 +02:00

39 lines
1.3 KiB
Swift

import Foundation
struct ThumbnailListGenerator {
private let factory: TemplateFactory
let files: FileProcessor
init(factory: TemplateFactory, files: FileProcessor) {
self.factory = factory
self.files = files
}
func generateContent(items: [ThumbnailInfo], style: ThumbnailStyle) throws -> String {
try items.map { try itemContent($0, style: style) }
.joined(separator: "\n")
}
private func itemContent(_ thumbnail: ThumbnailInfo, style: ThumbnailStyle) throws -> String {
var content = [ThumbnailKey : String]()
content[.url] = thumbnail.url.unwrapped { "href=\"\($0)\"" }
content[.image] = thumbnail.imageHtmlUrl
content[.title] = thumbnail.title
content[.image2x] = thumbnail.imageHtmlUrl.insert("@2x", beforeLast: ".")
content[.corner] = thumbnail.cornerText.unwrapped {
factory.largeThumbnail.makeCorner(text: $0)
}
try files.requireImage(
source: thumbnail.imageFilePath,
destination: thumbnail.imageFilePath,
width: style.width,
desiredHeight: style.height,
createDoubleVersion: true)
return try factory.thumbnail(style: style).generate(content, shouldIndent: false)
}
}