CHGenerator/WebsiteGenerator/Generators/PageHeadGenerator.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

38 lines
1.5 KiB
Swift

import Foundation
struct PageHeadGenerator {
static let linkPreviewDesiredImageWidth = 1600
let template: PageHeadTemplate
init(factory: TemplateFactory) {
self.template = factory.pageHead
}
func generate(page: Element, language: String) -> String {
let metadata = page.localized(for: language)
var content = [PageHeadTemplate.Key : String]()
content[.author] = page.author
content[.title] = metadata.linkPreviewTitle
content[.description] = metadata.linkPreviewDescription
if let image = page.linkPreviewImage(for: language) {
// Note: Generate separate destination link for the image,
// since we don't want a single large image for thumbnails.
// Warning: Link preview source path must be relative to root
let linkPreviewImageName = image.insert("-link", beforeLast: ".")
let sourceImagePath = page.pathRelativeToRootForContainedInputFile(image)
let destinationImagePath = page.pathRelativeToRootForContainedInputFile(linkPreviewImageName)
files.requireImage(
source: sourceImagePath,
destination: destinationImagePath,
width: PageHeadGenerator.linkPreviewDesiredImageWidth)
content[.image] = "<meta property=\"og:image\" content=\"\(linkPreviewImageName)\" />"
}
content[.customPageContent] = page.customHeadContent()
return template.generate(content)
}
}