From 0eee8454310481bf5a88a3100eb8af3aeda3cabf Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 19 Dec 2022 23:31:06 +0100 Subject: [PATCH] Support Gif in pages --- .../Generators/HTMLElementsGenerator.swift | 15 ++++--- .../Generators/PageContentGenerator.swift | 42 ++++++++++++++----- .../Processing/GenerationResultsHandler.swift | 4 +- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Sources/Generator/Generators/HTMLElementsGenerator.swift b/Sources/Generator/Generators/HTMLElementsGenerator.swift index 924e27f..c753861 100644 --- a/Sources/Generator/Generators/HTMLElementsGenerator.swift +++ b/Sources/Generator/Generators/HTMLElementsGenerator.swift @@ -1,5 +1,7 @@ import Foundation +typealias SVGSelection = (x: Int, y: Int, width: Int, height: Int) + struct HTMLElementsGenerator { init() { @@ -37,20 +39,17 @@ struct HTMLElementsGenerator { "\(text)" } - func svgImage(file: String) -> String { + func image(file: String, width: Int, height: Int) -> String { """ - + """ } - func svgImage(file: String, x: Int, y: Int, width: Int, height: Int) -> String { - """ - - - - """ + func svgImage(file: String, part: SVGSelection, width: Int, height: Int) -> String { + let path = "\(file)#svgView(viewBox(\(part.x),\(part.y),\(part.width),\(part.height))" + return image(file: path, width: width, height: height) } func downloadButtons(_ buttons: [(file: String, text: String, downloadName: String?)]) -> String { diff --git a/Sources/Generator/Generators/PageContentGenerator.swift b/Sources/Generator/Generators/PageContentGenerator.swift index 9d9ac3f..117271f 100644 --- a/Sources/Generator/Generators/PageContentGenerator.swift +++ b/Sources/Generator/Generators/PageContentGenerator.swift @@ -103,10 +103,14 @@ struct PageContentGenerator { if let _ = VideoType(rawValue: fileExtension) { return handleVideo(page: page, file: file, optionString: alt) } - if fileExtension == "svg" { + switch fileExtension { + case "svg": return handleSvg(page: page, file: file, area: alt) + case "gif": + return handleGif(page: page, file: file) + default: + return handleFile(page: page, file: file, fileExtension: fileExtension) } - return handleFile(page: page, file: file, fileExtension: fileExtension) } private func handleShortHandCommand(_ command: ShorthandMarkdownKey, page: Element, language: String, content: String) -> String { @@ -189,24 +193,42 @@ struct PageContentGenerator { return factory.video.generate(sources: sources, options: options) } + private func handleGif(page: Element, file: 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) + } + private func handleSvg(page: Element, 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 "" + } + let width = Int(size.width) + let height = Int(size.height) + guard let area = area else { - return factory.html.svgImage(file: file) + return factory.html.image(file: file, width: width, height: height) } let parts = area.components(separatedBy: ",").map { $0.trimmed } guard parts.count == 4, - let x = Int(parts[0].trimmed), - let y = Int(parts[1].trimmed), - let width = Int(parts[2].trimmed), - let height = Int(parts[3].trimmed) else { + 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.svgImage(file: file) + return factory.html.image(file: file, width: width, height: height) } - - return factory.html.svgImage(file: file, x: x, y: y, width: width, height: height) + let part = SVGSelection(x, y, partWidth, partHeight) + return factory.html.svgImage(file: file, part: part, width: width, height: height) } private func handleFile(page: Element, file: String, fileExtension: String) -> String { diff --git a/Sources/Generator/Processing/GenerationResultsHandler.swift b/Sources/Generator/Processing/GenerationResultsHandler.swift index d09a472..8cc91d7 100644 --- a/Sources/Generator/Processing/GenerationResultsHandler.swift +++ b/Sources/Generator/Processing/GenerationResultsHandler.swift @@ -368,7 +368,7 @@ final class GenerationResultsHandler { return scaledSize } - private func getImageSize(atPath path: String, source: String) -> NSSize? { + func getImageSize(atPath path: String, source: String) -> NSSize? { if let size = imageSizeCache[path] { return size } @@ -469,9 +469,9 @@ final class GenerationResultsHandler { add("Unwritable files", unwritableFiles) { "\($0.key) (required by \($0.value.source)): \($0.value.message)" } add("Missing linked pages", missingLinkedPages) { "\($0.key) (linked by \($0.value))" } add("Warnings", pageWarnings) { "\($0.source): \($0.message)" } + add("Generated files", generatedFiles) { $0 } add("Drafts", draftPages) { $0 } add("Empty pages", emptyPages) { "\($0.key) (from \($0.value))" } - add("Generated files", generatedFiles) { $0 } let data = lines.joined(separator: "\n").data(using: .utf8)! do { try data.createFolderAndWrite(to: file)