79 lines
3.3 KiB
Swift
79 lines
3.3 KiB
Swift
import Foundation
|
|
import Ink
|
|
|
|
struct PageGenerator {
|
|
|
|
struct NavigationLink {
|
|
|
|
let link: String
|
|
|
|
let text: String
|
|
}
|
|
|
|
private let factory: LocalizedSiteTemplate
|
|
|
|
private let files: FileProcessor
|
|
|
|
init(factory: LocalizedSiteTemplate, files: FileProcessor) {
|
|
self.factory = factory
|
|
self.files = files
|
|
}
|
|
|
|
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)
|
|
var content = [PageTemplate.Key : String]()
|
|
content[.head] = try makeHead(page: page, language: language)
|
|
content[.topBar] = factory.topBar.generate(section: page.sectionId, languageButton: nextLanguage)
|
|
content[.contentClass] = "content"
|
|
content[.header] = makeHeader(page: page.metadata, metadata: metadata, language: language, backText: backText)
|
|
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()
|
|
|
|
let url = files.outputFolder.appendingPathComponent(path)
|
|
try factory.page.generate(content, to: url)
|
|
}
|
|
|
|
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(files: files).generate(page: page, language: language, at: url)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|