CHGenerator/Sources/Generator/Generators/OverviewPageGenerator.swift

53 lines
2.1 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
struct OverviewPageGenerator {
private let factory: LocalizedSiteTemplate
2022-12-04 19:15:22 +01:00
private let results: GenerationResultsHandler
init(factory: LocalizedSiteTemplate, results: GenerationResultsHandler) {
2022-08-16 10:39:05 +02:00
self.factory = factory
2022-12-04 19:15:22 +01:00
self.results = results
2022-08-16 10:39:05 +02:00
}
func generate(
section: Element,
2022-08-26 22:30:10 +02:00
language: String) {
let path = section.localizedPath(for: language)
2022-08-16 10:39:05 +02:00
let metadata = section.localized(for: language)
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-09-04 17:47:13 +02:00
languageButton: languageButton,
page: section)
2022-12-20 12:49:21 +01:00
content[.language] = language
2022-08-16 12:27:13 +02:00
content[.contentClass] = "overview"
content[.header] = makeHeader(page: section, metadata: metadata, language: language)
content[.content] = makeContent(section: section, language: language)
2022-12-04 19:15:22 +01:00
content[.footer] = results.getContentOfOptionalFile(at: section.additionalFooterContentPath, source: section.path)
factory.page.generate(content, to: 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(
2022-08-28 11:15:36 +02:00
sections: section.sortedItems,
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(page: Element, metadata: Element.LocalizedMetadata, language: String) -> String {
let content = factory.makeHeaderContent(page: page, metadata: metadata, language: language)
2022-08-16 12:27:13 +02:00
return factory.factory.centeredHeader.generate(content)
}
2022-08-16 10:39:05 +02:00
}