CHGenerator/Sources/Generator/Templates/Filled/PrefilledTopBarTemplate.swift
2022-09-09 11:18:32 +02:00

42 lines
1.3 KiB
Swift

import Foundation
struct PrefilledTopBarTemplate {
let language: String
let sections: [SectionInfo]
let topBarWebsiteTitle: String
private let factory: TemplateFactory
init(factory: TemplateFactory, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) {
self.factory = factory
self.language = language
self.sections = sections
self.topBarWebsiteTitle = topBarWebsiteTitle
}
func generate(sectionUrl: String?, languageButton: String?, page: Element) -> String {
var content = [TopBarTemplate.Key : String]()
content[.title] = topBarWebsiteTitle
content[.titleLink] = factory.html.topBarWebsiteTitle(language: language, from: page)
content[.elements] = elements(activeSectionUrl: sectionUrl)
content[.languageButton] = languageButton.unwrapped(factory.html.topBarLanguageButton) ?? ""
return factory.topBar.generate(content)
}
private func elements(activeSectionUrl: String?) -> String {
sections
.map { factory.html.topBarNavigationLink(url: $0.url, text: $0.name, isActive: activeSectionUrl == $0.url) }
.joined(separator: "\n")
}
struct SectionInfo {
let name: String
let url: String
}
}