446 lines
17 KiB
Swift
446 lines
17 KiB
Swift
import Foundation
|
|
import Ink
|
|
import Splash
|
|
|
|
typealias VideoSource = (url: String, type: VideoFileType)
|
|
|
|
final class PageContentParser {
|
|
|
|
private let pageLinkMarker = "page:"
|
|
|
|
private let largeImageIndicator = "*large*"
|
|
|
|
private let swift = SyntaxHighlighter(format: HTMLOutputFormat())
|
|
|
|
private let results: GenerationResultsHandler
|
|
|
|
private let content: Content
|
|
|
|
private let imageGenerator: ImageGenerator
|
|
|
|
private let page: Page
|
|
|
|
private let language: ContentLanguage
|
|
|
|
private var largeImageCount: Int = 0
|
|
|
|
init(page: Page, content: Content, language: ContentLanguage, results: GenerationResultsHandler, imageGenerator: ImageGenerator) {
|
|
self.page = page
|
|
self.content = content
|
|
self.language = language
|
|
self.results = results
|
|
self.imageGenerator = imageGenerator
|
|
}
|
|
|
|
func generatePage(from content: String) -> String {
|
|
|
|
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>"
|
|
}
|
|
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)
|
|
}
|
|
|
|
private func handleLink(html: String, markdown: Substring) -> String {
|
|
let file = markdown.between("(", and: ")")
|
|
if file.hasPrefix(pageLinkMarker) {
|
|
// Retain links pointing to elements within a page
|
|
let textToChange = file.dropAfterFirst("#")
|
|
let pageId = textToChange.replacingOccurrences(of: pageLinkMarker, with: "")
|
|
guard let pagePath = content.pageLink(pageId: pageId, language: language) else {
|
|
// Remove link since the page can't be found
|
|
return markdown.between("[", and: "]")
|
|
}
|
|
// Adjust file path to get the page url
|
|
// TODO: Calculate relative links to make pages more portable
|
|
return html.replacingOccurrences(of: textToChange, with: pagePath)
|
|
}
|
|
|
|
// TODO: Check that linked file exists
|
|
// 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 {
|
|
// First, check the content type, then parse the remaining arguments
|
|
// Notation:
|
|
// <abc?> -> Optional argument
|
|
// <abc...> -> Repeated argument (0 or more)
|
|
// 
|
|
// 
|
|
// 
|
|
// 
|
|
// 
|
|
// 
|
|
// 
|
|
guard let argumentList = markdown.between(first: "](", andLast: ")").removingPercentEncoding else {
|
|
results.warning("Invalid percent encoding for markdown image", page: page)
|
|
return ""
|
|
}
|
|
let arguments = argumentList.components(separatedBy: ";")
|
|
|
|
let rawCommand = markdown.between("![", and: "]").trimmed
|
|
guard rawCommand != "" else {
|
|
return handleImage(arguments)
|
|
}
|
|
|
|
guard let convertedCommand = rawCommand.removingPercentEncoding,
|
|
let command = ShorthandMarkdownKey(rawValue: convertedCommand) else {
|
|
// Treat unknown commands as normal links
|
|
print("Unknown markdown command: \(rawCommand)")
|
|
return html
|
|
}
|
|
|
|
switch command {
|
|
case .image:
|
|
return handleImage(arguments)
|
|
case .hikingStatistics:
|
|
return handleHikingStatistics(arguments)
|
|
case .downloadButtons:
|
|
return handleDownloadButtons(arguments)
|
|
case .video:
|
|
return handleVideo(arguments)
|
|
default:
|
|
print("Unhandled markdown command: \(command)")
|
|
return ""
|
|
/*
|
|
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 .model:
|
|
return handle3dModel(content: content)
|
|
*/
|
|
}
|
|
}
|
|
|
|
private func handleImage(_ arguments: [String]) -> String {
|
|
// [image](<imageId>;<caption?>]
|
|
guard (1...2).contains(arguments.count) else {
|
|
results.warning("Invalid image arguments: \(arguments)", page: page)
|
|
return ""
|
|
}
|
|
let imageId = arguments[0]
|
|
|
|
guard let image = content.image(imageId) else {
|
|
results.warning("Missing image \(imageId)", page: page)
|
|
return ""
|
|
}
|
|
let caption = arguments.count == 2 ? arguments[1] : nil
|
|
let altText = image.getDescription(for: language)
|
|
|
|
let thumbnailWidth = CGFloat(content.settings.pages.contentWidth)
|
|
let thumbnail = imageGenerator.generateImageSet(
|
|
for: imageId,
|
|
maxWidth: thumbnailWidth, maxHeight: thumbnailWidth,
|
|
altText: altText)
|
|
|
|
let largeImageWidth = CGFloat(1200) // TODO: Move to settings
|
|
|
|
let largeImage = imageGenerator.generateImageSet(
|
|
for: imageId,
|
|
maxWidth: largeImageWidth, maxHeight: largeImageWidth,
|
|
altText: altText)
|
|
|
|
return PageImage(
|
|
imageId: imageId.replacingOccurrences(of: ".", with: "-"),
|
|
thumbnail: thumbnail,
|
|
largeImage: largeImage,
|
|
caption: caption).content
|
|
}
|
|
|
|
private func handleHikingStatistics(_ arguments: [String]) -> String {
|
|
guard (1...5).contains(arguments.count) else {
|
|
results.warning("Invalid hiking statistic arguments: \(arguments)", page: page)
|
|
return ""
|
|
}
|
|
|
|
let time = arguments[0].trimmed
|
|
let elevationUp = arguments.count > 1 ? arguments[1].trimmed : nil
|
|
let elevationDown = arguments.count > 2 ? arguments[2].trimmed : nil
|
|
let distance = arguments.count > 3 ? arguments[3].trimmed : nil
|
|
let calories = arguments.count > 4 ? arguments[4].trimmed : nil
|
|
|
|
return HikingStatistics(
|
|
time: time,
|
|
elevationUp: elevationUp,
|
|
elevationDown: elevationDown,
|
|
distance: distance,
|
|
calories: calories)
|
|
.content
|
|
}
|
|
|
|
private func handleDownloadButtons(_ arguments: [String]) -> String {
|
|
let buttons: [DownloadButtons.Item] = arguments.compactMap { button in
|
|
let parts = button.components(separatedBy: ",")
|
|
guard (2...3).contains(parts.count) else {
|
|
results.warning("Invalid download definition with \(parts)", page: page)
|
|
return nil
|
|
}
|
|
let file = parts[0].trimmed
|
|
let title = parts[1].trimmed
|
|
let downloadName = parts.count > 2 ? parts[2].trimmed : nil
|
|
|
|
// Ensure that file is available
|
|
guard let filePath = content.pathToFile(file) else {
|
|
results.warning("Missing download file \(file)", page: page)
|
|
return nil
|
|
}
|
|
|
|
return DownloadButtons.Item(filePath: filePath, text: title, downloadFileName: downloadName)
|
|
}
|
|
return DownloadButtons(items: buttons).content
|
|
}
|
|
|
|
private func handleVideo(_ arguments: [String]) -> String {
|
|
guard arguments.count >= 1 else {
|
|
return ""
|
|
}
|
|
let fileId = arguments[0].trimmed
|
|
|
|
let options: [VideoOption] = arguments.dropFirst().compactMap { optionText in
|
|
guard let optionText = optionText.trimmed.nonEmpty else {
|
|
return nil
|
|
}
|
|
guard let option = VideoOption(rawValue: optionText) else {
|
|
results.warning("Unknown video option \(optionText)", page: page)
|
|
return nil
|
|
}
|
|
return option
|
|
}
|
|
|
|
guard let filePath = content.pathToFile(fileId),
|
|
let file = content.file(id: fileId) else {
|
|
results.warning("Missing video file \(fileId)", page: page)
|
|
return ""
|
|
}
|
|
guard let videoType = file.type.videoType?.htmlType else {
|
|
results.warning("Unknown video file type for \(fileId)", page: page)
|
|
return ""
|
|
}
|
|
|
|
results.addRequiredVideoFile(fileId: fileId)
|
|
|
|
return ContentPageVideo(
|
|
filePath: filePath,
|
|
videoType: videoType,
|
|
options: options)
|
|
.content
|
|
}
|
|
|
|
/*
|
|
|
|
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 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", page: page)
|
|
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", page: page)
|
|
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", page: page)
|
|
return ""
|
|
}
|
|
let file = parts[0]
|
|
guard file.hasSuffix(".glb") else {
|
|
results.warning("Invalid 3d model file \(file) (must be .glb)", page: page)
|
|
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>
|
|
"""
|
|
}
|
|
*/
|
|
}
|