CHGenerator/WebsiteGenerator/Generators/OverviewPageGenerator.swift

72 lines
2.8 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
struct OverviewPageGenerator {
private let factory: LocalizedSiteTemplate
let outputFolder: URL
2022-08-16 12:26:45 +02:00
init(factory: LocalizedSiteTemplate, files: FileProcessor) {
2022-08-16 10:39:05 +02:00
self.factory = factory
2022-08-16 12:26:45 +02:00
self.outputFolder = files.outputFolder
2022-08-16 10:39:05 +02:00
}
func generate(
section: Section,
language: String,
backText: String?) throws {
let url = outputFolder.appendingPathComponent(section.localizedPath(for: language))
let metadata = section.localized(for: language)
2022-08-16 12:27:13 +02:00
var content = [PageTemplate.Key : String]()
2022-08-16 10:39:05 +02:00
content[.head] = try makeHead(section: section, language: language)
let languageButton = section.nextLanguage(for: language)
content[.topBar] = factory.topBar.generate(
section: section.sectionId,
languageButton: languageButton)
2022-08-16 12:27:13 +02:00
content[.contentClass] = "overview"
content[.header] = makeHeader(metadata: metadata, language: language, backText: backText)
content[.content] = try makeContent(section: section, language: language)
2022-08-16 10:39:05 +02:00
content[.footer] = SiteGenerator.pageFooter
2022-08-16 12:27:13 +02:00
try factory.page.generate(content, to: url)
2022-08-16 10:39:05 +02:00
}
private func makeContent(section: Section, language: String) throws -> String {
if section.hasNestingElements {
let sections = section.elements.compactMap { $0 as? Section }
return try factory.overviewSection.generate(
sections: sections,
in: section,
language: language,
sectionItemCount: section.metadata.sectionOverviewItemCount)
} else {
return try factory.overviewSection.generate(section: section, language: language)
}
}
private func makeHead(section: Section, language: String) throws -> String {
let localized = section.localized(for: language)
let image = section.linkPreviewImage(for: language)
let info = PageHeadInfo(
author: factory.author,
linkPreviewTitle: localized.title,
linkPreviewDescription: localized.linkPreviewDescription,
linkPreviewImage: image,
customHeadContent: try section.customHeadContent())
return try factory.pageHead.generate(page: info)
}
2022-08-16 12:27:13 +02:00
private func makeHeader(metadata: Section.LocalizedMetadata,
language: String,
backText: String?) -> String {
var content = [HeaderKey : String]()
content[.title] = metadata.title
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
}