CHGenerator/WebsiteGenerator/Generators/IndexPageGenerator.swift

61 lines
2.0 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
private struct AboveRootDummy: SiteElement {
var sortIndex: Int? { nil }
var sortDate: Date? { nil }
var path: String { "" }
let inputFolder: URL
func title(for language: String) -> String { "" }
func cornerText(for language: String) -> String? { nil }
var elements: [SiteElement] { [] }
}
struct IndexPageGenerator {
private let factory: LocalizedSiteTemplate
init(factory: LocalizedSiteTemplate, imageProcessor: ImageProcessor) {
self.factory = factory
}
func generate(
site: Site,
language: String,
languageButton: String?,
sectionItemCount: Int,
to url: URL) throws {
let localized = site.localized(for: language)
var content = [OverviewPageTemplate.Key : String]()
content[.head] = try makeHead(site: site, language: language)
content[.topBar] = factory.topBar.generate(section: nil, languageButton: languageButton)
content[.title] = localized.title
content[.subtitle] = localized.subtitle
content[.titleText] = localized.description
let sections = site.elements.compactMap { $0 as? Section }
content[.sections] = try factory.overviewSection.generate(
sections: sections,
in: site,
language: language,
sectionItemCount: sectionItemCount)
content[.footer] = SiteGenerator.pageFooter
try factory.overviewPage.generate(content, to: url)
}
private func makeHead(site: Site, language: String) throws -> String {
let localized = site.localized(for: language)
return try factory.pageHead.generate(page: PageHeadInfo(
author: site.metadata.author,
linkPreviewTitle: localized.linkPreviewTitle,
linkPreviewDescription: localized.linkPreviewDescription,
linkPreviewImage: site.linkPreviewImage(for: language),
customHeadContent: try site.customHeadContent()))
}
}