Skip to content

Commit

Permalink
Improve RenderWrapper#fromResourceName
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Jul 17, 2024
1 parent 3c17a33 commit 563b2d6
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eu.iamgio.quarkdown.rendering.wrapper

import eu.iamgio.quarkdown.util.replace
import java.io.InputStreamReader

/**
* A code wrapper that adds static content to the output code of the rendering stage, and supports injection of values via placeholder keys.
Expand Down Expand Up @@ -76,7 +75,8 @@ class RenderWrapper(private val code: String) {
// Delimiters are defined as [[if:NAME]]...[[endif:NAME]] in the template files.
conditionals.forEach { (placeholder, value) ->
// Regex to find conditional fragments.
val regex = "\\[\\[if:$placeholder]]((.|\\R)+?)\\[\\[endif:$placeholder]]\\R?".toRegex(RegexOption.MULTILINE)
val regex =
"\\[\\[if:$placeholder]]((.|\\R)+?)\\[\\[endif:$placeholder]]\\R?".toRegex(RegexOption.MULTILINE)
// If there is a match:
// Keep the inner content (without the delimiters) if the conditional value is true, remove it otherwise.
regex.findAll(this).forEach { match ->
Expand All @@ -103,10 +103,10 @@ class RenderWrapper(private val code: String) {
*/
fun fromResourceName(name: String) =
RenderWrapper(
InputStreamReader(
Companion::class.java.getResourceAsStream(name)
?: throw IllegalStateException("Cannot find wrapper resource $name."),
).readText(),
Companion::class.java.getResourceAsStream(name)
?.reader()
?.readText()
?: throw IllegalStateException("Cannot find wrapper resource $name."),
)
}
}

0 comments on commit 563b2d6

Please sign in to comment.