From 53500c31f6f5a66dc04798eeb19880c4fb454a14 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sun, 18 Sep 2022 17:21:57 +0200 Subject: [PATCH] Add convenience box command --- .../Generator/Generators/MarkdownProcessor.swift | 14 ++++++++++++++ Sources/Generator/Generators/PageGenerator.swift | 2 +- ...PlaceholderTemplate.swift => BoxTemplate.swift} | 4 ++-- .../Templates/Filled/LocalizedSiteTemplate.swift | 8 +------- Sources/Generator/Templates/TemplateFactory.swift | 12 ++++++++++-- 5 files changed, 28 insertions(+), 12 deletions(-) rename Sources/Generator/Templates/Elements/{PlaceholderTemplate.swift => BoxTemplate.swift} (63%) diff --git a/Sources/Generator/Generators/MarkdownProcessor.swift b/Sources/Generator/Generators/MarkdownProcessor.swift index 58cd0b3..e30fb03 100644 --- a/Sources/Generator/Generators/MarkdownProcessor.swift +++ b/Sources/Generator/Generators/MarkdownProcessor.swift @@ -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) + } } diff --git a/Sources/Generator/Generators/PageGenerator.swift b/Sources/Generator/Generators/PageGenerator.swift index 39928b6..d935e3c 100644 --- a/Sources/Generator/Generators/PageGenerator.swift +++ b/Sources/Generator/Generators/PageGenerator.swift @@ -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) } } diff --git a/Sources/Generator/Templates/Elements/PlaceholderTemplate.swift b/Sources/Generator/Templates/Elements/BoxTemplate.swift similarity index 63% rename from Sources/Generator/Templates/Elements/PlaceholderTemplate.swift rename to Sources/Generator/Templates/Elements/BoxTemplate.swift index 1e1cd5d..c2de89e 100644 --- a/Sources/Generator/Templates/Elements/PlaceholderTemplate.swift +++ b/Sources/Generator/Templates/Elements/BoxTemplate.swift @@ -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 } diff --git a/Sources/Generator/Templates/Filled/LocalizedSiteTemplate.swift b/Sources/Generator/Templates/Filled/LocalizedSiteTemplate.swift index 559e45d..4c90a82 100644 --- a/Sources/Generator/Templates/Filled/LocalizedSiteTemplate.swift +++ b/Sources/Generator/Templates/Filled/LocalizedSiteTemplate.swift @@ -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 { diff --git a/Sources/Generator/Templates/TemplateFactory.swift b/Sources/Generator/Templates/TemplateFactory.swift index cbb0e73..5839d99 100644 --- a/Sources/Generator/Templates/TemplateFactory.swift +++ b/Sources/Generator/Templates/TemplateFactory.swift @@ -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]) + } }