Simplify site generation code

This commit is contained in:
Christoph Hagen 2022-08-30 11:30:23 +02:00
parent 107fa2f877
commit db3c25f9db
2 changed files with 59 additions and 66 deletions

View File

@ -8,13 +8,12 @@ struct IndexPageGenerator {
self.factory = factory
}
func generate(
site: Element,
language: String,
languageButton: String?,
sectionItemCount: Int,
to url: URL) {
func generate(site: Element, language: String) {
let localized = site.localized(for: language)
let path = site.localizedPath(for: language)
let pageUrl = files.urlInOutputFolder(path)
let languageButton = site.nextLanguage(for: language)
let sectionItemCount = site.overviewItemCount
var content = [PageTemplate.Key : String]()
content[.head] = factory.pageHead.generate(page: site, language: language)
@ -27,10 +26,10 @@ struct IndexPageGenerator {
language: language,
sectionItemCount: sectionItemCount)
content[.footer] = site.customFooterContent()
guard factory.page.generate(content, to: url) else {
guard factory.page.generate(content, to: pageUrl) else {
return
}
log.add(info: "Generated \(url.lastPathComponent)", source: site.path)
log.add(info: "Page generated", source: path)
}
private func makeHeader(page: Element, metadata: Element.LocalizedMetadata, language: String) -> String {

View File

@ -12,7 +12,12 @@ struct SiteGenerator {
func generate(site: Element) throws {
site.requiredFiles.forEach(files.require)
site.externalFiles.forEach(files.exclude)
try site.languages.forEach { metadata in
try site.languages.forEach {
try generate(site: site, metadata: $0)
}
}
private func generate(site: Element, metadata: Element.LocalizedMetadata) throws {
let language = metadata.language
let template = try LocalizedSiteTemplate(
factory: templates,
@ -31,9 +36,7 @@ struct SiteGenerator {
element.externalFiles.forEach(files.exclude)
if !element.elements.isEmpty {
overviewGenerator.generate(
section: element,
language: language)
overviewGenerator.generate(section: element, language: language)
} else {
#warning("Determine previous and next pages (with relative links)")
pageGenerator.generate(
@ -47,15 +50,6 @@ struct SiteGenerator {
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)
}
generator.generate(site: site, language: language)
}
}