From: LaniusTrolling Date: Sun, 11 May 2025 19:25:39 +0000 (-0400) Subject: Add [random] tag to BBCode X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=06620281457496b8d1ee87f6b78a4b84a4739a5c;p=factbooks Add [random] tag to BBCode --- diff --git a/src/main/kotlin/info/mechyrdia/lore/ParserPreprocess.kt b/src/main/kotlin/info/mechyrdia/lore/ParserPreprocess.kt index 188e89c..0b934f9 100644 --- a/src/main/kotlin/info/mechyrdia/lore/ParserPreprocess.kt +++ b/src/main/kotlin/info/mechyrdia/lore/ParserPreprocess.kt @@ -450,6 +450,23 @@ enum class PreProcessorTags(val type: PreProcessorLexerTag) { PreProcessorTemplateLoader.loadTemplate(env.processTree(subNodes).treeToText()) } }), + RANDOM(PreProcessorLexerTag { env, param, subNodes -> + param.forbidParam("random") { + subNodes + .flatMap { subNode -> + if (subNode is ParserTreeNode.Tag && subNode isTag "item") + listOf(subNode) + else + env.processNode(subNode) + } + .filterIsInstance() + .filter { it isTag "item" } + .randomOrNull() + ?.subNodes + ?.let { env.processTree(it) } + ?: formatErrorToParserTree("At least one [item] subtag required inside body of [random] tag") + } + }), ; companion object { @@ -469,13 +486,15 @@ suspend fun ParserTree.preProcess(context: PreProcessorContext): ParserTree { } fun Exception.renderInBBCode(): ParserTree = listOf( - ParserTreeNode.Tag("error", null, listOf( - ParserTreeNode.Tag("b", null, listOf(ParserTreeNode.Text("${this::class.qualifiedName}: $message"))), - ParserTreeNode.LineBreak, - ParserTreeNode.Tag("ul", null, - stackTraceToString().split(System.lineSeparator()).map { - ParserTreeNode.Tag("li", null, listOf(ParserTreeNode.Text(it))) - } - ), - )), + ParserTreeNode.Tag( + "error", null, listOf( + ParserTreeNode.Tag("b", null, listOf(ParserTreeNode.Text("${this::class.qualifiedName}: $message"))), + ParserTreeNode.LineBreak, + ParserTreeNode.Tag( + "ul", null, + stackTraceToString().split(System.lineSeparator()).map { + ParserTreeNode.Tag("li", null, listOf(ParserTreeNode.Text(it))) + } + ), + )), )