80d3c08a93
- Move to global objects for files and validation - Only write changed files - Check images for changes before scaling - Simplify code
62 lines
2.2 KiB
Swift
62 lines
2.2 KiB
Swift
import Foundation
|
|
|
|
struct SiteGenerator {
|
|
|
|
let templates: TemplateFactory
|
|
|
|
init() throws {
|
|
let templatesFolder = files.urlInContentFolder("templates")
|
|
self.templates = try TemplateFactory(templateFolder: templatesFolder)
|
|
}
|
|
|
|
func generate(site: Element) throws {
|
|
try site.languages.forEach { metadata in
|
|
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)
|
|
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,
|
|
language: language,
|
|
backText: backLinkText)
|
|
} else {
|
|
#warning("Determine previous and next pages")
|
|
pageGenerator.generate(
|
|
page: element,
|
|
language: language,
|
|
backText: backLinkText,
|
|
nextPage: nil,
|
|
previousPage: nil)
|
|
}
|
|
}
|
|
|
|
let generator = IndexPageGenerator(factory: template)
|
|
|
|
// Generate front page
|
|
let relativeUrl = site.localizedPath(for: language)
|
|
let indexPageUrl = files.urlInOutputFolder(relativeUrl)
|
|
let button = site.nextLanguage(for: language)
|
|
generator.generate(
|
|
site: site,
|
|
language: language,
|
|
languageButton: button,
|
|
sectionItemCount: 6,
|
|
to: indexPageUrl)
|
|
}
|
|
}
|
|
}
|