CHGenerator/Sources/Generator/Generators/PageContentGenerator.swift
2023-12-16 22:10:27 +01:00

467 lines
19 KiB
Swift

import Foundation
import Ink
import Splash
struct PageContentGenerator {
private let factory: TemplateFactory
private let siteRoot: Element
private let results: GenerationResultsHandler
init(factory: TemplateFactory, siteRoot: Element, results: GenerationResultsHandler) {
self.factory = factory
self.siteRoot = siteRoot
self.results = results
}
func generate(page: Element, language: String, content: String) -> (content: String, headers: RequiredHeaders) {
let parser = PageContentParser(
factory: factory,
siteRoot: siteRoot,
results: results,
page: page,
language: language)
return parser.generatePage(from: content)
}
}
final class PageContentParser {
private let pageLinkMarker = "page:"
private let largeImageIndicator = "*large*"
private let factory: TemplateFactory
private let swift = SyntaxHighlighter(format: HTMLOutputFormat())
private let siteRoot: Element
private let results: GenerationResultsHandler
private let page: Element
private let language: String
private var headers = RequiredHeaders()
private var largeImageCount: Int = 0
init(factory: TemplateFactory, siteRoot: Element, results: GenerationResultsHandler, page: Element, language: String) {
self.factory = factory
self.siteRoot = siteRoot
self.results = results
self.page = page
self.language = language
}
func generatePage(from content: String) -> (content: String, headers: RequiredHeaders) {
headers = .init()
let imageModifier = Modifier(target: .images) { html, markdown in
self.processMarkdownImage(markdown: markdown, html: html)
}
let codeModifier = Modifier(target: .codeBlocks) { html, markdown in
if markdown.starts(with: "```swift") {
let code = markdown.between("```swift", and: "```").trimmed
return "<pre><code>" + self.swift.highlight(code) + "</pre></code>"
}
self.headers.insert(.codeHightlighting)
return html
}
let linkModifier = Modifier(target: .links) { html, markdown in
self.handleLink(html: html, markdown: markdown)
}
let htmlModifier = Modifier(target: .html) { html, markdown in
self.handleHTML(html: html, markdown: markdown)
}
let headlinesModifier = Modifier(target: .headings) { html, markdown in
self.handleHeadlines(html: html, markdown: markdown)
}
let parser = MarkdownParser(modifiers: [imageModifier, codeModifier, linkModifier, htmlModifier, headlinesModifier])
return (parser.html(from: content), headers)
}
private func handleLink(html: String, markdown: Substring) -> String {
let file = markdown.between("(", and: ")")
if file.hasPrefix(pageLinkMarker) {
let textToChange = file.dropAfterFirst("#")
let pageId = textToChange.replacingOccurrences(of: pageLinkMarker, with: "")
guard let pagePath = results.getPagePath(for: pageId, source: page.path, language: language) else {
// Remove link since the page can't be found
return markdown.between("[", and: "]")
}
// Adjust file path to get the page url
let url = page.relativePathToOtherSiteElement(file: pagePath)
return html.replacingOccurrences(of: textToChange, with: url)
}
if let filePath = page.nonAbsolutePathRelativeToRootForContainedInputFile(file) {
// The target of the page link must be present after generation is complete
results.expect(file: filePath, source: page.path)
}
return html
}
private func handleHTML(html: String, markdown: Substring) -> String {
// TODO: Check HTML code in markdown for required resources
//print("[HTML] Found in page \(page.path):")
//print(markdown)
// Things to check:
// <img src=
// <a href=
//
return html
}
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 processMarkdownImage(markdown: Substring, html: String) -> String {
// Split the markdown ![alt](file title)
// There are several known shorthand commands
// For images: ![*large* left_title](file right_title)
// For videos: ![option1,option2,...](file)
// For svg with custom area: ![x,y,width,height](file.svg)
// For downloads: ![download](file1, text1; file2, text2, (download-name); ...)
// For a simple box: ![box](title;body)
// For 3D models: ![3d](file;Description)
// A fancy page link: ![page](page_id)
// External pages: ![external](url1, text1; url2, text2, ...)
guard let fileAndTitle = markdown.between(first: "](", andLast: ")").removingPercentEncoding else {
results.warning("Invalid percent encoding for markdown image", source: page.path)
return ""
}
let alt = markdown.between("[", and: "]").nonEmpty?.removingPercentEncoding
if let alt = alt, let command = ShorthandMarkdownKey(rawValue: alt) {
return handleShortHandCommand(command, content: fileAndTitle)
}
let file = fileAndTitle.dropAfterFirst(" ")
let title = fileAndTitle.contains(" ") ? fileAndTitle.dropBeforeFirst(" ").nonEmpty : nil
let fileExtension = file.lastComponentAfter(".").lowercased()
if let _ = ImageType(fileExtension: fileExtension) {
return handleImage(file: file, rightTitle: title, leftTitle: alt, largeImageCount: &largeImageCount)
}
if let _ = VideoType(rawValue: fileExtension) {
return handleVideo(file: file, optionString: alt)
}
switch fileExtension {
case "svg":
return handleSvg(file: file, area: alt)
case "gif":
return handleGif(file: file, altText: alt ?? "gif image")
default:
return handleFile(file: file, fileExtension: fileExtension)
}
}
private func handleShortHandCommand(_ command: ShorthandMarkdownKey, content: String) -> String {
switch command {
case .downloadButtons:
return handleDownloadButtons(content: content)
case .externalLink:
return handleExternalButtons(content: content)
case .includedHtml:
return handleExternalHTML(file: content)
case .box:
return handleSimpleBox(content: content)
case .pageLink:
return handlePageLink(pageId: content)
case .model3d:
return handle3dModel(content: content)
}
}
private func handleImage(file: String, rightTitle: String?, leftTitle: String?, largeImageCount: inout Int) -> String {
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
let left: String
let createFullScreenVersion: Bool
if let leftTitle {
createFullScreenVersion = leftTitle.hasPrefix(largeImageIndicator)
left = leftTitle.dropBeforeFirst(largeImageIndicator).trimmed
} else {
left = ""
createFullScreenVersion = false
}
let size = results.requireFullSizeMultiVersionImage(
source: imagePath,
destination: imagePath,
requiredBy: page.path)
let altText = left.nonEmpty ?? rightTitle ?? "image"
var content = [PageImageTemplateKey : String]()
content[.altText] = altText
content[.image] = file.dropAfterLast(".")
content[.imageExtension] = file.lastComponentAfter(".")
content[.width] = "\(Int(size.width))"
content[.height] = "\(Int(size.height))"
content[.leftText] = left
content[.rightText] = rightTitle ?? ""
guard createFullScreenVersion else {
return factory.image.generate(content)
}
results.requireOriginalSizeImages(
source: imagePath,
destination: imagePath,
requiredBy: page.path)
largeImageCount += 1
content[.number] = "\(largeImageCount)"
return factory.largeImage.generate(content)
}
private func handleVideo(file: String, optionString: String?) -> String {
let options: [PageVideoTemplate.VideoOption] = optionString.unwrapped { string in
string.components(separatedBy: " ").compactMap { optionText -> PageVideoTemplate.VideoOption? in
guard let optionText = optionText.trimmed.nonEmpty else {
return nil
}
guard let option = PageVideoTemplate.VideoOption(rawValue: optionText) else {
results.warning("Unknown video option \(optionText)", source: page.path)
return nil
}
return option
}
} ?? []
let prefix = file.lastComponentAfter("/").dropAfterLast(".")
// Find all video files starting with the name of the video as a prefix
var sources: [PageVideoTemplate.VideoSource] = []
do {
let folder = results.contentFolder.appendingPathComponent(page.path)
let filesInFolder = try FileManager.default.contentsOfDirectory(atPath: folder.path)
sources += selectVideoFiles(with: prefix, from: filesInFolder)
} catch {
results.warning("Failed to check for additional videos", source: page.path)
}
// Also look in external files
sources += selectVideoFiles(with: prefix, from: page.externalFiles)
.map { (page.relativePathToFileWithPath($0.url), $0.type) }
// Require all video files
sources.forEach {
let path = page.pathRelativeToRootForContainedInputFile($0.url)
results.require(file: path, source: page.path)
}
// Sort, so that order of selection in browser is defined
sources.sort { $0.url < $1.url }
return factory.video.generate(sources: sources, options: options)
}
private func selectVideoFiles<T>(with prefix: String, from all: T) -> [PageVideoTemplate.VideoSource] where T: Sequence, T.Element == String {
all.compactMap {
guard $0.lastComponentAfter("/").hasPrefix(prefix) else {
return nil
}
guard let type = VideoType(rawValue: $0.lastComponentAfter(".").lowercased()) else {
return nil
}
return (url: $0, type: type)
}
}
private func handleGif(file: String, altText: String) -> String {
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
results.require(file: imagePath, source: page.path)
guard let size = results.getImageSize(atPath: imagePath, source: page.path) else {
return ""
}
let width = Int(size.width)
let height = Int(size.height)
return factory.html.image(file: file, width: width, height: height, altText: altText)
}
private func handleSvg(file: String, area: String?) -> String {
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
results.require(file: imagePath, source: page.path)
guard let size = results.getImageSize(atPath: imagePath, source: page.path) else {
return "" // Missing image warning already produced
}
let width = Int(size.width)
let height = Int(size.height)
var altText = "image " + file.lastComponentAfter("/")
guard let area = area else {
return factory.html.image(file: file, width: width, height: height, altText: altText)
}
let parts = area.components(separatedBy: ",").map { $0.trimmed }
switch parts.count {
case 1:
return factory.html.image(file: file, width: width, height: height, altText: parts[0])
case 4:
break
case 5:
altText = parts[4]
default:
results.warning("Invalid area string for svg image", source: page.path)
return factory.html.image(file: file, width: width, height: height, altText: altText)
}
guard let x = Int(parts[0]),
let y = Int(parts[1]),
let partWidth = Int(parts[2]),
let partHeight = Int(parts[3]) else {
results.warning("Invalid area string for svg image", source: page.path)
return factory.html.image(file: file, width: width, height: height, altText: altText)
}
let part = SVGSelection(x, y, partWidth, partHeight)
return factory.html.svgImage(file: file, part: part, altText: altText)
}
private func handleFile(file: String, fileExtension: String) -> String {
results.warning("Unhandled file \(file) with extension \(fileExtension)", source: page.path)
return ""
}
private func handleDownloadButtons(content: String) -> String {
let buttons = content
.components(separatedBy: ";")
.compactMap { button -> (file: String, text: String, downloadName: String?)? in
let parts = button.components(separatedBy: ",")
guard parts.count == 2 || parts.count == 3 else {
results.warning("Invalid download definition with \(parts)", source: page.path)
return nil
}
let file = parts[0].trimmed
let title = parts[1].trimmed
let downloadName = parts.count == 3 ? parts[2].trimmed : nil
// Ensure that file is available
let filePath = page.pathRelativeToRootForContainedInputFile(file)
results.require(file: filePath, source: page.path)
return (file, title, downloadName)
}
return factory.html.downloadButtons(buttons)
}
private func handleExternalButtons(content: String) -> String {
let buttons = content
.components(separatedBy: ";")
.compactMap { button -> (url: String, text: String)? in
let parts = button.components(separatedBy: ",")
guard parts.count == 2 else {
results.warning("Invalid external link definition", source: page.path)
return nil
}
guard let url = parts[0].trimmed.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
results.warning("Invalid external link \(parts[0].trimmed)", source: page.path)
return nil
}
let title = parts[1].trimmed
return (url, title)
}
return factory.html.externalButtons(buttons)
}
private func handleExternalHTML(file: String) -> String {
let path = page.pathRelativeToRootForContainedInputFile(file)
return results.getContentOfRequiredFile(at: path, source: page.path) ?? ""
}
private func handleSimpleBox(content: String) -> String {
let parts = content.components(separatedBy: ";")
guard parts.count > 1 else {
results.warning("Invalid box specification", source: page.path)
return ""
}
let title = parts[0]
let text = parts.dropFirst().joined(separator: ";")
return factory.makePlaceholder(title: title, text: text)
}
private func handlePageLink(pageId: String) -> String {
guard let linkedPage = siteRoot.find(pageId) else {
// Checking the page path will add it to the missing pages
_ = results.getPagePath(for: pageId, source: page.path, language: language)
// Remove link since the page can't be found
return ""
}
guard linkedPage.state == .standard else {
// Prevent linking to unpublished content
return ""
}
var content = [PageLinkTemplate.Key: String]()
content[.title] = linkedPage.title(for: language)
content[.altText] = ""
let fullThumbnailPath = linkedPage.thumbnailFilePath(for: language).destination
// Note: Here we assume that the thumbnail was already used elsewhere, so already generated
let relativeImageUrl = page.relativePathToOtherSiteElement(file: fullThumbnailPath)
let metadata = linkedPage.localized(for: language)
if linkedPage.state.hasThumbnailLink {
let fullPageUrl = linkedPage.fullPageUrl(for: language)
let relativePageUrl = page.relativePathToOtherSiteElement(file: fullPageUrl)
content[.url] = "href=\"\(relativePageUrl)\""
}
content[.image] = relativeImageUrl.dropAfterLast(".")
if let suffix = metadata.thumbnailSuffix {
content[.title] = factory.html.make(title: metadata.title, suffix: suffix)
} else {
content[.title] = metadata.title
}
let path = linkedPage.makePath(language: language, from: siteRoot)
content[.path] = factory.pageLink.makePath(components: path)
content[.description] = metadata.relatedContentText
if let parent = linkedPage.findParent(from: siteRoot), parent.thumbnailStyle == .large {
content[.className] = " related-page-link-large"
}
// We assume that the thumbnail images are already required by overview pages.
return factory.pageLink.generate(content)
}
private func handle3dModel(content: String) -> String {
let parts = content.components(separatedBy: ";")
guard parts.count > 1 else {
results.warning("Invalid 3d model specification", source: page.path)
return ""
}
let file = parts[0]
guard file.hasSuffix(".glb") else {
results.warning("Invalid 3d model file \(file) (must be .glb)", source: page.path)
return ""
}
// Ensure that file is available
let filePath = page.pathRelativeToRootForContainedInputFile(file)
results.require(file: filePath, source: page.path)
// Add required file to head
headers.insert(.modelViewer)
let description = parts.dropFirst().joined(separator: ";")
return """
<model-viewer alt="\(description)" src="\(file)" ar shadow-intensity="1" camera-controls touch-action="pan-y"></model-viewer>
"""
}
}