import Foundation struct SiteGenerator { let site: Site let templates: TemplateFactory private let files: FileProcessor private var outputFolder: URL { files.outputFolder } init(site: Site, files: FileProcessor) throws { self.site = site let templatesFolder = site.inputFolder.appendingPathComponent("templates") self.templates = try TemplateFactory(templateFolder: templatesFolder) self.files = files } func generate() throws { try site.metadata.languages.forEach { metadata in let language = metadata.languageIdentifier let template = try LocalizedSiteTemplate( factory: templates, language: language, site: site, files: files) // Generate sections let overviewGenerator = OverviewPageGenerator(factory: template, files: files) let pageGenerator = PageGenerator(factory: template, files: files) let backLinkText = try site.backLinkText(for: language) var elementsToProcess: [(element: SiteElement, backText: String?)] = site.elements.map { ($0, backLinkText) } while let (element, backText) = elementsToProcess.popLast() { if let section = element as? Section { try overviewGenerator.generate( section: section, language: language, backText: backText) let elementBackText = try element.backLinkText(for: language) let nestedElements = section.elements.map { ($0, elementBackText) } elementsToProcess.append(contentsOf: nestedElements) } if let page = element as? Page { #warning("Determine previous and next pages") try pageGenerator.generate( page: page, language: language, backText: backText ?? metadata.defaultBackLinkText, nextPage: nil, previousPage: nil) for file in page.metadata.requiredFiles { let relativePath = page.path + "/" + file files.require(file: relativePath) } } } let generator = IndexPageGenerator(factory: template) // Generate front page let relativeUrl = site.localizedPath(for: language) let indexPageUrl = outputFolder.appendingPathComponent(relativeUrl) let button = site.nextLanguage(for: language) try generator.generate( site: site, language: language, languageButton: button, sectionItemCount: 6, to: indexPageUrl) } } }