CHGenerator/WebsiteGenerator/Generators/PageGenerator.swift

82 lines
3.3 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
2022-08-16 12:26:45 +02:00
private let files: FileProcessor
2022-08-16 10:39:05 +02:00
2022-08-16 12:26:45 +02:00
init(factory: LocalizedSiteTemplate, files: FileProcessor) {
2022-08-16 10:39:05 +02:00
self.factory = factory
2022-08-16 12:26:45 +02:00
self.files = files
2022-08-16 10:39:05 +02:00
}
func generate(page: Page, language: String, backText: String, nextPage: NavigationLink?, previousPage: NavigationLink?) throws {
guard !page.isExternalPage else {
return
}
guard !page.metadata.isDraft else {
return
}
let path = page.fullPageUrl(for: language)
let inputContentUrl = page.inputFolder.appendingPathComponent("\(language).md")
#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]()
2022-08-16 10:39:05 +02:00
content[.head] = try makeHead(page: page, language: language)
content[.topBar] = factory.topBar.generate(section: page.sectionId, languageButton: nextLanguage)
2022-08-16 12:27:13 +02:00
content[.contentClass] = "content"
2022-08-16 14:58:57 +02:00
if !page.metadata.useCustomHeader {
2022-08-16 12:30:50 +02:00
content[.header] = makeHeader(page: page.metadata, metadata: metadata, language: language, backText: backText)
}
2022-08-16 10:39:05 +02:00
content[.content] = try makeContent(page: page, language: language, url: inputContentUrl)
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] = try page.customFooterContent()
2022-08-16 12:26:45 +02:00
let url = files.outputFolder.appendingPathComponent(path)
2022-08-16 12:27:13 +02:00
try factory.page.generate(content, to: url)
2022-08-16 10:39:05 +02:00
}
private func makeContent(page: Page, language: String, url: URL) throws -> String {
guard url.exists else {
print("Generated empty page \(page.path)")
return factory.placeholder
}
print("Generated page \(page.path)")
return try PageContentGenerator(factory: factory.factory, files: files)
.generate(page: page, language: language, at: url)
2022-08-16 10:39:05 +02:00
}
private func makeHead(page: Page, language: String) throws -> String {
let metadata = page.localized(for: language)
let info = PageHeadInfo(
author: page.metadata.author ?? factory.author,
linkPreviewTitle: metadata.linkPreviewTitle,
linkPreviewDescription: metadata.linkPreviewDescription,
linkPreviewImage: page.linkPreviewImage(for: language),
customHeadContent: try page.customHeadContent())
return try factory.pageHead.generate(page: info)
}
2022-08-16 12:27:13 +02:00
private func makeHeader(page: Page.Metadata, metadata: Page.LocalizedMetadata, language: String, backText: String) -> String {
var content = [HeaderKey : String]()
content[.backLink] = factory.makeBackLink(text: backText, language: language)
content[.title] = metadata.title
content[.subtitle] = metadata.subtitle
content[.date] = factory.makeDateString(start: page.date, end: page.endDate)
return factory.factory.leftHeader.generate(content)
}
2022-08-16 10:39:05 +02:00
}