Skip to content

Commit

Permalink
Add new helper method for escaping YAML text
Browse files Browse the repository at this point in the history
... which can be useful when importing user strings in conf.yaml files,
etc.

Bug #14
  • Loading branch information
bitspittle committed Oct 19, 2024
1 parent ccf3b3b commit 821ba6b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.varabyte.kobweb.cli.create.freemarker.methods.IsPositiveNumberMethod
import com.varabyte.kobweb.cli.create.freemarker.methods.IsYesNoMethod
import com.varabyte.kobweb.cli.create.freemarker.methods.NotMethod
import com.varabyte.kobweb.cli.create.freemarker.methods.PackageToPathMethod
import com.varabyte.kobweb.cli.create.freemarker.methods.EscapeYamlStringMethod
import com.varabyte.kobweb.cli.create.freemarker.methods.YesNoToBoolMethod
import com.varabyte.kobweb.common.error.KobwebException
import com.varabyte.kobweb.common.path.invariantSeparatorsPath
Expand Down Expand Up @@ -59,6 +60,7 @@ class FreemarkerState(private val src: Path, private val dest: Path) {
// endregion

// region Converters
"escapeYamlString" to EscapeYamlStringMethod(), // Added in 0.9.17
"fileToTitle" to FileToTitleMethod(),
"fileToPackage" to FileToPackageMethod(),
"not" to NotMethod(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.varabyte.kobweb.cli.create.freemarker.methods

/**
* Utility method for escaping relevant control characters before inserting text into a yaml string.
*/
class EscapeYamlStringMethod : SingleArgMethodModel() {
override fun exec(value: String): String {
return value
// Replace backslash first, else we'd modify any quote substitutions (e.g. '"' -> '\"' -> '\\"')
.replace("\\", """\\""")
.replace("\"", """\"""")
}
}

0 comments on commit 821ba6b

Please sign in to comment.