2022-08-16 10:39:05 +02:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct SiteGenerator {
|
|
|
|
|
|
|
|
let templates: TemplateFactory
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
init() throws {
|
|
|
|
let templatesFolder = files.urlInContentFolder("templates")
|
2022-08-16 10:39:05 +02:00
|
|
|
self.templates = try TemplateFactory(templateFolder: templatesFolder)
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
func generate(site: Element) throws {
|
2022-08-28 11:15:50 +02:00
|
|
|
site.requiredFiles.forEach(files.require)
|
2022-08-29 13:33:48 +02:00
|
|
|
site.externalFiles.forEach(files.exclude)
|
2022-08-30 11:30:23 +02:00
|
|
|
try site.languages.forEach {
|
|
|
|
try generate(site: site, metadata: $0)
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-08-30 11:30:23 +02:00
|
|
|
private func generate(site: Element, metadata: Element.LocalizedMetadata) throws {
|
|
|
|
let language = metadata.language
|
|
|
|
let template = try LocalizedSiteTemplate(
|
|
|
|
factory: templates,
|
|
|
|
language: language,
|
|
|
|
site: site)
|
|
|
|
|
|
|
|
// Generate sections
|
|
|
|
let overviewGenerator = OverviewPageGenerator(factory: template)
|
|
|
|
let pageGenerator = PageGenerator(factory: template)
|
2022-09-04 17:45:29 +02:00
|
|
|
|
|
|
|
var elementsToProcess: [Element] = [site]
|
2022-08-30 11:30:23 +02:00
|
|
|
while let element = elementsToProcess.popLast() {
|
|
|
|
// Move recursively down to all pages
|
|
|
|
elementsToProcess.append(contentsOf: element.elements)
|
|
|
|
|
|
|
|
element.requiredFiles.forEach(files.require)
|
|
|
|
element.externalFiles.forEach(files.exclude)
|
|
|
|
|
|
|
|
if !element.elements.isEmpty {
|
|
|
|
overviewGenerator.generate(section: element, language: language)
|
|
|
|
} else {
|
|
|
|
#warning("Determine previous and next pages (with relative links)")
|
|
|
|
pageGenerator.generate(
|
|
|
|
page: element,
|
|
|
|
language: language,
|
|
|
|
nextPage: nil,
|
|
|
|
previousPage: nil)
|
|
|
|
}
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|