From db3c25f9db7946f471a2ada2af869092a0538e5d Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Tue, 30 Aug 2022 11:30:23 +0200 Subject: [PATCH] Simplify site generation code --- .../Generators/IndexPageGenerator.swift | 43 +++++----- .../Generators/SiteGenerator.swift | 82 +++++++++---------- 2 files changed, 59 insertions(+), 66 deletions(-) diff --git a/WebsiteGenerator/Generators/IndexPageGenerator.swift b/WebsiteGenerator/Generators/IndexPageGenerator.swift index 662b311..b5b79f1 100644 --- a/WebsiteGenerator/Generators/IndexPageGenerator.swift +++ b/WebsiteGenerator/Generators/IndexPageGenerator.swift @@ -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 { diff --git a/WebsiteGenerator/Generators/SiteGenerator.swift b/WebsiteGenerator/Generators/SiteGenerator.swift index 7fd4a1c..c542943 100644 --- a/WebsiteGenerator/Generators/SiteGenerator.swift +++ b/WebsiteGenerator/Generators/SiteGenerator.swift @@ -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) + } }