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(sectionUrl: String?, languageButton: String?) -> String { var content = [TopBarTemplate.Key : String]() content[.title] = topBarWebsiteTitle content[.titleLink] = topBarWebsiteTitle(language: language) content[.elements] = elements(activeSectionUrl: sectionUrl) content[.languageButton] = languageButton.unwrapped(topBarLanguageButton) ?? "" return topBar.generate(content) } private func elements(activeSectionUrl: String?) -> String { sections .map { 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 let url: String } }