CHGenerator/WebsiteGenerator/Generators/ThumbnailListGenerator.swift
Christoph Hagen 80d3c08a93 Update generation
- Move to global objects for files and validation
- Only write changed files
- Check images for changes before scaling
- Simplify code
2022-08-26 17:40:51 +02:00

41 lines
1.6 KiB
Swift

import Foundation
struct ThumbnailListGenerator {
private let factory: TemplateFactory
init(factory: TemplateFactory) {
self.factory = factory
}
func generateContent(items: [Element], parent: Element, language: String, style: ThumbnailStyle) -> String {
items.map { itemContent($0, parent: parent, language: language, style: style) }
.joined(separator: "\n")
}
private func itemContent(_ item: Element, parent: Element, language: String, style: ThumbnailStyle) -> String {
let fullPageUrl = item.fullPageUrl(for: language)
let relativePageUrl = parent.relativePathToFileWithPath(fullPageUrl)
let fullThumbnailPath = item.thumbnailFilePath(for: language)
let relativeImageUrl = parent.relativePathToFileWithPath(fullThumbnailPath)
var content = [ThumbnailKey : String]()
content[.url] = "href=\"\(relativePageUrl)\""
content[.image] = relativeImageUrl
content[.title] = item.title(for: language)
#warning("Generate thumbnail suffix")
content[.image2x] = relativeImageUrl.insert("@2x", beforeLast: ".")
content[.corner] = item.cornerText(for: language).unwrapped {
factory.largeThumbnail.makeCorner(text: $0)
}
files.requireImage(
source: fullThumbnailPath,
destination: fullThumbnailPath,
width: style.width,
desiredHeight: style.height,
createDoubleVersion: true)
return factory.thumbnail(style: style).generate(content, shouldIndent: false)
}
}