import Foundation struct ThumbnailListGenerator { private let factory: TemplateFactory let imageProcessor: ImageProcessor init(factory: TemplateFactory, imageProcessor: ImageProcessor) { self.factory = factory self.imageProcessor = imageProcessor } 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 imageProcessor.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) } }