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,29 +8,28 @@ struct IndexPageGenerator {
self.factory = factory self.factory = factory
} }
func generate( func generate(site: Element, language: String) {
site: Element, let localized = site.localized(for: language)
language: String, let path = site.localizedPath(for: language)
languageButton: String?, let pageUrl = files.urlInOutputFolder(path)
sectionItemCount: Int, let languageButton = site.nextLanguage(for: language)
to url: URL) { let sectionItemCount = site.overviewItemCount
let localized = site.localized(for: language)
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)
content[.topBar] = factory.topBar.generate(sectionUrl: nil, languageButton: languageButton) content[.topBar] = factory.topBar.generate(sectionUrl: nil, languageButton: languageButton)
content[.contentClass] = "overview" content[.contentClass] = "overview"
content[.header] = makeHeader(page: site, metadata: localized, language: language) content[.header] = makeHeader(page: site, metadata: localized, language: language)
content[.content] = factory.overviewSection.generate( content[.content] = factory.overviewSection.generate(
sections: site.sortedItems, sections: site.sortedItems,
in: site, in: site,
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,50 +12,44 @@ 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 {
let language = metadata.language try generate(site: site, metadata: $0)
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)
element.externalFiles.forEach(files.exclude)
if !element.elements.isEmpty {
overviewGenerator.generate(
section: element,
language: language)
} else {
#warning("Determine previous and next pages (with relative links)")
pageGenerator.generate(
page: element,
language: language,
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)
} }
} }
private func generate(site: Element, metadata: Element.LocalizedMetadata) throws {
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)
element.externalFiles.forEach(files.exclude)
if !element.elements.isEmpty {
overviewGenerator.generate(section: element, language: language)
} else {
#warning("Determine previous and next pages (with relative links)")
pageGenerator.generate(
page: element,
language: language,
nextPage: nil,
previousPage: nil)
}
}
let generator = IndexPageGenerator(factory: template)
// Generate front page
generator.generate(site: site, language: language)
}
} }