CHGenerator/WebsiteGenerator/Templates/Filled/PrefilledTopBarTemplate.swift
Christoph Hagen 14b935249f First version
2022-08-16 10:39:05 +02:00

59 lines
1.7 KiB
Swift

import Foundation
struct PrefilledTopBarTemplate {
let language: String
let sections: [SectionInfo]
let topBarWebsiteTitle: String
private let topBar: TopBarTemplate
init(template: TopBarTemplate, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) throws {
self.topBar = template
self.language = language
self.sections = sections
self.topBarWebsiteTitle = topBarWebsiteTitle
}
func generate(section: String?, languageButton: String?) -> String {
var content = [TopBarTemplate.Key : String]()
content[.title] = topBarWebsiteTitle
content[.titleLink] = topBarWebsiteTitle(language: language)
content[.elements] = elements(activeSection: section)
content[.languageButton] = languageButton.unwrapped(topBarLanguageButton) ?? ""
return topBar.generate(content)
}
private func elements(activeSection: String?) -> String {
sections
.map {
topBarNavigationLink(url: $0.url, text: $0.name, isActive: activeSection == $0.id)
}
.joined(separator: "\n")
}
#warning("Move HTML code to single location")
private func topBarWebsiteTitle(language: String) -> String {
"/\(language).html"
}
private func topBarLanguageButton(_ language: String) -> String {
"<a href=\"\(language).html\">\(language)</a>"
}
private func topBarNavigationLink(url: String, text: String, isActive: Bool) -> String {
"<a\(isActive ? " class=\"active\"" : "") href=\"/\(url)\">\(text)</a>"
}
struct SectionInfo {
let id: String
let name: String
let url: String
}
}