Consolidate HTML element functions
This commit is contained in:
parent
6226f1a1c6
commit
ab2bc663ec
@ -9,4 +9,29 @@ struct HTMLElementsGenerator {
|
|||||||
func make(title: String, suffix: String) -> String {
|
func make(title: String, suffix: String) -> String {
|
||||||
"\(title)<span class=\"suffix\">\(suffix)</span>"
|
"\(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>"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,9 +39,9 @@ struct PageGenerator {
|
|||||||
}
|
}
|
||||||
let pageContent = makeContent(page: page, language: language, path: inputContentPath)
|
let pageContent = makeContent(page: page, language: language, path: inputContentPath)
|
||||||
content[.content] = pageContent ?? factory.placeholder
|
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[.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[.nextPageUrl] = nextPage?.link
|
||||||
content[.footer] = page.customFooterContent()
|
content[.footer] = page.customFooterContent()
|
||||||
|
|
||||||
|
@ -4,10 +4,10 @@ struct PageHeadGenerator {
|
|||||||
|
|
||||||
static let linkPreviewDesiredImageWidth = 1600
|
static let linkPreviewDesiredImageWidth = 1600
|
||||||
|
|
||||||
let template: PageHeadTemplate
|
let factory: TemplateFactory
|
||||||
|
|
||||||
init(factory: TemplateFactory) {
|
init(factory: TemplateFactory) {
|
||||||
self.template = factory.pageHead
|
self.factory = factory
|
||||||
}
|
}
|
||||||
|
|
||||||
func generate(page: Element, language: String) -> String {
|
func generate(page: Element, language: String) -> String {
|
||||||
@ -28,10 +28,10 @@ struct PageHeadGenerator {
|
|||||||
source: sourceImagePath,
|
source: sourceImagePath,
|
||||||
destination: destinationImagePath,
|
destination: destinationImagePath,
|
||||||
width: PageHeadGenerator.linkPreviewDesiredImageWidth)
|
width: PageHeadGenerator.linkPreviewDesiredImageWidth)
|
||||||
content[.image] = "<meta property=\"og:image\" content=\"\(linkPreviewImageName)\" />"
|
content[.image] = factory.html.linkPreviewImage(file: linkPreviewImageName)
|
||||||
}
|
}
|
||||||
content[.customPageContent] = page.customHeadContent()
|
content[.customPageContent] = page.customHeadContent()
|
||||||
|
|
||||||
return template.generate(content)
|
return factory.pageHead.generate(content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,14 +59,14 @@ struct LocalizedSiteTemplate {
|
|||||||
|
|
||||||
let metadata = site.localized(for: language)
|
let metadata = site.localized(for: language)
|
||||||
|
|
||||||
let sections = site.elements.map {
|
let sections = site.sortedItems.map {
|
||||||
PrefilledTopBarTemplate.SectionInfo(
|
PrefilledTopBarTemplate.SectionInfo(
|
||||||
name: $0.title(for: language),
|
name: $0.title(for: language),
|
||||||
url: "\($0.path)/\(language).html")
|
url: "\($0.path)/\(language).html")
|
||||||
}
|
}
|
||||||
|
|
||||||
self.topBar = try .init(
|
self.topBar = try .init(
|
||||||
template: factory.topBar,
|
factory: factory,
|
||||||
language: language,
|
language: language,
|
||||||
sections: sections,
|
sections: sections,
|
||||||
topBarWebsiteTitle: site.topBarTitle)
|
topBarWebsiteTitle: site.topBarTitle)
|
||||||
@ -90,14 +90,6 @@ struct LocalizedSiteTemplate {
|
|||||||
return backNavigation.generate(content)
|
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 {
|
func makeDateString(start: Date?, end: Date?) -> String {
|
||||||
guard let start = start else {
|
guard let start = start else {
|
||||||
return ""
|
return ""
|
||||||
|
@ -8,10 +8,10 @@ struct PrefilledTopBarTemplate {
|
|||||||
|
|
||||||
let topBarWebsiteTitle: String
|
let topBarWebsiteTitle: String
|
||||||
|
|
||||||
private let topBar: TopBarTemplate
|
private let factory: TemplateFactory
|
||||||
|
|
||||||
init(template: TopBarTemplate, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) throws {
|
init(factory: TemplateFactory, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) throws {
|
||||||
self.topBar = template
|
self.factory = factory
|
||||||
self.language = language
|
self.language = language
|
||||||
self.sections = sections
|
self.sections = sections
|
||||||
self.topBarWebsiteTitle = topBarWebsiteTitle
|
self.topBarWebsiteTitle = topBarWebsiteTitle
|
||||||
@ -20,33 +20,18 @@ struct PrefilledTopBarTemplate {
|
|||||||
func generate(sectionUrl: String?, languageButton: String?) -> String {
|
func generate(sectionUrl: String?, languageButton: String?) -> String {
|
||||||
var content = [TopBarTemplate.Key : String]()
|
var content = [TopBarTemplate.Key : String]()
|
||||||
content[.title] = topBarWebsiteTitle
|
content[.title] = topBarWebsiteTitle
|
||||||
content[.titleLink] = topBarWebsiteTitle(language: language)
|
content[.titleLink] = factory.html.topBarWebsiteTitle(language: language)
|
||||||
content[.elements] = elements(activeSectionUrl: sectionUrl)
|
content[.elements] = elements(activeSectionUrl: sectionUrl)
|
||||||
content[.languageButton] = languageButton.unwrapped(topBarLanguageButton) ?? ""
|
content[.languageButton] = languageButton.unwrapped(factory.html.topBarLanguageButton) ?? ""
|
||||||
return topBar.generate(content)
|
return factory.topBar.generate(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func elements(activeSectionUrl: String?) -> String {
|
private func elements(activeSectionUrl: String?) -> String {
|
||||||
sections
|
sections
|
||||||
.map {
|
.map { factory.html.topBarNavigationLink(url: $0.url, text: $0.name, isActive: activeSectionUrl == $0.url) }
|
||||||
topBarNavigationLink(url: $0.url, text: $0.name, isActive: activeSectionUrl == $0.url)
|
|
||||||
}
|
|
||||||
.joined(separator: "\n")
|
.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 {
|
struct SectionInfo {
|
||||||
|
|
||||||
let name: String
|
let name: String
|
||||||
|
Loading…
Reference in New Issue
Block a user