Generate full-screen images
This commit is contained in:
@ -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 
|
||||
// There are several known shorthand commands
|
||||
// For images: 
|
||||
// For images: 
|
||||
// For videos: 
|
||||
// For svg with custom area: 
|
||||
// For downloads: 
|
||||
@ -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 {
|
||||
|
Reference in New Issue
Block a user