X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FRenderFilter.kt;h=00015bb0783a973e9da5d7236fa98070477bc8ff;hb=a6486d6aab8e425cc235ac41f8159602ebdb8e3a;hp=2c38e58d0cf2f47af4bf375994dfc327832ee0e5;hpb=36e854bb84277c97d7934535e534ca6e0fe22698;p=Sone.git
diff --git a/src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt
index 2c38e58..00015bb 100644
--- a/src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt
+++ b/src/main/kotlin/net/pterodactylus/sone/template/RenderFilter.kt
@@ -1,82 +1,30 @@
package net.pterodactylus.sone.template
-import net.pterodactylus.sone.core.Core
-import net.pterodactylus.sone.text.FreemailPart
-import net.pterodactylus.sone.text.FreenetLinkPart
-import net.pterodactylus.sone.text.LinkPart
+import net.pterodactylus.sone.database.*
+import net.pterodactylus.sone.text.*
import net.pterodactylus.sone.text.Part
-import net.pterodactylus.sone.text.PlainTextPart
-import net.pterodactylus.sone.text.PostPart
-import net.pterodactylus.sone.text.SonePart
-import net.pterodactylus.sone.text.SoneTextParser
-import net.pterodactylus.sone.text.SoneTextParserContext
-import net.pterodactylus.util.template.Filter
-import net.pterodactylus.util.template.TemplateContext
-import net.pterodactylus.util.template.TemplateContextFactory
-import net.pterodactylus.util.template.TemplateParser
-import java.io.StringReader
-import java.io.StringWriter
-import java.io.Writer
-import java.net.URLEncoder
-import java.util.ArrayList
+import net.pterodactylus.sone.utils.*
+import net.pterodactylus.util.template.*
+import java.io.*
+import java.net.*
/**
* Renders a number of pre-parsed [Part] into a [String].
- *
- * @author [David âBombeâ Roden](mailto:bombe@pterodactylus.net)
*/
-class RenderFilter(private val core: Core, private val templateContextFactory: TemplateContextFactory) : Filter {
+class RenderFilter(private val soneProvider: SoneProvider, private val soneTextParser: SoneTextParser, htmlFilter: HtmlFilter) : Filter {
- companion object {
- private val plainTextTemplate = TemplateParser.parse(StringReader("<%text|html>"))
- private val linkTemplate = TemplateParser.parse(StringReader("\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html>"))
+ private val templateContextFactory = TemplateContextFactory().apply {
+ addFilter("html", htmlFilter)
}
override fun format(templateContext: TemplateContext?, data: Any?, parameters: MutableMap?): Any? {
@Suppress("UNCHECKED_CAST")
- val parts = getPartsToRender(parameters, data as? Iterable ?: return null)
+ val parts = data as? Iterable ?: return null
val parsedTextWriter = StringWriter()
render(parsedTextWriter, parts)
return parsedTextWriter.toString()
}
- private fun Map.parseInt(key: String) = this[key]?.toString()?.toInt()
-
- private fun getPartsToRender(parameters: MutableMap?, parts: Iterable): Iterable {
- val length = parameters?.parseInt("length") ?: -1
- val cutOffLength = parameters?.parseInt("cut-off-length") ?: length
- if (length > -1) {
- var allPartsLength = 0
- val shortenedParts = ArrayList()
- for (part in parts) {
- if (part is PlainTextPart) {
- val longText = part.text
- if (allPartsLength < cutOffLength) {
- if (allPartsLength + longText.length > cutOffLength) {
- shortenedParts.add(PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "â¦"))
- } else {
- shortenedParts.add(part)
- }
- }
- allPartsLength += longText.length
- } else if (part is LinkPart) {
- if (allPartsLength < cutOffLength) {
- shortenedParts.add(part)
- }
- allPartsLength += part.text.length
- } else {
- if (allPartsLength < cutOffLength) {
- shortenedParts.add(part)
- }
- }
- }
- if (allPartsLength >= length) {
- return shortenedParts
- }
- }
- return parts
- }
-
private fun render(writer: Writer, parts: Iterable) {
parts.forEach { render(writer, it) }
}
@@ -90,7 +38,6 @@ class RenderFilter(private val core: Core, private val templateContextFactory: T
is SonePart -> render(writer, part)
is PostPart -> render(writer, part)
is FreemailPart -> render(writer, part)
- is Iterable<*> -> render(writer, part as Iterable)
}
}
@@ -101,14 +48,14 @@ class RenderFilter(private val core: Core, private val templateContextFactory: T
}
private fun render(writer: Writer, freenetLinkPart: FreenetLinkPart) {
- renderLink(writer, "/${freenetLinkPart.link}", freenetLinkPart.text, freenetLinkPart.title, if (freenetLinkPart.isTrusted) "freenet-trusted" else "freenet")
+ renderLink(writer, "/${freenetLinkPart.link}", freenetLinkPart.text, freenetLinkPart.title, if (freenetLinkPart.trusted) "freenet-trusted" else "freenet")
}
private fun render(writer: Writer, linkPart: LinkPart) {
renderLink(writer, "/external-link/?_CHECKED_HTTP_=${linkPart.link.urlEncode()}", linkPart.text, linkPart.title, "internet")
}
- private fun String.urlEncode() = URLEncoder.encode(this, "UTF-8")!!
+ private fun String.urlEncode(): String = URLEncoder.encode(this, "UTF-8")
private fun render(writer: Writer, sonePart: SonePart) {
if (sonePart.sone.name != null) {
@@ -119,9 +66,8 @@ class RenderFilter(private val core: Core, private val templateContextFactory: T
}
private fun render(writer: Writer, postPart: PostPart) {
- val parser = SoneTextParser(core, core)
val parserContext = SoneTextParserContext(postPart.post.sone)
- val parts = parser.parse(postPart.post.text, parserContext)
+ val parts = soneTextParser.parse(postPart.post.text, parserContext)
val excerpt = StringBuilder()
for (part in parts) {
excerpt.append(part.text)
@@ -140,8 +86,8 @@ class RenderFilter(private val core: Core, private val templateContextFactory: T
}
private fun render(writer: Writer, freemailPart: FreemailPart) {
- val sone = core.getSone(freemailPart.identityId)
- val soneName = sone.transform(SoneAccessor::getNiceName).or(freemailPart.identityId)
+ val sone = soneProvider.getSone(freemailPart.identityId)
+ val soneName = sone?.let(SoneAccessor::getNiceName) ?: freemailPart.identityId
renderLink(writer,
"/Freemail/NewMessage?to=${freemailPart.identityId}",
"${freemailPart.emailLocalPart}@$soneName.freemail",
@@ -159,3 +105,6 @@ class RenderFilter(private val core: Core, private val templateContextFactory: T
}
}
+
+private val plainTextTemplate = "<%text|html>".asTemplate()
+private val linkTemplate = "\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html>".asTemplate()