CHGenerator/Sources/Generator/Generators/SiteGenerator.swift

63 lines
2.0 KiB
Swift
Raw 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
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)
2022-12-01 15:25:55 +01:00
let pageGenerator = PageGenerator(factory: template, siteRoot: site)
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)
2022-08-30 11:30:23 +02:00
if !element.elements.isEmpty {
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) {
element.requiredFiles.forEach(files.require)
element.externalFiles.forEach(files.exclude)
element.images.forEach {
2022-11-27 20:31:56 +01:00
files.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
}