Skip to content

Commit

Permalink
chore: Disable auto escaping for pebble (keeps symbols like < >)
Browse files Browse the repository at this point in the history
fix: Not deep inheriting includes, handle cyclic includes case (earlier wins)
  • Loading branch information
0ffz committed May 28, 2024
1 parent 0bb8215 commit 2a13869
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
version=3.0.0-alpha.1
version=3.0.0-alpha.2
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ class ConfigCommand : CliktCommand(name = "config", help = "Syncs local config f
t.println("${MSG.error} Config not found: $include")
return
})
val included = inventory.getOrCreateConfigs(config.include)
val reduced = ConfigDefinition.reduce(included + config)
val included = inventory.getOrCreateConfigs(include)
val reduced = ConfigDefinition.reduce(included)

t.println("${MSG.info} Included paths: ${reduced.copyPaths}")
val paths = reduced.copyPaths.map { sourceRoot / it }

val tree = ConfigTreeBuilder()
Expand Down
18 changes: 13 additions & 5 deletions src/main/kotlin/com/mineinabyss/keepup/config/Inventory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ import kotlinx.serialization.builtins.serializer
class Inventory(
val configs: Map<String, ConfigDefinition>,
) {
fun getOrCreateConfigs(names: Iterable<String>): List<ConfigDefinition> {
fun getOrCreateConfigs(name: String): List<ConfigDefinition> {
val global = configs["global"]
return listOfNotNull(global) + names.map {
configs[it] ?: ConfigDefinition(
copyPaths = listOf(it),
)
val includes = getDeepIncludes(names = listOf(name)).distinct().reversed()
return listOfNotNull(global) + includes.map {
configs[it] ?: ConfigDefinition(copyPaths = listOf(it))
}
}

tailrec fun getDeepIncludes(acc: MutableList<String> = mutableListOf(), names: List<String>): List<String> {
if (names.isEmpty()) return acc
val namesSet = names.toSet()
val nonCyclicNames = names - acc.filter { it in namesSet }.toSet()
acc.addAll(nonCyclicNames)
val newNames = nonCyclicNames.flatMap { configs[it]?.include?.reversed() ?: emptyList() }
return getDeepIncludes(acc, newNames)
}

object Serializer : InnerSerializer<Map<String, ConfigDefinition>, Inventory>(
"Inventory",
MapSerializer(String.serializer(), ConfigDefinition.serializer()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.pebbletemplates.pebble.PebbleEngine
import java.io.StringWriter

class Templater {
val pebble = PebbleEngine.Builder().build()
val pebble = PebbleEngine.Builder().autoEscaping(false).build()

fun template(input: String, variables: Map<String, Any?>): Result<String> {
val writer = StringWriter()
Expand Down

0 comments on commit 2a13869

Please sign in to comment.