Consolidate HTML element functions

This commit is contained in:
Christoph Hagen 2022-08-28 14:01:53 +02:00
parent 6226f1a1c6
commit ab2bc663ec
5 changed files with 40 additions and 38 deletions

View File

@ -9,4 +9,29 @@ struct HTMLElementsGenerator {
func make(title: String, suffix: String) -> String {
"\(title)<span class=\"suffix\">\(suffix)</span>"
}
// - TODO: Make link relative
func topBarWebsiteTitle(language: String) -> String {
"/\(language).html"
}
func topBarLanguageButton(_ language: String) -> String {
"<a href=\"\(language).html\">\(language)</a>"
}
func topBarNavigationLink(url: String, text: String, isActive: Bool) -> String {
"<a\(isActive ? " class=\"active\"" : "") href=\"/\(url)\">\(text)</a>"
}
func linkPreviewImage(file: String) -> String {
"<meta property=\"og:image\" content=\"\(file)\" />"
}
func makePrevText(_ text: String) -> String {
"<span class=\"icon-back\"></span>\(text)"
}
func makeNextText(_ text: String) -> String {
"\(text)<span class=\"icon-next\"></span>"
}
}

View File

@ -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()

View File

@ -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] = "<meta property=\"og:image\" content=\"\(linkPreviewImageName)\" />"
content[.image] = factory.html.linkPreviewImage(file: linkPreviewImageName)
}
content[.customPageContent] = page.customHeadContent()
return template.generate(content)
return factory.pageHead.generate(content)
}
}

View File

@ -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 {
"<span class=\"icon-back\"></span>\(text)"
}
func makeNextText(_ text: String) -> String {
"\(text)<span class=\"icon-next\"></span>"
}
func makeDateString(start: Date?, end: Date?) -> String {
guard let start = start else {
return ""

View File

@ -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 {
"<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