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

60 lines
2.3 KiB
Swift

import Foundation
struct OverviewPageGenerator {
private let factory: LocalizedSiteTemplate
init(factory: LocalizedSiteTemplate) {
self.factory = factory
}
func generate(
section: Element,
language: String,
backText: String?) {
let path = section.localizedPath(for: language)
let url = files.urlInOutputFolder(path)
let metadata = section.localized(for: language)
var content = [PageTemplate.Key : String]()
content[.head] = factory.pageHead.generate(page: section, language: language)
let languageButton = section.nextLanguage(for: language)
content[.topBar] = factory.topBar.generate(
sectionUrl: section.sectionUrl(for: language),
languageButton: languageButton)
content[.contentClass] = "overview"
content[.header] = makeHeader(metadata: metadata, language: language, backText: backText)
content[.content] = makeContent(section: section, language: language)
content[.footer] = section.customFooterContent()
guard factory.page.generate(content, to: url) else {
return
}
log.add(info: "Generated \(path)", source: section.path)
}
private func makeContent(section: Element, language: String) -> String {
if section.hasNestingElements {
return factory.overviewSection.generate(
sections: section.elements,
in: section,
language: language,
sectionItemCount: section.overviewItemCount)
} else {
return factory.overviewSection.generate(section: section, language: language)
}
}
private func makeHeader(metadata: Element.LocalizedMetadata,
language: String,
backText: String?) -> String {
var content = [HeaderKey : String]()
content[.title] = metadata.title
#warning("Add title suffix")
content[.subtitle] = metadata.subtitle
content[.titleText] = metadata.description
content[.backLink] = backText.unwrapped { factory.makeBackLink(text: $0, language: language) }
return factory.factory.centeredHeader.generate(content)
}
}