import Foundation import Ink import Splash typealias VideoSource = (url: String, type: VideoFileType) final class PageContentParser { private let pageLinkMarker = "page:" private let swift = SyntaxHighlighter(format: HTMLOutputFormat()) let results = PageGenerationResults() private let content: Content private let language: ContentLanguage private var largeImageCount: Int = 0 var largeImageWidth: Int { content.settings.pages.largeImageWidth } var thumbnailWidth: Int { content.settings.pages.contentWidth } init(content: Content, language: ContentLanguage) { self.content = content self.language = language } func requestImages(_ generator: ImageGenerator) { let thumbnailWidth = CGFloat(thumbnailWidth) let largeImageWidth = CGFloat(largeImageWidth) for image in results.files { guard case .image = image.type else { continue } generator.generateImageSet( for: image.id, maxWidth: thumbnailWidth, maxHeight: thumbnailWidth) generator.generateImageSet( for: image.id, maxWidth: largeImageWidth, maxHeight: largeImageWidth) } } func reset() { results.reset() largeImageCount = 0 } func generatePage(from content: String) -> String { reset() let parser = MarkdownParser(modifiers: [ Modifier(target: .images, closure: processMarkdownImage), Modifier(target: .codeBlocks, closure: handleCode), Modifier(target: .links, closure: handleLink), Modifier(target: .html, closure: handleHTML), Modifier(target: .headings, closure: handleHeadlines) ]) return parser.html(from: content) } private func handleCode(html: String, markdown: Substring) -> String { guard markdown.starts(with: "```swift") else { return html // Just use normal code highlighting } // Highlight swift code using Splash let code = markdown.between("```swift", and: "```").trimmed return "
" + swift.highlight(code) + "
" } 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 page = content.page(pageId) else { results.missingPages.insert(pageId) // Remove link since the page can't be found return markdown.between("[", and: "]") } results.linkedPages.insert(page) let pagePath = content.pageLink(page, language: language) 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: // # 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 processMarkdownImage(html: String, markdown: Substring) -> String { // First, check the content type, then parse the remaining arguments // Notation: // -> Optional argument // -> Repeated argument (0 or more) // ![url](;) // ![image](;] // ![video](;] // ![svg](;<;;;?>) // ![download](<,,;...) // ![box](;<body>) // ![model](<file>;<description>) // ![page](<pageId>) // ![external](<<url>;<text>...> // ![html](<fileId>) let argumentList = markdown.between(first: "](", andLast: ")").removingPercentEncoding ?? markdown.between(first: "](", andLast: ")") 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 results.warnings.append("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) case .externalLink: return handleExternalButtons(arguments) /* case .includedHtml: return handleExternalHTML(file: content) case .box: return handleSimpleBox(content: content) case .pageLink: return handlePageLink(pageId: content) case .model: return handle3dModel(content: content) */ default: results.warnings.append("Unhandled command '\(command.rawValue)'") return "" } } private func handleImage(_ arguments: [String]) -> String { // [image](<imageId>;<caption?>] guard (1...2).contains(arguments.count) else { results.invalidCommandArguments.append((.image , arguments)) return "" } let imageId = arguments[0] guard let image = content.image(imageId) else { results.missingFiles.insert(imageId) return "" } results.files.insert(image) let caption = arguments.count == 2 ? arguments[1] : nil let altText = image.getDescription(for: language) let path = content.pathToImage(image) let thumbnail = FeedEntryData.Image( rawImagePath: path, width: thumbnailWidth, height: thumbnailWidth, altText: altText) let largeImage = FeedEntryData.Image( rawImagePath: path, width: largeImageWidth, height: 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.invalidCommandArguments.append((.hikingStatistics, arguments)) 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 { // ![download](<<fileId>,<text>,<download-filename?>;...) let buttons: [ContentButtons.Item] = arguments.compactMap { button in let parts = button.components(separatedBy: ",") guard (2...3).contains(parts.count) else { results.invalidCommandArguments.append((.downloadButtons, parts)) 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.missingFiles.insert(file) return nil } return ContentButtons.Item(icon: .download, filePath: filePath, text: title, downloadFileName: downloadName) } return ContentButtons(items: buttons).content } private func handleVideo(_ arguments: [String]) -> String { // ![video](<fileId>;<option1...>] guard arguments.count >= 1 else { results.invalidCommandArguments.append((.video, arguments)) return "" } let fileId = arguments[0].trimmed let options = arguments.dropFirst().compactMap(convertVideoOption) guard let file = content.file(id: fileId) else { results.missingFiles.insert(fileId) return "" } results.files.insert(file) guard let videoType = file.type.videoType?.htmlType else { results.warnings.append("Unknown video file type for \(fileId)") return "" } let filePath = content.pathToFile(file) return ContentPageVideo( filePath: filePath, videoType: videoType, options: options) .content } private func convertVideoOption(_ videoOption: String) -> VideoOption? { guard let optionText = videoOption.trimmed.nonEmpty else { return nil } guard let option = VideoOption(rawValue: optionText) else { results.invalidCommandArguments.append((.video, [optionText])) return nil } if case let .poster(imageId) = option { if let image = content.image(imageId) { results.files.insert(image) let link = content.pathToImage(image) let width = 2*thumbnailWidth let fullLink = WebsiteImage.imagePath(source: link, width: width, height: width) return .poster(image: fullLink) } else { results.missingFiles.insert(imageId) return nil // Image file not present, so skip the option } } if case let .src(videoId) = option { if let video = content.video(videoId) { results.files.insert(video) let link = content.pathToVideo(video) // TODO: Set correct video path? return .src(link) } else { results.missingFiles.insert(videoId) return nil // Video file not present, so skip the option } } return option } /* 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(_ arguments: [String]) -> String { // ![external](<<url>;<text>...> guard arguments.count >= 1 else { results.invalidCommandArguments.append((.externalLink, arguments)) return "" } let buttons: [ContentButtons.Item] = arguments.compactMap { button in let parts = button.components(separatedBy: ",") guard parts.count == 2 else { results.invalidCommandArguments.append((.externalLink, parts)) return nil } let rawUrl = parts[0].trimmed guard let url = rawUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { results.invalidCommandArguments.append((.externalLink, parts)) return nil } let title = parts[1].trimmed return .init( icon: .externalLink, filePath: url, text: title) } return ContentButtons(items: buttons).content } /* 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> """ } */ }