CHGenerator/WebsiteGenerator/Generators/SiteGenerator.swift

83 lines
3.1 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
struct SiteGenerator {
let site: Site
let templates: TemplateFactory
2022-08-16 12:26:45 +02:00
private let files: FileProcessor
2022-08-16 10:39:05 +02:00
private var outputFolder: URL {
2022-08-16 12:26:45 +02:00
files.outputFolder
2022-08-16 10:39:05 +02:00
}
2022-08-16 12:26:45 +02:00
init(site: Site, files: FileProcessor) throws {
2022-08-16 10:39:05 +02:00
self.site = site
let templatesFolder = site.inputFolder.appendingPathComponent("templates")
self.templates = try TemplateFactory(templateFolder: templatesFolder)
2022-08-16 12:26:45 +02:00
self.files = files
2022-08-16 10:39:05 +02:00
}
func generate() throws {
try site.metadata.languages.forEach { metadata in
let language = metadata.languageIdentifier
let template = try LocalizedSiteTemplate(
factory: templates,
language: language,
site: site,
2022-08-16 12:26:45 +02:00
files: files)
2022-08-16 10:39:05 +02:00
// Generate sections
2022-08-16 12:26:45 +02:00
let overviewGenerator = OverviewPageGenerator(factory: template, files: files)
let pageGenerator = PageGenerator(factory: template, files: files)
2022-08-16 10:39:05 +02:00
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)
2022-08-16 12:26:45 +02:00
for file in page.metadata.requiredFiles {
let relativePath = page.path + "/" + file
files.require(file: relativePath)
}
2022-08-16 10:39:05 +02:00
}
}
2022-08-16 12:26:45 +02:00
let generator = IndexPageGenerator(factory: template)
2022-08-16 10:39:05 +02:00
// 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>
"""
}