81 lines
3.0 KiB
Swift
81 lines
3.0 KiB
Swift
|
import Foundation
|
||
|
|
||
|
struct SiteGenerator {
|
||
|
|
||
|
let site: Site
|
||
|
|
||
|
let templates: TemplateFactory
|
||
|
|
||
|
private let imageProcessor: ImageProcessor
|
||
|
|
||
|
private var outputFolder: URL {
|
||
|
imageProcessor.outputFolder
|
||
|
}
|
||
|
|
||
|
init(site: Site, imageProcessor: ImageProcessor) throws {
|
||
|
self.site = site
|
||
|
let templatesFolder = site.inputFolder.appendingPathComponent("templates")
|
||
|
self.templates = try TemplateFactory(templateFolder: templatesFolder)
|
||
|
self.imageProcessor = imageProcessor
|
||
|
}
|
||
|
|
||
|
func generate() throws {
|
||
|
try site.metadata.languages.forEach { metadata in
|
||
|
let language = metadata.languageIdentifier
|
||
|
let template = try LocalizedSiteTemplate(
|
||
|
factory: templates,
|
||
|
language: language,
|
||
|
site: site,
|
||
|
imageProcessor: imageProcessor)
|
||
|
|
||
|
|
||
|
// Generate sections
|
||
|
let overviewGenerator = OverviewPageGenerator(factory: template, imageProcessor: imageProcessor)
|
||
|
let pageGenerator = PageGenerator(factory: template, imageProcessor: imageProcessor)
|
||
|
let backLinkText = try site.backLinkText(for: language)
|
||
|
var elementsToProcess: [(element: SiteElement, backText: String?)] = site.elements.map { ($0, backLinkText) }
|
||
|
while let (element, backText) = elementsToProcess.popLast() {
|
||
|
if let section = element as? Section {
|
||
|
try overviewGenerator.generate(
|
||
|
section: section,
|
||
|
language: language,
|
||
|
backText: backText)
|
||
|
let elementBackText = try element.backLinkText(for: language)
|
||
|
let nestedElements = section.elements.map { ($0, elementBackText) }
|
||
|
elementsToProcess.append(contentsOf: nestedElements)
|
||
|
}
|
||
|
if let page = element as? Page {
|
||
|
#warning("Determine previous and next pages")
|
||
|
try pageGenerator.generate(
|
||
|
page: page,
|
||
|
language: language,
|
||
|
backText: backText ?? metadata.defaultBackLinkText,
|
||
|
nextPage: nil,
|
||
|
previousPage: nil)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let generator = IndexPageGenerator(
|
||
|
factory: template,
|
||
|
imageProcessor: imageProcessor)
|
||
|
|
||
|
// Generate front page
|
||
|
let relativeUrl = site.localizedPath(for: language)
|
||
|
let indexPageUrl = outputFolder.appendingPathComponent(relativeUrl)
|
||
|
let button = site.nextLanguage(for: language)
|
||
|
try generator.generate(
|
||
|
site: site,
|
||
|
language: language,
|
||
|
languageButton: button,
|
||
|
sectionItemCount: 6,
|
||
|
to: indexPageUrl)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static let pageFooter: String =
|
||
|
"""
|
||
|
<script src="/assets/js/jquery.js"></script>
|
||
|
<script src="/assets/js/global.min.js"></script>
|
||
|
"""
|
||
|
}
|