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-16 12:26:45 +02:00
|
|
|
private let files: FileProcessor
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-08-18 08:49:01 +02:00
|
|
|
private let swift = SyntaxHighlighter(format: HTMLOutputFormat())
|
|
|
|
|
2022-08-17 10:36:21 +02:00
|
|
|
init(factory: TemplateFactory, files: FileProcessor) {
|
|
|
|
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, at url: URL) throws -> String {
|
|
|
|
var errorToThrow: Error? = nil
|
|
|
|
|
|
|
|
let content = try wrap(.missingPage(page: url.path, language: language)) {
|
|
|
|
try String(contentsOf: url)
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasCodeContent = false
|
|
|
|
|
|
|
|
let imageModifier = Modifier(target: .images) { html, markdown in
|
2022-08-17 10:36:21 +02:00
|
|
|
do {
|
|
|
|
return try processMarkdownImage(markdown: markdown, html: html, page: page)
|
|
|
|
} catch {
|
2022-08-16 10:39:05 +02:00
|
|
|
errorToThrow = error
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
#warning("Automatically add hljs hightlighting if code samples are found")
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = parser.html(from: content)
|
|
|
|
if let error = errorToThrow {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-08-17 10:36:21 +02:00
|
|
|
private func processMarkdownImage(markdown: Substring, html: String, page: Page) throws -> String {
|
|
|
|
// 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()
|
|
|
|
switch files.mediaType(forExtension: fileExtension) {
|
|
|
|
case .image:
|
|
|
|
return try handleImage(page: page, file: file, rightTitle: title, leftTitle: alt)
|
|
|
|
case .video:
|
|
|
|
return try handleVideo(page: page, file: file, optionString: alt)
|
|
|
|
case .file:
|
2022-08-18 08:49:36 +02:00
|
|
|
if fileExtension == "svg" {
|
|
|
|
return try handleSvg(page: page, file: file)
|
|
|
|
}
|
|
|
|
return try 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-17 10:36:21 +02:00
|
|
|
private func handleImage(page: Page, file: String, rightTitle: String?, leftTitle: String?) throws -> String {
|
2022-08-16 10:39:05 +02:00
|
|
|
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
|
|
|
|
|
2022-08-17 10:36:21 +02:00
|
|
|
let size = try 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: ".")
|
|
|
|
try files.requireImage(source: imagePath, destination: imagePath2x, width: 2 * pageImageWidth)
|
|
|
|
|
|
|
|
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-17 10:36:21 +02:00
|
|
|
private func handleVideo(page: Page, file: String, optionString: String?) throws -> String {
|
|
|
|
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
|
|
|
|
|
|
|
private func handleSvg(page: Page, file: String) throws -> String {
|
|
|
|
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
|
|
|
|
files.require(file: imagePath)
|
|
|
|
|
|
|
|
return """
|
|
|
|
<span class="image">
|
|
|
|
<img src="\(file)"/>
|
|
|
|
</span>
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
|
|
|
|
private func handleFile(page: Page, file: String, fileExtension: String) throws -> String {
|
|
|
|
#warning("Handle other files in markdown")
|
|
|
|
print("[WARN] Unhandled file \(file) with extension \(fileExtension)")
|
|
|
|
return ""
|
|
|
|
}
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|