diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/CssBuilder.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/CssBuilder.kt index 33332f8d..e5631f54 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/CssBuilder.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/CssBuilder.kt @@ -17,7 +17,7 @@ class CssBuilder { * @param value CSS entry value * @return this for concatenation */ - fun entry( + private fun entry( key: String, value: String?, ) = apply { @@ -30,13 +30,31 @@ class CssBuilder { * @param value CSS entry value * @return this for concatenation */ - fun entry( + private fun entry( key: String, value: RenderRepresentable?, ) = entry(key, value?.asCSS) + /** + * Shorthand syntactic sugar for [entry]. + * @see entry + */ + infix fun String.value(value: RenderRepresentable?) = entry(this, value) + /** * @return a string representation of the CSS entries contained within this builder */ fun build() = entries.entries.joinToString(separator = " ") { "${it.key}: ${it.value};" } } + +/** + * Example usage: + * ``` + * val css = css { + * "color" value Color(255, 0, 0) + * "font-size" value Size(16, SizeUnit.PX) + * } + * ``` + * @return a string representation of CSS entries contained within the builder. + */ +fun css(init: CssBuilder.() -> Unit): String = CssBuilder().apply(init).build() diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/QuarkdownHtmlNodeRenderer.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/QuarkdownHtmlNodeRenderer.kt index 8ab0fd04..5a036d5a 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/QuarkdownHtmlNodeRenderer.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/rendering/html/QuarkdownHtmlNodeRenderer.kt @@ -62,11 +62,11 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context .attribute("class", "stack stack-" + node.orientation.asCSS) .attribute( "style", - CssBuilder() - .entry("justify-content", node.mainAxisAlignment.asCSS) - .entry("align-items", node.crossAxisAlignment.asCSS) - .entry("gap", node.gap) - .build(), + css { + "justify-content" value node.mainAxisAlignment + "align-items" value node.crossAxisAlignment + "gap" value node.gap + }, ) .build() } @@ -87,12 +87,11 @@ class QuarkdownHtmlNodeRenderer(context: Context) : BaseHtmlNodeRenderer(context // Box style. optionalAttribute( "style", - CssBuilder() - .entry("padding", node.padding) - .entry("background-color", node.backgroundColor) - .entry("color", node.foregroundColor) - .build() - .takeUnless { it.isEmpty() }, + css { + "padding" value node.padding + "background-color" value node.backgroundColor + "color" value node.foregroundColor + }.takeUnless { it.isEmpty() }, ) }