From ab2bc663ec5e4001b5a56d625dd104723e735f0f Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sun, 28 Aug 2022 14:01:53 +0200 Subject: [PATCH] Consolidate HTML element functions --- .../Generators/HTMLElementsGenerator.swift | 25 ++++++++++++++++ .../Generators/PageGenerator.swift | 4 +-- .../Generators/PageHeadGenerator.swift | 8 ++--- .../Filled/LocalizedSiteTemplate.swift | 12 ++------ .../Filled/PrefilledTopBarTemplate.swift | 29 +++++-------------- 5 files changed, 40 insertions(+), 38 deletions(-) diff --git a/WebsiteGenerator/Generators/HTMLElementsGenerator.swift b/WebsiteGenerator/Generators/HTMLElementsGenerator.swift index 7be5f3d..8e97604 100644 --- a/WebsiteGenerator/Generators/HTMLElementsGenerator.swift +++ b/WebsiteGenerator/Generators/HTMLElementsGenerator.swift @@ -9,4 +9,29 @@ struct HTMLElementsGenerator { func make(title: String, suffix: String) -> String { "\(title)\(suffix)" } + + // - TODO: Make link relative + func topBarWebsiteTitle(language: String) -> String { + "/\(language).html" + } + + func topBarLanguageButton(_ language: String) -> String { + "\(language)" + } + + func topBarNavigationLink(url: String, text: String, isActive: Bool) -> String { + "\(text)" + } + + func linkPreviewImage(file: String) -> String { + "" + } + + func makePrevText(_ text: String) -> String { + "\(text)" + } + + func makeNextText(_ text: String) -> String { + "\(text)" + } } diff --git a/WebsiteGenerator/Generators/PageGenerator.swift b/WebsiteGenerator/Generators/PageGenerator.swift index aa7cae3..f213bb4 100644 --- a/WebsiteGenerator/Generators/PageGenerator.swift +++ b/WebsiteGenerator/Generators/PageGenerator.swift @@ -39,9 +39,9 @@ struct PageGenerator { } let pageContent = makeContent(page: page, language: language, path: inputContentPath) content[.content] = pageContent ?? factory.placeholder - content[.previousPageLinkText] = previousPage.unwrapped { factory.makePrevText($0.text) } + content[.previousPageLinkText] = previousPage.unwrapped { factory.factory.html.makePrevText($0.text) } content[.previousPageUrl] = previousPage?.link - content[.nextPageLinkText] = nextPage.unwrapped { factory.makeNextText($0.text) } + content[.nextPageLinkText] = nextPage.unwrapped { factory.factory.html.makeNextText($0.text) } content[.nextPageUrl] = nextPage?.link content[.footer] = page.customFooterContent() diff --git a/WebsiteGenerator/Generators/PageHeadGenerator.swift b/WebsiteGenerator/Generators/PageHeadGenerator.swift index e9956d9..b1662a9 100644 --- a/WebsiteGenerator/Generators/PageHeadGenerator.swift +++ b/WebsiteGenerator/Generators/PageHeadGenerator.swift @@ -4,10 +4,10 @@ struct PageHeadGenerator { static let linkPreviewDesiredImageWidth = 1600 - let template: PageHeadTemplate + let factory: TemplateFactory init(factory: TemplateFactory) { - self.template = factory.pageHead + self.factory = factory } func generate(page: Element, language: String) -> String { @@ -28,10 +28,10 @@ struct PageHeadGenerator { source: sourceImagePath, destination: destinationImagePath, width: PageHeadGenerator.linkPreviewDesiredImageWidth) - content[.image] = "" + content[.image] = factory.html.linkPreviewImage(file: linkPreviewImageName) } content[.customPageContent] = page.customHeadContent() - return template.generate(content) + return factory.pageHead.generate(content) } } diff --git a/WebsiteGenerator/Templates/Filled/LocalizedSiteTemplate.swift b/WebsiteGenerator/Templates/Filled/LocalizedSiteTemplate.swift index 431079c..7478363 100644 --- a/WebsiteGenerator/Templates/Filled/LocalizedSiteTemplate.swift +++ b/WebsiteGenerator/Templates/Filled/LocalizedSiteTemplate.swift @@ -59,14 +59,14 @@ struct LocalizedSiteTemplate { let metadata = site.localized(for: language) - let sections = site.elements.map { + let sections = site.sortedItems.map { PrefilledTopBarTemplate.SectionInfo( name: $0.title(for: language), url: "\($0.path)/\(language).html") } self.topBar = try .init( - template: factory.topBar, + factory: factory, language: language, sections: sections, topBarWebsiteTitle: site.topBarTitle) @@ -90,14 +90,6 @@ struct LocalizedSiteTemplate { return backNavigation.generate(content) } - func makePrevText(_ text: String) -> String { - "\(text)" - } - - func makeNextText(_ text: String) -> String { - "\(text)" - } - func makeDateString(start: Date?, end: Date?) -> String { guard let start = start else { return "" diff --git a/WebsiteGenerator/Templates/Filled/PrefilledTopBarTemplate.swift b/WebsiteGenerator/Templates/Filled/PrefilledTopBarTemplate.swift index 8cc2b4a..b487b24 100644 --- a/WebsiteGenerator/Templates/Filled/PrefilledTopBarTemplate.swift +++ b/WebsiteGenerator/Templates/Filled/PrefilledTopBarTemplate.swift @@ -8,10 +8,10 @@ struct PrefilledTopBarTemplate { let topBarWebsiteTitle: String - private let topBar: TopBarTemplate + private let factory: TemplateFactory - init(template: TopBarTemplate, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) throws { - self.topBar = template + init(factory: TemplateFactory, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) throws { + self.factory = factory self.language = language self.sections = sections self.topBarWebsiteTitle = topBarWebsiteTitle @@ -20,33 +20,18 @@ struct PrefilledTopBarTemplate { func generate(sectionUrl: String?, languageButton: String?) -> String { var content = [TopBarTemplate.Key : String]() content[.title] = topBarWebsiteTitle - content[.titleLink] = topBarWebsiteTitle(language: language) + content[.titleLink] = factory.html.topBarWebsiteTitle(language: language) content[.elements] = elements(activeSectionUrl: sectionUrl) - content[.languageButton] = languageButton.unwrapped(topBarLanguageButton) ?? "" - return topBar.generate(content) + content[.languageButton] = languageButton.unwrapped(factory.html.topBarLanguageButton) ?? "" + return factory.topBar.generate(content) } private func elements(activeSectionUrl: String?) -> String { sections - .map { - topBarNavigationLink(url: $0.url, text: $0.name, isActive: activeSectionUrl == $0.url) - } + .map { factory.html.topBarNavigationLink(url: $0.url, text: $0.name, isActive: activeSectionUrl == $0.url) } .joined(separator: "\n") } - #warning("Move HTML code to single location") - private func topBarWebsiteTitle(language: String) -> String { - "/\(language).html" - } - - private func topBarLanguageButton(_ language: String) -> String { - "\(language)" - } - - private func topBarNavigationLink(url: String, text: String, isActive: Bool) -> String { - "\(text)" - } - struct SectionInfo { let name: String