CHGenerator/Sources/Generator/Generators/SiteGenerator.swift

66 lines
2.3 KiB
Swift
Raw Permalink Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
2022-09-25 22:07:34 +02:00
typealias LinkedElement = (previous: Element?, element: Element, next: Element?)
2022-08-16 10:39:05 +02:00
struct SiteGenerator {
let templates: TemplateFactory
2022-12-04 19:15:22 +01:00
let results: GenerationResultsHandler
init(results: GenerationResultsHandler) throws {
self.results = results
let templatesFolder = results.contentFolder.appendingPathComponent("templates")
self.templates = try TemplateFactory(templateFolder: templatesFolder, results: results)
2022-08-16 10:39:05 +02:00
}
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,
2022-12-04 19:15:22 +01:00
site: site,
results: results)
2022-08-30 11:30:23 +02:00
// Generate sections
2022-12-04 19:15:22 +01:00
let overviewGenerator = OverviewPageGenerator(factory: template, results: results)
let pageGenerator = PageGenerator(factory: template, siteRoot: site, results: results)
2022-09-04 17:45:29 +02:00
2022-09-25 22:07:34 +02:00
var elementsToProcess: [LinkedElement] = [(nil, site, nil)]
while let (previous, element, next) = elementsToProcess.popLast() {
2022-08-30 11:30:23 +02:00
// Move recursively down to all pages
2022-09-25 22:07:34 +02:00
elementsToProcess.append(contentsOf: element.linkedElements)
2022-08-30 11:30:23 +02:00
2022-09-08 13:01:32 +02:00
processAllFiles(for: element)
if element.hasVisibleChildren {
2022-08-30 11:30:23 +02:00
overviewGenerator.generate(section: element, language: language)
} else {
pageGenerator.generate(
page: element,
language: language,
2022-09-25 22:07:34 +02:00
previousPage: previous,
nextPage: next)
2022-08-30 11:30:23 +02:00
}
2022-08-16 10:39:05 +02:00
}
}
2022-09-08 13:01:32 +02:00
private func processAllFiles(for element: Element) {
2022-12-04 19:15:22 +01:00
element.externalFiles.forEach { results.exclude(file: $0, source: element.path) }
element.requiredFiles.forEach { results.require(file: $0, source: element.path) }
2022-09-08 13:01:32 +02:00
element.images.forEach {
2022-12-04 19:15:22 +01:00
results.requireSingleImage(
2022-09-08 13:01:32 +02:00
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
}