2022-08-16 10:39:05 +02:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct ThumbnailListGenerator {
|
|
|
|
|
|
|
|
private let factory: TemplateFactory
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
init(factory: TemplateFactory) {
|
2022-08-16 10:39:05 +02:00
|
|
|
self.factory = factory
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
func generateContent(items: [Element], parent: Element, language: String, style: ThumbnailStyle) -> String {
|
|
|
|
items.map { itemContent($0, parent: parent, language: language, style: style) }
|
2022-08-16 10:39:05 +02:00
|
|
|
.joined(separator: "\n")
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
private func itemContent(_ item: Element, parent: Element, language: String, style: ThumbnailStyle) -> String {
|
|
|
|
let fullThumbnailPath = item.thumbnailFilePath(for: language)
|
|
|
|
let relativeImageUrl = parent.relativePathToFileWithPath(fullThumbnailPath)
|
2022-08-28 11:14:11 +02:00
|
|
|
let metadata = item.localized(for: language)
|
2022-08-16 10:39:05 +02:00
|
|
|
var content = [ThumbnailKey : String]()
|
2022-08-26 22:29:32 +02:00
|
|
|
|
|
|
|
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)\""
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
content[.image] = relativeImageUrl
|
2022-08-28 11:14:11 +02:00
|
|
|
if style == .large, let suffix = metadata.thumbnailSuffix {
|
|
|
|
content[.title] = factory.html.make(title: metadata.title, suffix: suffix)
|
|
|
|
} else {
|
|
|
|
content[.title] = metadata.title
|
|
|
|
}
|
2022-08-26 17:40:51 +02:00
|
|
|
content[.image2x] = relativeImageUrl.insert("@2x", beforeLast: ".")
|
|
|
|
content[.corner] = item.cornerText(for: language).unwrapped {
|
2022-08-16 10:39:05 +02:00
|
|
|
factory.largeThumbnail.makeCorner(text: $0)
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
files.requireImage(
|
|
|
|
source: fullThumbnailPath,
|
|
|
|
destination: fullThumbnailPath,
|
2022-08-16 10:39:05 +02:00
|
|
|
width: style.width,
|
|
|
|
desiredHeight: style.height,
|
|
|
|
createDoubleVersion: true)
|
2022-08-26 17:40:51 +02:00
|
|
|
return factory.thumbnail(style: style).generate(content, shouldIndent: false)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
}
|