X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftext%2FSoneTextParser.kt;h=6af0fded74d25da45c2c9e5f52c8f24a48083017;hb=0a8bee89e07058cf46f1966b22ed264c8b8a24fc;hp=24b04faa3028db2e0f217fb6dc4fa22deda0671f;hpb=5c5bee980f9cab5792e34d1c9840f73b8b191830;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt index 24b04fa..6af0fde 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt @@ -32,7 +32,7 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? else LinkType.values() .mapNotNull { it.findNext(remainder.second) } - .minBy { it.position } + .minByOrNull { it.position } .let { when { it == null -> PlainTextPart(remainder.second) to "" @@ -66,13 +66,15 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? } SSK, USK -> try { - FreenetURI(linkWithoutBacklink).let { uri -> - uri.allMetaStrings - ?.takeIf { (it.size > 1) || ((it.size == 1) && (it.single() != "")) } - ?.lastOrNull() - ?: uri.docName - ?: "${uri.keyType}@${uri.routingKey.asFreenetBase64}" - }.let { FreenetLinkPart(linkWithoutBacklink.removeSuffix("/"), it, trusted = context?.routingKey?.contentEquals(FreenetURI(linkWithoutBacklink).routingKey) == true) } + FreenetURI(linkWithoutBacklink) + .workaroundForFaultyConstructorInFred1485AndBelow() + .let { uri -> + uri.allMetaStrings + ?.takeIf { (it.size > 1) || ((it.size == 1) && (it.single() != "")) } + ?.lastOrNull() + ?: uri.docName + ?: "${uri.keyType}@${uri.routingKey.asFreenetBase64}" + }.let { FreenetLinkPart(linkWithoutBacklink.removeSuffix("/"), it, trusted = context?.routingKey?.contentEquals(FreenetURI(linkWithoutBacklink).routingKey) == true) } } catch (e: MalformedURLException) { PlainTextPart(linkWithoutBacklink) } @@ -93,6 +95,9 @@ class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider? } +private fun FreenetURI.workaroundForFaultyConstructorInFred1485AndBelow() = + also { if (it.routingKey == null) throw MalformedURLException("SSK/USK without routing key") } + private fun List.mergeMultipleEmptyLines() = fold(emptyList()) { previous, current -> if (previous.isEmpty()) { previous + current