Skip to content

Commit

Permalink
Set paperwhite+latex as overridable default theme
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Oct 16, 2024
1 parent f878d0b commit 13fd995
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions core/src/main/kotlin/eu/iamgio/quarkdown/document/DocumentTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/kotlin/eu/iamgio/quarkdown/HtmlPostRendererTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<html><head></head><body></body></html>"

val resources = postRenderer.generateResources(html)
assertEquals(3, resources.size)

val themeGroup = resources.filterIsInstance<OutputResourceGroup>().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,
)
}
}
}

0 comments on commit 13fd995

Please sign in to comment.