X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FParserFilter.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FParserFilter.kt;h=4d2f9c1a123923defceb698191e5d6a386ad6a45;hb=f6bb52d378645bf821febd6696bc69f250a92280;hp=0000000000000000000000000000000000000000;hpb=8e2fb40c556d340c5d6a8d78f8c38c9dc14d8365;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt new file mode 100644 index 0000000..4d2f9c1 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt @@ -0,0 +1,28 @@ +package net.pterodactylus.sone.template + +import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.text.Part +import net.pterodactylus.sone.text.SoneTextParser +import net.pterodactylus.sone.text.SoneTextParserContext +import net.pterodactylus.util.template.Filter +import net.pterodactylus.util.template.TemplateContext + +/** + * Parses a [String] into a number of [Part]s. + */ +class ParserFilter(private val core: Core, private val soneTextParser: SoneTextParser) : Filter { + + override fun format(templateContext: TemplateContext?, data: Any?, parameters: MutableMap?): Any? { + val text = data?.toString() ?: return listOf() + val soneParameter = parameters?.get("sone") + val sone = when (soneParameter) { + is String -> core.getSone(soneParameter).orNull() + is Sone -> soneParameter + else -> null + } + val context = SoneTextParserContext(sone as? Sone) + return soneTextParser.parse(text, context) + } + +}