CHGenerator/WebsiteGenerator/Templates/Filled/PrefilledTopBarTemplate.swift
Christoph Hagen 80d3c08a93 Update generation
- Move to global objects for files and validation
- Only write changed files
- Check images for changes before scaling
- Simplify code
2022-08-26 17:40:51 +02:00

57 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(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 {
"<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 name: String
let url: String
}
}