83 lines
3.4 KiB
Swift
83 lines
3.4 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
|
|
}
|
|
let path = page.fullPageUrl(for: language)
|
|
let inputContentPath = page.path + "/\(language).md"
|
|
let metadata = page.localized(for: language)
|
|
let nextLanguage = page.nextLanguage(for: language)
|
|
let (pageContent, pageIncludesCode, pageIsEmpty) = makeContent(
|
|
page: page, metadata: metadata, language: language, path: inputContentPath)
|
|
|
|
var content = [PageTemplate.Key : String]()
|
|
content[.head] = factory.pageHead.generate(page: page, language: language, includesCode: pageIncludesCode)
|
|
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)
|
|
}
|
|
content[.content] = pageContent
|
|
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()
|
|
|
|
if pageIncludesCode {
|
|
let highlightCode = factory.factory.html.codeHighlightFooter()
|
|
content[.footer] = (content[.footer].unwrapped { $0 + "\n" } ?? "") + highlightCode
|
|
}
|
|
|
|
let url = files.urlInOutputFolder(path)
|
|
if page.state == .draft {
|
|
files.isDraft(path: page.path)
|
|
} else if pageIsEmpty, page.state != .hidden {
|
|
files.isEmpty(page: path)
|
|
}
|
|
guard factory.page.generate(content, to: url) else {
|
|
return
|
|
}
|
|
files.generated(page: path)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
}
|