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 {
|
|
|
|
try site.languages.forEach { metadata in
|
|
|
|
let language = metadata.language
|
2022-08-16 10:39:05 +02:00
|
|
|
let template = try LocalizedSiteTemplate(
|
|
|
|
factory: templates,
|
|
|
|
language: language,
|
2022-08-26 17:40:51 +02:00
|
|
|
site: site)
|
2022-08-16 10:39:05 +02:00
|
|
|
|
|
|
|
// Generate sections
|
2022-08-26 17:40:51 +02:00
|
|
|
let overviewGenerator = OverviewPageGenerator(factory: template)
|
|
|
|
let pageGenerator = PageGenerator(factory: template)
|
|
|
|
var elementsToProcess: [Element] = site.elements
|
|
|
|
while let element = elementsToProcess.popLast() {
|
|
|
|
// Move recursively down to all pages
|
|
|
|
elementsToProcess.append(contentsOf: element.elements)
|
|
|
|
|
|
|
|
element.requiredFiles.forEach(files.require)
|
|
|
|
|
|
|
|
let backLinkText = element.backLinkText(for: language)
|
|
|
|
if !element.elements.isEmpty {
|
|
|
|
overviewGenerator.generate(
|
|
|
|
section: element,
|
2022-08-16 10:39:05 +02:00
|
|
|
language: language,
|
2022-08-26 17:40:51 +02:00
|
|
|
backText: backLinkText)
|
|
|
|
} else {
|
2022-08-16 10:39:05 +02:00
|
|
|
#warning("Determine previous and next pages")
|
2022-08-26 17:40:51 +02:00
|
|
|
pageGenerator.generate(
|
|
|
|
page: element,
|
2022-08-16 10:39:05 +02:00
|
|
|
language: language,
|
2022-08-26 17:40:51 +02:00
|
|
|
backText: backLinkText,
|
2022-08-16 10:39:05 +02:00
|
|
|
nextPage: nil,
|
|
|
|
previousPage: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 12:26:45 +02:00
|
|
|
let generator = IndexPageGenerator(factory: template)
|
2022-08-16 10:39:05 +02:00
|
|
|
|
|
|
|
// Generate front page
|
|
|
|
let relativeUrl = site.localizedPath(for: language)
|
2022-08-26 17:40:51 +02:00
|
|
|
let indexPageUrl = files.urlInOutputFolder(relativeUrl)
|
2022-08-16 10:39:05 +02:00
|
|
|
let button = site.nextLanguage(for: language)
|
2022-08-26 17:40:51 +02:00
|
|
|
generator.generate(
|
2022-08-16 10:39:05 +02:00
|
|
|
site: site,
|
|
|
|
language: language,
|
|
|
|
languageButton: button,
|
|
|
|
sectionItemCount: 6,
|
|
|
|
to: indexPageUrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|