275 lines
9.2 KiB
Swift
275 lines
9.2 KiB
Swift
import Foundation
|
|
import Ink
|
|
|
|
final class PageContentParser {
|
|
|
|
private let language: ContentLanguage
|
|
|
|
private let content: Content
|
|
|
|
private let results: PageGenerationResults
|
|
|
|
// MARK: Command handlers
|
|
|
|
private let buttonHandler: ButtonCommandProcessor
|
|
|
|
private let labelHandler: LabelsCommandProcessor
|
|
|
|
private let audioPlayer: AudioPlayerCommandProcessor
|
|
|
|
private let icons: IconCommandProcessor
|
|
|
|
private let box: BoxCommandProcessor
|
|
|
|
private let html: PageHtmlProcessor
|
|
|
|
private let video: VideoCommandProcessor
|
|
|
|
private let imageCompare: ImageCompareCommandProcessor
|
|
|
|
private let images: ImageCommandProcessor
|
|
|
|
// MARK: Other handlers
|
|
|
|
private let inlineLink: InlineLinkProcessor
|
|
|
|
private let code: PageCodeProcessor
|
|
|
|
init(content: Content, language: ContentLanguage, results: PageGenerationResults) {
|
|
self.content = content
|
|
self.results = results
|
|
self.language = language
|
|
self.buttonHandler = .init(content: content, results: results, language: language)
|
|
self.labelHandler = .init(content: content, results: results, language: language)
|
|
self.audioPlayer = .init(content: content, results: results, language: language)
|
|
self.icons = .init(content: content, results: results, language: language)
|
|
self.box = .init(content: content, results: results, language: language)
|
|
self.html = .init(content: content, results: results, language: language)
|
|
self.video = .init(content: content, results: results, language: language)
|
|
self.imageCompare = .init(content: content, results: results, language: language)
|
|
self.images = .init(content: content, results: results, language: language)
|
|
|
|
self.inlineLink = .init(content: content, results: results, language: language)
|
|
self.code = .init(results: results)
|
|
}
|
|
|
|
func generatePage(from content: String) -> String {
|
|
let parser = MarkdownParser(modifiers: [
|
|
Modifier(target: .images, closure: processMarkdownImage),
|
|
Modifier(target: .codeBlocks, closure: code.process),
|
|
Modifier(target: .links, closure: inlineLink.process),
|
|
Modifier(target: .html, closure: html.process),
|
|
Modifier(target: .headings, closure: handleHeadlines)
|
|
])
|
|
return parser.html(from: content)
|
|
}
|
|
|
|
/**
|
|
Modify headlines by extracting an id from the headline and adding it into the html element
|
|
|
|
Format: ##<title>#<id>
|
|
|
|
The id is created by lowercasing the string, removing all special characters, and replacing spaces with scores
|
|
*/
|
|
private func handleHeadlines(html: String, markdown: Substring) -> String {
|
|
let id = markdown
|
|
.last(after: "#")
|
|
.trimmed
|
|
.filter { $0.isNumber || $0.isLetter || $0 == " " }
|
|
.lowercased()
|
|
.components(separatedBy: " ")
|
|
.filter { $0 != "" }
|
|
.joined(separator: "-")
|
|
let parts = html.components(separatedBy: ">")
|
|
return parts[0] + " id=\"\(id)\">" + parts.dropFirst().joined(separator: ">")
|
|
}
|
|
|
|
private func percentDecoded(_ string: String) -> String {
|
|
guard let decoded = string.removingPercentEncoding else {
|
|
print("Invalid string: \(string)")
|
|
return string
|
|
}
|
|
return decoded
|
|
}
|
|
|
|
private func processMarkdownImage(html: String, markdown: Substring) -> String {
|
|
//
|
|
let argumentList = percentDecoded(markdown.between(first: "](", andLast: ")"))
|
|
let arguments = argumentList.components(separatedBy: ";")
|
|
|
|
|
|
let rawCommand = percentDecoded(markdown.between("![", and: "]").trimmed)
|
|
guard rawCommand != "" else {
|
|
return images.process(arguments, markdown: markdown)
|
|
}
|
|
|
|
guard let command = ShorthandMarkdownKey(rawValue: rawCommand) else {
|
|
// Treat unknown commands as normal links
|
|
results.invalid(command: nil, markdown)
|
|
return html
|
|
}
|
|
|
|
switch command {
|
|
case .image:
|
|
return images.process(arguments, markdown: markdown)
|
|
case .labels:
|
|
return labelHandler.process(arguments, markdown: markdown)
|
|
case .buttons:
|
|
return buttonHandler.process(arguments, markdown: markdown)
|
|
case .video:
|
|
return video.process(arguments, markdown: markdown)
|
|
case .pageLink:
|
|
return handlePageLink(arguments, markdown: markdown)
|
|
case .includedHtml:
|
|
return self.html.process(arguments, markdown: markdown)
|
|
case .box:
|
|
return box.process(arguments, markdown: markdown)
|
|
case .model:
|
|
return handleModel(arguments, markdown: markdown)
|
|
case .svg:
|
|
return handleSvg(arguments, markdown: markdown)
|
|
case .audioPlayer:
|
|
return audioPlayer.process(arguments, markdown: markdown)
|
|
case .tagLink:
|
|
return handleTagLink(arguments, markdown: markdown)
|
|
case .icons:
|
|
return icons.process(arguments, markdown: markdown)
|
|
case .imageCompare:
|
|
return imageCompare.process(arguments, markdown: markdown)
|
|
}
|
|
}
|
|
|
|
/**
|
|
Format: ``
|
|
*/
|
|
private func handlePageLink(_ arguments: [String], markdown: Substring) -> String {
|
|
guard arguments.count == 1 else {
|
|
results.invalid(command: .pageLink, markdown)
|
|
return ""
|
|
}
|
|
let pageId = arguments[0]
|
|
|
|
guard let page = content.page(pageId) else {
|
|
results.missing(page: pageId, source: "Page link command")
|
|
return ""
|
|
}
|
|
guard !page.isDraft else {
|
|
// Prevent linking to unpublished content
|
|
return ""
|
|
}
|
|
|
|
results.linked(to: page)
|
|
|
|
let localized = page.localized(in: language)
|
|
let url = page.absoluteUrl(in: language)
|
|
let title = localized.linkPreviewTitle ?? localized.title
|
|
let description = localized.linkPreviewDescription ?? ""
|
|
let image = makePageImage(item: localized)
|
|
|
|
return RelatedPageLink(
|
|
title: title,
|
|
description: description,
|
|
url: url,
|
|
image: image)
|
|
.content
|
|
}
|
|
|
|
/**
|
|
Format: ``
|
|
*/
|
|
private func handleTagLink(_ arguments: [String], markdown: Substring) -> String {
|
|
guard arguments.count == 1 else {
|
|
results.invalid(command: .tagLink, markdown)
|
|
return ""
|
|
}
|
|
let tagId = arguments[0]
|
|
|
|
guard let tag = content.tag(tagId) else {
|
|
results.missing(tag: tagId, source: "Tag link command")
|
|
return ""
|
|
}
|
|
|
|
let localized = tag.localized(in: language)
|
|
let url = tag.absoluteUrl(in: language)
|
|
let title = localized.name
|
|
let description = localized.linkPreviewDescription ?? ""
|
|
let image = makePageImage(item: localized)
|
|
|
|
return RelatedPageLink(
|
|
title: title,
|
|
description: description,
|
|
url: url,
|
|
image: image)
|
|
.content
|
|
}
|
|
|
|
private func makePageImage(item: LinkPreviewItem) -> ImageSet? {
|
|
item.linkPreviewImage.map { image in
|
|
let size = content.settings.pages.pageLinkImageSize
|
|
let imageSet = image.imageSet(width: size, height: size, language: language)
|
|
results.require(imageSet: imageSet)
|
|
return imageSet
|
|
}
|
|
}
|
|
|
|
/**
|
|
Format: ``
|
|
*/
|
|
private func handleModel(_ arguments: [String], markdown: Substring) -> String {
|
|
guard arguments.count == 1 else {
|
|
results.invalid(command: .model, markdown)
|
|
return ""
|
|
}
|
|
let fileId = arguments[0]
|
|
guard fileId.hasSuffix(".glb") else {
|
|
results.invalid(command: .model, markdown)
|
|
return ""
|
|
}
|
|
|
|
guard let file = content.file(fileId) else {
|
|
results.missing(file: fileId, source: "Model command")
|
|
return ""
|
|
}
|
|
results.require(file: file)
|
|
results.require(header: .modelViewer)
|
|
|
|
let description = file.localized(in: language)
|
|
return ModelViewer(file: file.absoluteUrl, description: description).content
|
|
}
|
|
|
|
private func handleSvg(_ arguments: [String], markdown: Substring) -> String {
|
|
guard arguments.count == 5 else {
|
|
results.invalid(command: .svg, markdown)
|
|
return ""
|
|
}
|
|
|
|
guard let x = Int(arguments[1]),
|
|
let y = Int(arguments[2]),
|
|
let partWidth = Int(arguments[3]),
|
|
let partHeight = Int(arguments[4]) else {
|
|
results.invalid(command: .svg, markdown)
|
|
return ""
|
|
}
|
|
|
|
let imageId = arguments[0]
|
|
|
|
guard let image = content.image(imageId) else {
|
|
results.missing(file: imageId, source: "SVG command")
|
|
return ""
|
|
}
|
|
guard image.type == .svg else {
|
|
results.invalid(command: .svg, markdown)
|
|
return ""
|
|
}
|
|
|
|
return PartialSvgImage(
|
|
imagePath: image.absoluteUrl,
|
|
altText: image.localized(in: language),
|
|
x: x,
|
|
y: y,
|
|
width: partWidth,
|
|
height: partHeight)
|
|
.content
|
|
}
|
|
}
|