import Foundation struct OverviewPageGenerator { private let factory: LocalizedSiteTemplate let outputFolder: URL init(factory: LocalizedSiteTemplate, files: FileProcessor) { self.factory = factory self.outputFolder = files.outputFolder } func generate( section: Section, language: String, backText: String?) throws { let url = outputFolder.appendingPathComponent(section.localizedPath(for: language)) let metadata = section.localized(for: language) var content = [PageTemplate.Key : String]() content[.head] = try makeHead(section: section, language: language) let languageButton = section.nextLanguage(for: language) content[.topBar] = factory.topBar.generate( section: section.sectionId, languageButton: languageButton) content[.contentClass] = "overview" content[.header] = makeHeader(metadata: metadata, language: language, backText: backText) content[.content] = try makeContent(section: section, language: language) content[.footer] = try section.customFooterContent() try factory.page.generate(content, to: url) } 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) } 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) } }