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-26 17:40:51 +02:00
|
|
|
init(factory: LocalizedSiteTemplate) {
|
2022-08-16 10:39:05 +02:00
|
|
|
self.factory = factory
|
|
|
|
}
|
|
|
|
|
2022-08-26 22:30:10 +02:00
|
|
|
func generate(page: Element, language: String, nextPage: NavigationLink?, previousPage: NavigationLink?) {
|
2022-08-16 10:39:05 +02:00
|
|
|
guard !page.isExternalPage else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let path = page.fullPageUrl(for: language)
|
2022-08-26 17:40:51 +02:00
|
|
|
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-09-02 23:19:30 +02:00
|
|
|
let (pageContent, pageIncludesCode, pageIsEmpty) = makeContent(
|
|
|
|
page: page, metadata: metadata, language: language, path: inputContentPath)
|
|
|
|
|
2022-08-16 12:27:13 +02:00
|
|
|
var content = [PageTemplate.Key : String]()
|
2022-08-29 13:35:25 +02:00
|
|
|
content[.head] = factory.pageHead.generate(page: page, language: language, includesCode: pageIncludesCode)
|
2022-08-26 17:40:51 +02:00
|
|
|
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-08-28 14:01:53 +02:00
|
|
|
content[.previousPageLinkText] = previousPage.unwrapped { factory.factory.html.makePrevText($0.text) }
|
2022-08-16 10:39:05 +02:00
|
|
|
content[.previousPageUrl] = previousPage?.link
|
2022-08-28 14:01:53 +02:00
|
|
|
content[.nextPageLinkText] = nextPage.unwrapped { factory.factory.html.makeNextText($0.text) }
|
2022-08-16 10:39:05 +02:00
|
|
|
content[.nextPageUrl] = nextPage?.link
|
2022-08-26 17:40:51 +02:00
|
|
|
content[.footer] = page.customFooterContent()
|
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-26 17:40:51 +02:00
|
|
|
let url = files.urlInOutputFolder(path)
|
2022-08-31 09:02:40 +02:00
|
|
|
if page.state == .draft {
|
|
|
|
files.isDraft(path: page.path)
|
2022-09-02 23:19:30 +02:00
|
|
|
} else if pageIsEmpty, page.state != .hidden {
|
2022-08-29 19:20:13 +02:00
|
|
|
files.isEmpty(page: path)
|
|
|
|
}
|
2022-08-26 17:40:51 +02:00
|
|
|
guard factory.page.generate(content, to: url) else {
|
|
|
|
return
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
2022-09-02 23:19:13 +02:00
|
|
|
files.generated(page: path)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 23:19:30 +02:00
|
|
|
private func makeContent(page: Element, metadata: Element.LocalizedMetadata, language: String, path: String) -> (content: String, includesCode: Bool, isEmpty: Bool) {
|
|
|
|
if let raw = files.contentOfOptionalFile(atPath: path, source: page.path, createEmptyFileIfMissing: true)?
|
|
|
|
.trimmed.nonEmpty {
|
|
|
|
let (content, includesCode) = PageContentGenerator(factory: factory.factory)
|
|
|
|
.generate(page: page, language: language, content: raw)
|
|
|
|
return (content, includesCode, false)
|
|
|
|
} else {
|
|
|
|
let (content, includesCode) = PageContentGenerator(factory: factory.factory)
|
|
|
|
.generate(page: page, language: language, content: metadata.placeholderText)
|
|
|
|
let placeholder = factory.makePlaceholder(title: metadata.placeholderTitle, text: content)
|
|
|
|
return (placeholder, includesCode, true)
|
2022-08-26 17:40:51 +02:00
|
|
|
}
|
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? {
|
2022-08-28 11:14:11 +02:00
|
|
|
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-26 17:40:51 +02:00
|
|
|
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|