CHGenerator/WebsiteGenerator/Generators/ThumbnailListGenerator.swift
2022-08-28 14:02:13 +02:00

56 lines
2.1 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 fullThumbnailPath = item.thumbnailFilePath(for: language)
let relativeImageUrl = parent.relativePathToFileWithPath(fullThumbnailPath)
let metadata = item.localized(for: language)
var content = [ThumbnailKey : String]()
if item.state.hasThumbnailLink {
#warning("If page in language is missing, link to different language")
let fullPageUrl = item.fullPageUrl(for: language)
let relativePageUrl = parent.relativePathToFileWithPath(fullPageUrl)
content[.url] = "href=\"\(relativePageUrl)\""
}
content[.image] = relativeImageUrl
if style == .large, let suffix = metadata.thumbnailSuffix {
content[.title] = factory.html.make(title: metadata.title, suffix: suffix)
} else {
content[.title] = metadata.title
}
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)
// Create image version for high-resolution screens
files.requireImage(
source: fullThumbnailPath,
destination: fullThumbnailPath.insert("@2x", beforeLast: "."),
width: style.width * 2,
desiredHeight: style.height * 2)
return factory.thumbnail(style: style).generate(content, shouldIndent: false)
}
}