import Foundation struct OverviewPageGenerator { private let factory: LocalizedSiteTemplate let outputFolder: URL init(factory: LocalizedSiteTemplate, imageProcessor: ImageProcessor) { self.factory = factory self.outputFolder = imageProcessor.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 = [OverviewPageTemplate.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[.sections] = try makeContent(section: section, language: language) content[.title] = metadata.title content[.subtitle] = metadata.subtitle content[.titleText] = metadata.description content[.footer] = SiteGenerator.pageFooter content[.backLink] = backText.unwrapped { factory.makeBackLink(text: $0, language: language) } try factory.overviewPage.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) } }