CHGenerator/Sources/Generator/Generators/PageContentGenerator.swift

403 lines
17 KiB
Swift
Raw Normal View History

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 {
2023-07-28 13:59:08 +02:00
private let pageLinkMarker = "page:"
2022-12-08 17:16:54 +01:00
private let largeImageIndicator = "*large*"
private let factory: TemplateFactory
2022-08-18 08:49:01 +02:00
private let swift = SyntaxHighlighter(format: HTMLOutputFormat())
2022-12-01 15:25:55 +01:00
private let siteRoot: Element
2022-12-04 19:15:22 +01:00
private let results: GenerationResultsHandler
init(factory: TemplateFactory, siteRoot: Element, results: GenerationResultsHandler) {
self.factory = factory
2022-12-01 15:25:55 +01:00
self.siteRoot = siteRoot
2022-12-04 19:15:22 +01:00
self.results = results
2022-08-16 10:39:05 +02:00
}
2022-08-29 13:35:25 +02:00
func generate(page: Element, language: String, content: String) -> (content: String, includesCode: Bool) {
2022-08-16 10:39:05 +02:00
var hasCodeContent = false
2022-12-08 17:16:54 +01:00
var largeImageCount = 0
2022-08-16 10:39:05 +02:00
let imageModifier = Modifier(target: .images) { html, markdown in
2022-12-08 17:16:54 +01:00
processMarkdownImage(markdown: markdown, html: html, page: page, language: language, largeImageCount: &largeImageCount)
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
}
let linkModifier = Modifier(target: .links) { html, markdown in
2022-08-31 00:02:42 +02:00
handleLink(page: page, language: language, html: html, markdown: markdown)
}
2022-08-29 13:35:25 +02:00
let htmlModifier = Modifier(target: .html) { html, markdown in
2022-08-31 00:02:42 +02:00
handleHTML(page: page, language: language, html: html, markdown: markdown)
2022-08-16 10:39:05 +02:00
}
2023-02-28 09:31:44 +01:00
let headlinesModifier = Modifier(target: .headings) { html, markdown in
handleHeadlines(page: page, language: language, html: html, markdown: markdown)
}
2022-08-16 10:39:05 +02:00
2023-02-28 09:31:44 +01:00
let parser = MarkdownParser(modifiers: [imageModifier, codeModifier, linkModifier, htmlModifier, headlinesModifier])
2022-08-29 13:35:25 +02:00
return (parser.html(from: content), hasCodeContent)
2022-08-16 10:39:05 +02:00
}
2022-08-31 00:02:42 +02:00
private func handleLink(page: Element, language: String, html: String, markdown: Substring) -> String {
let file = markdown.between("(", and: ")")
2023-07-28 13:59:08 +02:00
if file.hasPrefix(pageLinkMarker) {
let textToChange = file.dropAfterFirst("#")
let pageId = textToChange.replacingOccurrences(of: pageLinkMarker, with: "")
2022-12-05 17:25:07 +01:00
guard let pagePath = results.getPagePath(for: pageId, source: page.path, language: language) else {
2022-08-31 00:02:42 +02:00
// Remove link since the page can't be found
return markdown.between("[", and: "]")
}
// Adjust file path to get the page url
2022-12-05 17:25:07 +01:00
let url = page.relativePathToOtherSiteElement(file: pagePath)
2023-07-28 13:59:08 +02:00
return html.replacingOccurrences(of: textToChange, with: url)
2022-08-31 00:02:42 +02:00
}
if let filePath = page.nonAbsolutePathRelativeToRootForContainedInputFile(file) {
// The target of the page link must be present after generation is complete
2022-12-04 19:15:22 +01:00
results.expect(file: filePath, source: page.path)
2022-08-31 00:02:42 +02:00
}
return html
}
private func handleHTML(page: Element, language: String, html: String, markdown: Substring) -> String {
2022-12-04 19:15:22 +01:00
// TODO: Check HTML code in markdown for required resources
2022-08-31 00:02:42 +02:00
//print("[HTML] Found in page \(page.path):")
//print(markdown)
// Things to check:
// <img src=
// <a href=
//
return html
}
2023-02-28 09:31:44 +01:00
private func handleHeadlines(page: Element, language: String, 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: ">")
}
2022-12-08 17:16:54 +01:00
private func processMarkdownImage(markdown: Substring, html: String, page: Element, language: String, largeImageCount: inout Int) -> String {
2022-08-29 13:35:25 +02:00
// Split the markdown ![alt](file title)
2022-09-18 17:49:50 +02:00
// There are several known shorthand commands
2022-12-08 17:16:54 +01:00
// For images: ![*large* left_title](file right_title)
2022-08-29 13:35:25 +02:00
// For videos: ![option1,option2,...](file)
// For svg with custom area: ![x,y,width,height](file.svg)
2023-07-28 13:21:57 +02:00
// For downloads: ![download](file1, text1; file2, text2, (download-name); ...)
2022-09-18 17:21:57 +02:00
// For a simple boxes: ![box](title;body)
2022-09-25 17:19:07 +02:00
// A fancy page link: ![page](page_id)
2022-09-04 17:48:13 +02:00
// 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
2022-09-18 17:49:50 +02:00
if let alt = alt, let command = ShorthandMarkdownKey(rawValue: alt) {
2022-09-25 17:19:07 +02:00
return handleShortHandCommand(command, page: page, language: language, content: fileAndTitle)
2022-08-30 11:29:53 +02:00
}
2022-08-29 13:35:25 +02:00
let file = fileAndTitle.dropAfterFirst(" ")
let title = fileAndTitle.contains(" ") ? fileAndTitle.dropBeforeFirst(" ").nonEmpty : nil
let fileExtension = file.lastComponentAfter(".").lowercased()
2022-08-29 18:57:37 +02:00
if let _ = ImageType(fileExtension: fileExtension) {
2022-12-08 17:16:54 +01:00
return handleImage(page: page, file: file, rightTitle: title, leftTitle: alt, largeImageCount: &largeImageCount)
2022-08-29 18:57:37 +02:00
}
if let _ = VideoType(rawValue: fileExtension) {
return handleVideo(page: page, file: file, optionString: alt)
2022-08-29 18:57:37 +02:00
}
2022-12-19 23:31:06 +01:00
switch fileExtension {
case "svg":
2022-08-29 13:35:25 +02:00
return handleSvg(page: page, file: file, area: alt)
2022-12-19 23:31:06 +01:00
case "gif":
2022-12-21 12:59:42 +01:00
return handleGif(page: page, file: file, altText: alt ?? "gif image")
2022-12-19 23:31:06 +01:00
default:
return handleFile(page: page, file: file, fileExtension: fileExtension)
2022-08-16 10:39:05 +02:00
}
}
2022-08-16 10:39:05 +02:00
2022-09-25 17:19:07 +02:00
private func handleShortHandCommand(_ command: ShorthandMarkdownKey, page: Element, language: String, content: String) -> String {
2022-09-18 17:49:50 +02:00
switch command {
case .downloadButtons:
return handleDownloadButtons(page: page, content: content)
case .externalLink:
return handleExternalButtons(page: page, content: content)
case .includedHtml:
return handleExternalHTML(page: page, file: content)
case .box:
return handleSimpleBox(page: page, content: content)
2022-09-25 17:19:07 +02:00
case .pageLink:
return handlePageLink(page: page, language: language, pageId: content)
2022-09-18 17:49:50 +02:00
}
}
2022-12-08 17:16:54 +01:00
private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?, largeImageCount: inout Int) -> String {
2022-08-16 10:39:05 +02:00
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
2022-12-08 17:16:54 +01:00
let left: String
let createFullScreenVersion: Bool
if let leftTitle {
createFullScreenVersion = leftTitle.hasPrefix(largeImageIndicator)
left = leftTitle.dropBeforeFirst(largeImageIndicator).trimmed
} else {
left = ""
createFullScreenVersion = false
}
2022-12-04 19:15:22 +01:00
let size = results.requireFullSizeMultiVersionImage(
2022-09-18 16:47:13 +02:00
source: imagePath,
destination: imagePath,
requiredBy: page.path)
2022-08-16 10:39:05 +02:00
2022-12-21 12:59:42 +01:00
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 ?? ""
2022-12-08 17:16:54 +01:00
guard createFullScreenVersion else {
return factory.image.generate(content)
}
results.requireOriginalSizeImages(
source: imagePath,
destination: imagePath,
requiredBy: page.path)
largeImageCount += 1
2022-12-21 12:59:42 +01:00
content[.number] = "\(largeImageCount)"
2022-12-08 17:16:54 +01:00
return factory.largeImage.generate(content)
2022-08-16 10:39:05 +02:00
}
private func handleVideo(page: Element, file: String, optionString: String?) -> String {
let options: [PageVideoTemplate.VideoOption] = optionString.unwrapped { string in
2022-12-04 19:15:22 +01:00
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 {
2022-12-04 19:15:22 +01:00
results.warning("Unknown video option \(optionText)", source: page.path)
return nil
}
return option
}
} ?? []
2022-08-16 10:39:05 +02:00
2023-05-31 22:36:23 +02:00
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)
2023-05-31 23:08:55 +02:00
.map { (page.relativePathToFileWithPath($0.url), $0.type) }
2023-05-31 22:36:23 +02:00
// 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)
2022-08-16 10:39:05 +02:00
}
2022-08-18 08:49:36 +02:00
2023-05-31 22:36:23 +02:00
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)
}
}
2022-12-21 12:59:42 +01:00
private func handleGif(page: Element, file: String, altText: String) -> String {
2022-12-19 23:31:06 +01:00
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)
2022-12-21 12:59:42 +01:00
return factory.html.image(file: file, width: width, height: height, altText: altText)
2022-12-19 23:31:06 +01:00
}
2022-08-29 13:35:25 +02:00
private func handleSvg(page: Element, file: String, area: String?) -> String {
2022-08-18 08:49:36 +02:00
let imagePath = page.pathRelativeToRootForContainedInputFile(file)
2022-12-04 19:15:22 +01:00
results.require(file: imagePath, source: page.path)
2022-08-18 08:49:36 +02:00
2022-12-19 23:31:06 +01:00
guard let size = results.getImageSize(atPath: imagePath, source: page.path) else {
2022-12-21 12:59:42 +01:00
return "" // Missing image warning already produced
2022-12-19 23:31:06 +01:00
}
let width = Int(size.width)
let height = Int(size.height)
2022-12-21 12:59:42 +01:00
var altText = "image " + file.lastComponentAfter("/")
2022-08-29 13:35:25 +02:00
guard let area = area else {
2022-12-21 12:59:42 +01:00
return factory.html.image(file: file, width: width, height: height, altText: altText)
2022-08-29 13:35:25 +02:00
}
2022-08-31 00:02:42 +02:00
let parts = area.components(separatedBy: ",").map { $0.trimmed }
2022-12-21 12:59:42 +01:00
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]),
2022-12-19 23:31:06 +01:00
let y = Int(parts[1]),
let partWidth = Int(parts[2]),
let partHeight = Int(parts[3]) else {
2022-12-04 19:15:22 +01:00
results.warning("Invalid area string for svg image", source: page.path)
2022-12-21 12:59:42 +01:00
return factory.html.image(file: file, width: width, height: height, altText: altText)
2022-08-29 13:35:25 +02:00
}
2022-12-19 23:31:06 +01:00
let part = SVGSelection(x, y, partWidth, partHeight)
2022-12-27 09:57:34 +01:00
return factory.html.svgImage(file: file, part: part, altText: altText)
2022-08-18 08:49:36 +02:00
}
private func handleFile(page: Element, file: String, fileExtension: String) -> String {
2022-12-04 19:15:22 +01:00
results.warning("Unhandled file \(file) with extension \(fileExtension)", source: page.path)
2022-08-18 08:49:36 +02:00
return ""
}
2022-08-29 13:35:25 +02:00
private func handleDownloadButtons(page: Element, 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 {
2023-02-20 15:34:26 +01:00
results.warning("Invalid download definition with \(parts)", source: page.path)
2022-08-29 13:35:25 +02:00
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)
2022-12-04 19:15:22 +01:00
results.require(file: filePath, source: page.path)
2022-08-29 13:35:25 +02:00
return (file, title, downloadName)
}
return factory.html.downloadButtons(buttons)
}
2022-08-30 11:29:53 +02:00
private func handleExternalButtons(page: Element, 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 {
2022-12-04 19:15:22 +01:00
results.warning("Invalid external link definition", source: page.path)
2022-08-30 11:29:53 +02:00
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
}
2022-08-30 11:29:53 +02:00
let title = parts[1].trimmed
return (url, title)
}
return factory.html.externalButtons(buttons)
}
2022-09-08 13:12:55 +02:00
private func handleExternalHTML(page: Element, file: String) -> String {
2022-12-04 19:15:22 +01:00
let path = page.pathRelativeToRootForContainedInputFile(file)
return results.getContentOfRequiredFile(at: path, source: page.path) ?? ""
2022-09-08 13:12:55 +02:00
}
2022-09-18 17:21:57 +02:00
private func handleSimpleBox(page: Element, content: String) -> String {
let parts = content.components(separatedBy: ";")
guard parts.count > 1 else {
2022-12-04 19:15:22 +01:00
results.warning("Invalid box specification", source: page.path)
2022-09-18 17:21:57 +02:00
return ""
}
let title = parts[0]
let text = parts.dropFirst().joined(separator: ";")
return factory.makePlaceholder(title: title, text: text)
}
2022-09-25 17:19:07 +02:00
private func handlePageLink(page: Element, language: String, pageId: String) -> String {
guard let linkedPage = siteRoot.find(pageId) else {
2022-12-04 19:15:22 +01:00
// Checking the page path will add it to the missing pages
2022-12-05 17:25:07 +01:00
_ = results.getPagePath(for: pageId, source: page.path, language: language)
2022-09-25 17:19:07 +02:00
// Remove link since the page can't be found
return ""
}
2023-08-03 13:29:11 +02:00
guard linkedPage.state == .standard else {
// Prevent linking to unpublished content
return ""
}
2022-09-25 17:19:07 +02:00
var content = [PageLinkTemplate.Key: String]()
content[.title] = linkedPage.title(for: language)
2022-12-21 12:59:42 +01:00
content[.altText] = ""
2022-09-25 17:19:07 +02:00
let fullThumbnailPath = linkedPage.thumbnailFilePath(for: language).destination
2022-12-04 19:15:22 +01:00
// Note: Here we assume that the thumbnail was already used elsewhere, so already generated
2022-09-25 17:19:07 +02:00
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(".")
2022-09-25 17:19:07 +02:00
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)
}
2022-08-16 10:39:05 +02:00
}