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 self.factory = factory
} }
func generate( func generate(site: Element, language: String) {
site: Element,
language: String,
languageButton: String?,
sectionItemCount: Int,
to url: URL) {
let localized = site.localized(for: language) 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]() var content = [PageTemplate.Key : String]()
content[.head] = factory.pageHead.generate(page: site, language: language) content[.head] = factory.pageHead.generate(page: site, language: language)
@ -27,10 +26,10 @@ struct IndexPageGenerator {
language: language, language: language,
sectionItemCount: sectionItemCount) sectionItemCount: sectionItemCount)
content[.footer] = site.customFooterContent() content[.footer] = site.customFooterContent()
guard factory.page.generate(content, to: url) else { guard factory.page.generate(content, to: pageUrl) else {
return 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 { private func makeHeader(page: Element, metadata: Element.LocalizedMetadata, language: String) -> String {

View File

@ -12,7 +12,12 @@ struct SiteGenerator {
func generate(site: Element) throws { func generate(site: Element) throws {
site.requiredFiles.forEach(files.require) site.requiredFiles.forEach(files.require)
site.externalFiles.forEach(files.exclude) 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 language = metadata.language
let template = try LocalizedSiteTemplate( let template = try LocalizedSiteTemplate(
factory: templates, factory: templates,
@ -31,9 +36,7 @@ struct SiteGenerator {
element.externalFiles.forEach(files.exclude) element.externalFiles.forEach(files.exclude)
if !element.elements.isEmpty { if !element.elements.isEmpty {
overviewGenerator.generate( overviewGenerator.generate(section: element, language: language)
section: element,
language: language)
} else { } else {
#warning("Determine previous and next pages (with relative links)") #warning("Determine previous and next pages (with relative links)")
pageGenerator.generate( pageGenerator.generate(
@ -47,15 +50,6 @@ struct SiteGenerator {
let generator = IndexPageGenerator(factory: template) let generator = IndexPageGenerator(factory: template)
// Generate front page // Generate front page
let relativeUrl = site.localizedPath(for: language) generator.generate(site: site, language: language)
let indexPageUrl = files.urlInOutputFolder(relativeUrl)
let button = site.nextLanguage(for: language)
generator.generate(
site: site,
language: language,
languageButton: button,
sectionItemCount: 6,
to: indexPageUrl)
}
} }
} }