CHGenerator/WebsiteGenerator/Generators/PageGenerator.swift
2022-08-28 14:01:53 +02:00

69 lines
2.6 KiB
Swift

import Foundation
import Ink
struct PageGenerator {
struct NavigationLink {
let link: String
let text: String
}
private let factory: LocalizedSiteTemplate
init(factory: LocalizedSiteTemplate) {
self.factory = factory
}
func generate(page: Element, language: String, nextPage: NavigationLink?, previousPage: NavigationLink?) {
guard !page.isExternalPage else {
return
}
guard page.state != .draft else {
return
}
let path = page.fullPageUrl(for: language)
let inputContentPath = page.path + "/\(language).md"
#warning("Make prev and next navigation relative")
let metadata = page.localized(for: language)
let nextLanguage = page.nextLanguage(for: language)
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)
content[.contentClass] = "content"
if !page.useCustomHeader {
content[.header] = makeHeader(page: page, metadata: metadata, language: language)
}
let pageContent = makeContent(page: page, language: language, path: inputContentPath)
content[.content] = pageContent ?? factory.placeholder
content[.previousPageLinkText] = previousPage.unwrapped { factory.factory.html.makePrevText($0.text) }
content[.previousPageUrl] = previousPage?.link
content[.nextPageLinkText] = nextPage.unwrapped { factory.factory.html.makeNextText($0.text) }
content[.nextPageUrl] = nextPage?.link
content[.footer] = page.customFooterContent()
let url = files.urlInOutputFolder(path)
guard factory.page.generate(content, to: url) else {
return
}
log.add(info: "Generated \(pageContent == nil ? "empty page " : "")\(path)", source: page.path)
}
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)
}
private func makeHeader(page: Element, metadata: Element.LocalizedMetadata, language: String) -> String {
let content = factory.makeHeaderContent(page: page, metadata: metadata, language: language)
return factory.factory.leftHeader.generate(content)
}
}