2022-08-16 10:39:05 +02:00
|
|
|
import Foundation
|
|
|
|
import Ink
|
2022-08-18 08:49:01 +02:00
|
|
|
import Splash
|
2022-08-16 10:39:05 +02:00
|
|
|
|
|
|
|
struct PageContentGenerator {
|
|
|
|
|
2022-08-18 08:49:36 +02:00
|
|
|
#warning("Specify page image width in configuration")
|
|
|
|
let pageImageWidth = 748
|
|
|
|
|
2022-08-17 10:36:21 +02:00
|
|
|
private let factory: TemplateFactory
|
|
|
|
|
2022-08-18 08:49:01 +02:00
|
|
|
private let swift = SyntaxHighlighter(format: HTMLOutputFormat())
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
init(factory: TemplateFactory) {
|
2022-08-17 10:36:21 +02:00
|
|
|
self.factory = factory
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
func generate(page: Element, language: String, content: String) -> String {
|
2022-08-16 10:39:05 +02:00
|
|
|
|
|
|
|
var hasCodeContent = false
|
|
|
|
|
|
|
|
let imageModifier = Modifier(target: .images) { html, markdown in
|
2022-08-26 17:40:51 +02:00
|
|
|
processMarkdownImage(markdown: markdown, html: html, page: page)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
let codeModifier = Modifier(target: .codeBlocks) { html, markdown in
|
|
|
|
if markdown.starts(with: "```swift") {
|
2022-08-18 08:49:01 +02:00
|
|
|
let code = markdown.between("```swift", and: "```").trimmed
|
|
|
|
return "<pre><code>" + swift.highlight(code) + "</pre></code>"
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
hasCodeContent = true
|
|
|
|
return html
|
|
|
|
}
|
2022-08-17 10:36:21 +02:00
|
|
|
let linkModifier = Modifier(target: .links) { html, markdown in
|
|
|
|
#warning("Check links in markdown for (missing) files to copy")
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
|
|
|
let parser = MarkdownParser(modifiers: [imageModifier, codeModifier, linkModifier])
|
2022-08-16 10:39:05 +02:00
|
|
|
|
|
|
|
if hasCodeContent {
|
2022-08-26 17:40:51 +02:00
|
|
|
#warning("Automatically add hljs highlighting if code samples are found")
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
return parser.html(from: content)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
private func processMarkdownImage(markdown: Substring, html: String, page: Element) -> String {
|
2022-08-17 10:36:21 +02:00
|
|
|
// Split the markdown ![alt](file "title")
|
|
|
|
// For images: ![left_title](file "right_title")
|
|
|
|
// For videos: ![option...](file)
|
|
|
|
// For files: ?
|
|
|
|
let fileAndTitle = markdown.between("(", and: ")")
|
|
|
|
let file = fileAndTitle.dropAfterFirst(" \"")
|
|
|
|
let title = fileAndTitle.contains(" \"") ? fileAndTitle.between("\"", and: "\"").nonEmpty : nil
|
|
|
|
let alt = markdown.between("[", and: "]").nonEmpty
|
|
|
|
|
|
|
|
let fileExtension = file.lastComponentAfter(".").lowercased()
|
2022-08-26 17:40:51 +02:00
|
|
|
switch MediaType(fileExtension: fileExtension) {
|
2022-08-17 10:36:21 +02:00
|
|
|
case .image:
|
2022-08-26 17:40:51 +02:00
|
|
|
return handleImage(page: page, file: file, rightTitle: title, leftTitle: alt)
|
2022-08-17 10:36:21 +02:00
|
|
|
case .video:
|
2022-08-26 17:40:51 +02:00
|
|
|
return handleVideo(page: page, file: file, optionString: alt)
|
2022-08-17 10:36:21 +02:00
|
|
|
case .file:
|
2022-08-18 08:49:36 +02:00
|
|
|
if fileExtension == "svg" {
|
2022-08-26 17:40:51 +02:00
|
|
|
return handleSvg(page: page, file: file)
|
2022-08-18 08:49:36 +02:00
|
|
|
}
|
2022-08-26 17:40:51 +02:00
|
|
|
return handleFile(page: page, file: file, fileExtension: fileExtension)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
2022-08-17 10:36:21 +02:00
|
|
|
}
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?) -> String {
|
2022-08-16 10:39:05 +02:00
|
|
|
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
let size = files.requireImage(source: imagePath, destination: imagePath, width: pageImageWidth)
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-08-17 10:36:21 +02:00
|
|
|
let imagePath2x = imagePath.insert("@2x", beforeLast: ".")
|
|
|
|
let file2x = file.insert("@2x", beforeLast: ".")
|
2022-08-26 17:40:51 +02:00
|
|
|
files.requireImage(source: imagePath, destination: imagePath2x, width: 2 * pageImageWidth)
|
2022-08-17 10:36:21 +02:00
|
|
|
|
|
|
|
let content: [PageImageTemplate.Key : String] = [
|
|
|
|
.image: file,
|
|
|
|
.image2x: file2x,
|
|
|
|
.width: "\(Int(size.width))",
|
|
|
|
.height: "\(Int(size.height))",
|
|
|
|
.leftText: leftTitle ?? "",
|
|
|
|
.rightText: rightTitle ?? ""]
|
|
|
|
return factory.image.generate(content)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
private func handleVideo(page: Element, file: String, optionString: String?) -> String {
|
2022-08-17 10:36:21 +02:00
|
|
|
let options: [PageVideoTemplate.VideoOption] = optionString.unwrapped { string in
|
|
|
|
string.components(separatedBy: " ").compactMap { optionText in
|
|
|
|
guard let optionText = optionText.trimmed.nonEmpty else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard let option = PageVideoTemplate.VideoOption(rawValue: optionText) else {
|
|
|
|
print("[WARN] Unknown video option \(optionText) in page \(page.path)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return option
|
|
|
|
}
|
|
|
|
} ?? []
|
|
|
|
#warning("Check page folder for alternative video versions")
|
|
|
|
let sources: [PageVideoTemplate.VideoSource] = [(url: file, type: .mp4)]
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-08-17 10:36:21 +02:00
|
|
|
let filePath = page.pathRelativeToRootForContainedInputFile(file)
|
|
|
|
files.require(file: filePath)
|
|
|
|
return factory.video.generate(sources: sources, options: options)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
2022-08-18 08:49:36 +02:00
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
private func handleSvg(page: Element, file: String) -> String {
|
2022-08-18 08:49:36 +02:00
|
|
|
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
|
|
|
|
files.require(file: imagePath)
|
|
|
|
|
|
|
|
return """
|
|
|
|
<span class="image">
|
|
|
|
<img src="\(file)"/>
|
|
|
|
</span>
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
private func handleFile(page: Element, file: String, fileExtension: String) -> String {
|
2022-08-18 08:49:36 +02:00
|
|
|
#warning("Handle other files in markdown")
|
|
|
|
print("[WARN] Unhandled file \(file) with extension \(fileExtension)")
|
|
|
|
return ""
|
|
|
|
}
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|