diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/document/DocumentTheme.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/document/DocumentTheme.kt index 22901b3f..0a3fa5b5 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/document/DocumentTheme.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/document/DocumentTheme.kt @@ -10,3 +10,15 @@ data class DocumentTheme( val color: String?, val layout: String?, ) + +/** + * Given [this] theme with nullable components, merges it with a default theme in order to fill in the missing components. + * If [this] is `null` itself, the default theme is returned. + * @param default default theme + * @return a new theme with all components filled in, either from [this] (higher priority) or [default] (fill) + */ +fun DocumentTheme?.orDefault(default: DocumentTheme) = + DocumentTheme( + color = this?.color ?: default.color, + layout = this?.layout ?: default.layout, + ) diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/HtmlPostRenderer.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/HtmlPostRenderer.kt index 39134e59..33a554f2 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/HtmlPostRenderer.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/HtmlPostRenderer.kt @@ -3,6 +3,7 @@ package eu.iamgio.quarkdown.rendering.html import eu.iamgio.quarkdown.context.Context import eu.iamgio.quarkdown.document.DocumentTheme import eu.iamgio.quarkdown.document.DocumentType +import eu.iamgio.quarkdown.document.orDefault import eu.iamgio.quarkdown.media.storage.options.MediaStorageOptions import eu.iamgio.quarkdown.media.storage.options.ReadOnlyMediaStorageOptions import eu.iamgio.quarkdown.pipeline.output.ArtifactType @@ -14,6 +15,13 @@ import eu.iamgio.quarkdown.rendering.PostRenderer import eu.iamgio.quarkdown.rendering.wrapper.RenderWrapper import eu.iamgio.quarkdown.rendering.wrapper.TemplatePlaceholders +// Default theme components to use if not specified by the user. +private val DEFAULT_THEME = + DocumentTheme( + color = "paperwhite", + layout = "latex", + ) + /** * A [PostRenderer] that injects content into an HTML template, which supports out of the box: * - RevealJS for slides rendering; @@ -58,13 +66,16 @@ class HtmlPostRenderer(private val context: Context) : PostRenderer { type = ArtifactType.HTML, ) + // The user-set theme is merged with the default one + // to fill in the missing components with the default ones. + val theme = context.documentInfo.theme.orDefault(DEFAULT_THEME) // A group of CSS theme resources is added to the output resources. // Theme components (global style, color scheme and layout format) are stored in a single group (directory) // and linked via @import statements in a theme.css file. this += OutputResourceGroup( name = "theme", - resources = retrieveThemeComponentsArtifacts(context.documentInfo.theme), + resources = retrieveThemeComponentsArtifacts(theme), ) // A group of JS script resources is added to the output resources. diff --git a/core/src/test/kotlin/eu/iamgio/quarkdown/HtmlPostRendererTest.kt b/core/src/test/kotlin/eu/iamgio/quarkdown/HtmlPostRendererTest.kt index 3c163a36..817ba302 100644 --- a/core/src/test/kotlin/eu/iamgio/quarkdown/HtmlPostRendererTest.kt +++ b/core/src/test/kotlin/eu/iamgio/quarkdown/HtmlPostRendererTest.kt @@ -276,4 +276,35 @@ class HtmlPostRendererTest { assertTrue("code" !in scripts) } } + + @Test + fun `resource generation, default theme`() { + val context = MutableContext(QuarkdownFlavor) + + val postRenderer = HtmlPostRenderer(context) + val html = "" + + val resources = postRenderer.generateResources(html) + assertEquals(3, resources.size) + + val themeGroup = resources.filterIsInstance().first { it.name == "theme" } + themeGroup.resources.map { it.name }.let { themes -> + assertTrue("paperwhite" in themes) // Default + assertTrue("latex" in themes) // Default + assertTrue("global" in themes) + assertTrue("theme" in themes) + } + + (themeGroup.resources.first { it.name == "theme" } as TextOutputArtifact).let { + assertEquals(ArtifactType.CSS, it.type) + assertEquals( + """ + @import url('global.css'); + @import url('latex.css'); + @import url('paperwhite.css'); + """.trimIndent(), + it.content, + ) + } + } }