Generate full-screen images

This commit is contained in:
Christoph Hagen
2022-12-08 17:16:54 +01:00
parent 3bd75a63ab
commit 59667af4b0
5 changed files with 101 additions and 29 deletions

View File

@ -4,6 +4,8 @@ import Splash
struct PageContentGenerator {
private let largeImageIndicator = "*large*"
private let factory: TemplateFactory
private let swift = SyntaxHighlighter(format: HTMLOutputFormat())
@ -20,9 +22,10 @@ struct PageContentGenerator {
func generate(page: Element, language: String, content: String) -> (content: String, includesCode: Bool) {
var hasCodeContent = false
var largeImageCount = 0
let imageModifier = Modifier(target: .images) { html, markdown in
processMarkdownImage(markdown: markdown, html: html, page: page, language: language)
processMarkdownImage(markdown: markdown, html: html, page: page, language: language, largeImageCount: &largeImageCount)
}
let codeModifier = Modifier(target: .codeBlocks) { html, markdown in
if markdown.starts(with: "```swift") {
@ -74,10 +77,10 @@ struct PageContentGenerator {
return html
}
private func processMarkdownImage(markdown: Substring, html: String, page: Element, language: String) -> String {
private func processMarkdownImage(markdown: Substring, html: String, page: Element, language: String, largeImageCount: inout Int) -> String {
// Split the markdown ![alt](file title)
// There are several known shorthand commands
// For images: ![left_title](file right_title)
// 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, ...)
@ -95,7 +98,7 @@ struct PageContentGenerator {
let fileExtension = file.lastComponentAfter(".").lowercased()
if let _ = ImageType(fileExtension: fileExtension) {
return handleImage(page: page, file: file, rightTitle: title, leftTitle: alt)
return handleImage(page: page, file: file, rightTitle: title, leftTitle: alt, largeImageCount: &largeImageCount)
}
if let _ = VideoType(rawValue: fileExtension) {
return handleVideo(page: page, file: file, optionString: alt)
@ -121,22 +124,48 @@ struct PageContentGenerator {
}
}
private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?) -> String {
private func handleImage(page: Element, 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 content: [PageImageTemplate.Key : String] = [
guard createFullScreenVersion else {
let content: [PageImageTemplate.Key : String] = [
.image: file.dropAfterLast("."),
.imageExtension: file.lastComponentAfter("."),
.width: "\(Int(size.width))",
.height: "\(Int(size.height))",
.leftText: left,
.rightText: rightTitle ?? ""]
return factory.image.generate(content)
}
results.requireOriginalSizeImages(
source: imagePath,
destination: imagePath,
requiredBy: page.path)
largeImageCount += 1
let content: [EnlargeableImageTemplate.Key : String] = [
.image: file.dropAfterLast("."),
.imageExtension: file.lastComponentAfter("."),
.width: "\(Int(size.width))",
.height: "\(Int(size.height))",
.leftText: leftTitle ?? "",
.rightText: rightTitle ?? ""]
return factory.image.generate(content)
.leftText: left,
.rightText: rightTitle ?? "",
.number: "\(largeImageCount)"]
return factory.largeImage.generate(content)
}
private func handleVideo(page: Element, file: String, optionString: String?) -> String {