CHGenerator/Sources/Generator/Generators/SiteGenerator.swift

62 lines
1.9 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
struct SiteGenerator {
let templates: TemplateFactory
init() throws {
let templatesFolder = files.urlInContentFolder("templates")
2022-08-16 10:39:05 +02:00
self.templates = try TemplateFactory(templateFolder: templatesFolder)
}
func generate(site: Element) {
site.languages.forEach {
generate(site: site, metadata: $0)
2022-08-30 11:30:23 +02:00
}
}
2022-08-16 10:39:05 +02:00
private func generate(site: Element, metadata: Element.LocalizedMetadata) {
2022-08-30 11:30:23 +02:00
let language = metadata.language
let template = LocalizedSiteTemplate(
2022-08-30 11:30:23 +02:00
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)
2022-09-08 13:01:32 +02:00
processAllFiles(for: element)
2022-08-30 11:30:23 +02:00
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
}
}
2022-09-08 13:01:32 +02:00
private func processAllFiles(for element: Element) {
element.requiredFiles.forEach(files.require)
element.externalFiles.forEach(files.exclude)
element.images.forEach {
files.requireImage(
source: $0.sourcePath,
destination: $0.destinationPath,
2022-09-18 16:47:13 +02:00
requiredBy: element.path,
2022-09-08 13:01:32 +02:00
width: $0.desiredWidth,
desiredHeight: $0.desiredHeight)
}
}
2022-08-16 10:39:05 +02:00
}