CHGenerator/WebsiteGenerator/Generators/PageGenerator.swift

79 lines
3.1 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
import Ink
struct PageGenerator {
struct NavigationLink {
let link: String
let text: String
}
private let factory: LocalizedSiteTemplate
init(factory: LocalizedSiteTemplate) {
2022-08-16 10:39:05 +02:00
self.factory = factory
}
func generate(page: Element, language: String, backText: String, nextPage: NavigationLink?, previousPage: NavigationLink?) {
2022-08-16 10:39:05 +02:00
guard !page.isExternalPage else {
return
}
guard page.state != .draft else {
2022-08-16 10:39:05 +02:00
return
}
let path = page.fullPageUrl(for: language)
let inputContentPath = page.path + "/\(language).md"
2022-08-16 10:39:05 +02:00
#warning("Make prev and next navigation relative")
let metadata = page.localized(for: language)
let nextLanguage = page.nextLanguage(for: language)
2022-08-16 12:27:13 +02:00
var content = [PageTemplate.Key : String]()
content[.head] = factory.pageHead.generate(page: page, language: language)
let sectionUrl = page.sectionUrl(for: language)
content[.topBar] = factory.topBar.generate(sectionUrl: sectionUrl, languageButton: nextLanguage)
2022-08-16 12:27:13 +02:00
content[.contentClass] = "content"
if !page.useCustomHeader {
content[.header] = makeHeader(page: page, metadata: metadata, language: language, backText: backText)
2022-08-16 12:30:50 +02:00
}
let pageContent = makeContent(page: page, language: language, path: inputContentPath)
content[.content] = pageContent ?? factory.placeholder
2022-08-16 10:39:05 +02:00
content[.previousPageLinkText] = previousPage.unwrapped { factory.makePrevText($0.text) }
content[.previousPageUrl] = previousPage?.link
content[.nextPageLinkText] = nextPage.unwrapped { factory.makeNextText($0.text) }
content[.nextPageUrl] = nextPage?.link
content[.footer] = page.customFooterContent()
2022-08-16 10:39:05 +02:00
let url = files.urlInOutputFolder(path)
guard factory.page.generate(content, to: url) else {
return
2022-08-16 10:39:05 +02:00
}
log.add(info: "Generated \(pageContent == nil ? "empty page " : "")\(path)", source: page.path)
2022-08-16 10:39:05 +02:00
}
private func makeContent(page: Element, language: String, path: String) -> String? {
guard let content = files.contentOfOptionalFile(atPath: path, source: page.path) else {
return nil
}
return PageContentGenerator(factory: factory.factory)
.generate(page: page, language: language, content: content)
2022-08-16 10:39:05 +02:00
}
2022-08-16 12:27:13 +02:00
private func makeHeader(page: Element, metadata: Element.LocalizedMetadata, language: String, backText: String) -> String {
2022-08-16 12:27:13 +02:00
var content = [HeaderKey : String]()
content[.backLink] = factory.makeBackLink(text: backText, language: language)
if let suffix = metadata.titleSuffix {
content[.title] = make(title: metadata.title, suffix: suffix)
} else {
content[.title] = metadata.title
}
2022-08-16 12:27:13 +02:00
content[.subtitle] = metadata.subtitle
content[.date] = factory.makeDateString(start: page.date, end: page.endDate)
return factory.factory.leftHeader.generate(content)
}
private func make(title: String, suffix: String) -> String {
"\(title)<span class=\"suffix\">\(suffix)</span>"
}
2022-08-16 10:39:05 +02:00
}