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
}
func generate(
site: Element,
language: String,
languageButton: String?,
sectionItemCount: Int,
to url: URL) {
let localized = site.localized(for: language)
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)
content[.topBar] = factory.topBar.generate(sectionUrl: nil, languageButton: languageButton)
content[.contentClass] = "overview"
content[.header] = makeHeader(page: site, metadata: localized, language: language)
content[.content] = factory.overviewSection.generate(
sections: site.sortedItems,
in: site,
language: language,
sectionItemCount: sectionItemCount)
content[.footer] = site.customFooterContent()
guard factory.page.generate(content, to: url) else {
return
}
log.add(info: "Generated \(url.lastPathComponent)", source: site.path)
var content = [PageTemplate.Key : String]()
content[.head] = factory.pageHead.generate(page: site, language: language)
content[.topBar] = factory.topBar.generate(sectionUrl: nil, languageButton: languageButton)
content[.contentClass] = "overview"
content[.header] = makeHeader(page: site, metadata: localized, language: language)
content[.content] = factory.overviewSection.generate(
sections: site.sortedItems,
in: site,
language: language,
sectionItemCount: sectionItemCount)
content[.footer] = site.customFooterContent()
guard factory.page.generate(content, to: pageUrl) else {
return
}
log.add(info: "Page generated", source: path)
}
private func makeHeader(page: Element, metadata: Element.LocalizedMetadata, language: String) -> String {

View File

@ -12,50 +12,44 @@ struct SiteGenerator {
func generate(site: Element) throws {
site.requiredFiles.forEach(files.require)
site.externalFiles.forEach(files.exclude)
try site.languages.forEach { metadata in
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
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)
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,
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)
}
}