Add convenience box command

This commit is contained in:
Christoph Hagen 2022-09-18 17:21:57 +02:00
parent 396e03279f
commit 53500c31f6
5 changed files with 28 additions and 12 deletions

View File

@ -76,6 +76,7 @@ struct PageContentGenerator {
// For videos: ![option1,option2,...](file)
// For svg with custom area: ![x,y,width,height](file.svg)
// For downloads: ![download](file1, text1; file2, text2, ...)
// For a simple boxes: ![box](title;body)
// External pages: ![external](url1, text1; url2, text2, ...)
let fileAndTitle = markdown.between("(", and: ")")
let alt = markdown.between("[", and: "]").nonEmpty
@ -86,6 +87,8 @@ struct PageContentGenerator {
return handleExternalButtons(page: page, content: fileAndTitle)
case "html":
return handleExternalHTML(page: page, file: fileAndTitle)
case "box":
return handleSimpleBox(page: page, content: fileAndTitle)
default:
break
}
@ -231,4 +234,15 @@ struct PageContentGenerator {
return ""
}
}
private func handleSimpleBox(page: Element, content: String) -> String {
let parts = content.components(separatedBy: ";")
guard parts.count > 1 else {
log.add(error: "Invalid box specification", source: page.path)
return ""
}
let title = parts[0]
let text = parts.dropFirst().joined(separator: ";")
return factory.makePlaceholder(title: title, text: text)
}
}

View File

@ -68,7 +68,7 @@ struct PageGenerator {
} else {
let (content, includesCode) = PageContentGenerator(factory: factory.factory)
.generate(page: page, language: language, content: metadata.placeholderText)
let placeholder = factory.makePlaceholder(title: metadata.placeholderTitle, text: content)
let placeholder = factory.factory.makePlaceholder(title: metadata.placeholderTitle, text: content)
return (placeholder, includesCode, true)
}
}

View File

@ -1,13 +1,13 @@
import Foundation
struct PlaceholderTemplate: Template {
struct BoxTemplate: Template {
enum Key: String, CaseIterable {
case title = "TITLE"
case text = "TEXT"
}
static let templateName = "empty.html"
static let templateName = "box.html"
var raw: String
}

View File

@ -75,13 +75,7 @@ struct LocalizedSiteTemplate {
// MARK: Content
func makePlaceholder(metadata: Element.LocalizedMetadata) -> String {
makePlaceholder(title: metadata.placeholderTitle, text: metadata.placeholderText)
}
func makePlaceholder(title: String, text: String) -> String {
factory.placeholder.generate([
.title: title,
.text: text])
factory.makePlaceholder(title: metadata.placeholderTitle, text: metadata.placeholderText)
}
func makeBackLink(text: String, language: String) -> String {

View File

@ -16,7 +16,7 @@ final class TemplateFactory {
let overviewSectionClean: OverviewSectionCleanTemplate
let placeholder: PlaceholderTemplate
let box: BoxTemplate
// MARK: Thumbnails
@ -64,7 +64,7 @@ final class TemplateFactory {
self.topBar = try .init(in: templateFolder)
self.overviewSection = try .init(in: templateFolder)
self.overviewSectionClean = try .init(in: templateFolder)
self.placeholder = try .init(in: templateFolder)
self.box = try .init(in: templateFolder)
self.largeThumbnail = try .init(in: templateFolder)
self.squareThumbnail = try .init(in: templateFolder)
self.smallThumbnail = try .init(in: templateFolder)
@ -75,4 +75,12 @@ final class TemplateFactory {
self.video = try .init(in: templateFolder)
self.html = .init()
}
// MARK: Convenience methods
func makePlaceholder(title: String, text: String) -> String {
box.generate([
.title: title,
.text: text])
}
}