CHGenerator/Sources/Generator/Generators/PageGenerator.swift

101 lines
4.4 KiB
Swift
Raw Normal View History

2022-08-16 10:39:05 +02:00
import Foundation
import Ink
struct PageGenerator {
private let factory: LocalizedSiteTemplate
2022-12-01 15:25:55 +01:00
private let contentGenerator: PageContentGenerator
2022-12-04 19:15:22 +01:00
private let results: GenerationResultsHandler
init(factory: LocalizedSiteTemplate, siteRoot: Element, results: GenerationResultsHandler) {
2022-08-16 10:39:05 +02:00
self.factory = factory
2022-12-04 19:15:22 +01:00
self.results = results
self.contentGenerator = PageContentGenerator(factory: factory.factory, siteRoot: siteRoot, results: results)
2022-08-16 10:39:05 +02:00
}
2022-09-25 22:07:34 +02:00
func generate(page: Element, language: String, previousPage: Element?, nextPage: Element?) {
2022-08-16 10:39:05 +02:00
guard !page.isExternalPage else {
2022-12-04 19:15:22 +01:00
results.didCompletePage()
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
let metadata = page.localized(for: language)
let nextLanguage = page.nextLanguage(for: language)
2022-12-04 19:15:22 +01:00
let (pageContent, pageIncludesCode) = makeContent(
2022-09-02 23:19:30 +02:00
page: page, metadata: metadata, language: language, path: inputContentPath)
2022-08-16 12:27:13 +02:00
var content = [PageTemplate.Key : String]()
2022-12-20 12:49:21 +01:00
content[.language] = language
2022-08-29 13:35:25 +02:00
content[.head] = factory.pageHead.generate(page: page, language: language, includesCode: pageIncludesCode)
let sectionUrl = page.sectionUrl(for: language)
2022-09-04 17:47:13 +02:00
content[.topBar] = factory.topBar.generate(sectionUrl: sectionUrl, languageButton: nextLanguage, page: page)
2022-08-16 12:27:13 +02:00
content[.contentClass] = "content"
2022-08-29 13:35:25 +02:00
2022-09-04 20:36:43 +02:00
content[.header] = makeHeader(page: page, metadata: metadata, language: language)
2022-09-02 23:19:30 +02:00
content[.content] = pageContent
2022-09-25 22:07:34 +02:00
content[.previousPageLinkText] = previousText(for: previousPage, language: language)
content[.previousPageUrl] = navLink(from: page, to: previousPage, language: language)
content[.nextPageLinkText] = nextText(for: nextPage, language: language)
content[.nextPageUrl] = navLink(from: page, to: nextPage, language: language)
2022-12-04 19:15:22 +01:00
content[.footer] = results.getContentOfOptionalFile(at: page.additionalFooterContentPath, source: page.path)
2022-08-16 10:39:05 +02:00
2022-08-29 13:35:25 +02:00
if pageIncludesCode {
let highlightCode = factory.factory.html.codeHighlightFooter()
content[.footer] = (content[.footer].unwrapped { $0 + "\n" } ?? "") + highlightCode
}
2022-08-31 09:02:40 +02:00
if page.state == .draft {
2022-12-04 19:15:22 +01:00
results.markPageAsDraft(page: page.path)
2022-08-16 10:39:05 +02:00
}
2022-12-04 19:15:22 +01:00
factory.page.generate(content, to: path, source: page.path)
2022-08-16 10:39:05 +02:00
}
2022-09-25 22:07:34 +02:00
private func navLink(from element: Element, to destination: Element?, language: String) -> String? {
guard let fullPath = destination?.fullPageUrl(for: language) else {
return nil
}
return element.relativePathToOtherSiteElement(file: fullPath)
}
private func previousText(for element: Element?, language: String) -> String? {
guard let text = element?.localized(for: language).navigationTextAsPreviousPage else {
return nil
}
return factory.factory.html.makePrevText(text)
}
private func nextText(for element: Element?, language: String) -> String? {
guard let text = element?.localized(for: language).navigationTextAsNextPage else {
return nil
}
return factory.factory.html.makeNextText(text)
}
2022-12-04 19:15:22 +01:00
private func makeContent(page: Element, metadata: Element.LocalizedMetadata, language: String, path: String) -> (content: String, includesCode: Bool) {
if let raw = results.getContentOfMdFile(at: path, source: page.path)?.trimmed.nonEmpty {
2022-12-01 15:25:55 +01:00
let (content, includesCode) = contentGenerator.generate(page: page, language: language, content: raw)
2022-12-04 19:15:22 +01:00
return (content, includesCode)
2022-09-02 23:19:30 +02:00
} else {
2022-12-01 15:25:55 +01:00
let (content, includesCode) = contentGenerator.generate(page: page, language: language, content: metadata.placeholderText)
2022-09-18 17:21:57 +02:00
let placeholder = factory.factory.makePlaceholder(title: metadata.placeholderTitle, text: content)
2022-12-04 19:15:22 +01:00
return (placeholder, includesCode)
}
2022-08-16 10:39:05 +02:00
}
2022-08-16 12:27:13 +02:00
2022-09-04 20:36:43 +02:00
private func makeHeader(page: Element, metadata: Element.LocalizedMetadata, language: String) -> String? {
let content = factory.makeHeaderContent(page: page, metadata: metadata, language: language)
2022-09-04 20:36:43 +02:00
switch page.headerType {
case .none:
return nil
case .left:
return factory.factory.leftHeader.generate(content)
case .center:
return factory.factory.centeredHeader.generate(content)
}
2022-08-16 12:27:13 +02:00
}
2022-08-16 10:39:05 +02:00
}