CHGenerator/WebsiteGenerator/Generators/OverviewPageGenerator.swift

60 lines
2.3 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
struct OverviewPageGenerator {
private let factory: LocalizedSiteTemplate
init(factory: LocalizedSiteTemplate) {
2022-08-16 10:39:05 +02:00
self.factory = factory
}
func generate(
section: Element,
2022-08-26 22:30:10 +02:00
language: String) {
let path = section.localizedPath(for: language)
let url = files.urlInOutputFolder(path)
2022-08-16 10:39:05 +02:00
let metadata = section.localized(for: language)
2022-08-26 22:30:10 +02:00
let backLinkText = section.backLinkText(for: language)
2022-08-16 10:39:05 +02:00
2022-08-16 12:27:13 +02:00
var content = [PageTemplate.Key : String]()
content[.head] = factory.pageHead.generate(page: section, language: language)
2022-08-16 10:39:05 +02:00
let languageButton = section.nextLanguage(for: language)
content[.topBar] = factory.topBar.generate(
sectionUrl: section.sectionUrl(for: language),
2022-08-16 10:39:05 +02:00
languageButton: languageButton)
2022-08-16 12:27:13 +02:00
content[.contentClass] = "overview"
2022-08-26 22:30:10 +02:00
content[.header] = makeHeader(metadata: metadata, language: language, backText: backLinkText)
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)
2022-08-16 10:39:05 +02:00
}
private func makeContent(section: Element, language: String) -> String {
2022-08-16 10:39:05 +02:00
if section.hasNestingElements {
return factory.overviewSection.generate(
sections: section.elements,
2022-08-16 10:39:05 +02:00
in: section,
language: language,
sectionItemCount: section.overviewItemCount)
2022-08-16 10:39:05 +02:00
} else {
return factory.overviewSection.generate(section: section, language: language)
2022-08-16 10:39:05 +02:00
}
}
private func makeHeader(metadata: Element.LocalizedMetadata,
2022-08-16 12:27:13 +02:00
language: String,
backText: String?) -> String {
var content = [HeaderKey : String]()
content[.title] = metadata.title
#warning("Add title suffix")
2022-08-16 12:27:13 +02:00
content[.subtitle] = metadata.subtitle
content[.titleText] = metadata.description
content[.backLink] = backText.unwrapped { factory.makeBackLink(text: $0, language: language) }
return factory.factory.centeredHeader.generate(content)
}
2022-08-16 10:39:05 +02:00
}