From a72774c302cdb2c35508380fb2ec445be2705efe Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 2 Sep 2022 16:42:41 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Move=20extension=20methods?= =?utf8?q?=20out=20of=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/core/DefaultElementLoader.kt | 49 +++++++++++----------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt index 409d18c..2849029 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/DefaultElementLoader.kt @@ -58,29 +58,6 @@ class DefaultElementLoader(private val freenetInterface: FreenetInterface, ticke } } - private val String?.emptyToNull get() = if (this == "") null else this - - private val Document.metaDescription: String? - get() = head().getElementsByTag("meta") - .map { it.attr("name") to it.attr("content") } - .firstOrNull { it.first == "description" } - ?.second - - private val Document.firstNonHeadingParagraph: String? - get() = body().children() - .filter { it.children().all { it is TextNode } } - .map { it to it.text() } - .filterNot { it.second == "" } - .firstOrNull { !it.first.tagName().startsWith("h", ignoreCase = true) } - ?.second - - private val Int.human get() = when (this) { - in 0..1023 -> "$this B" - in 1024..1048575 -> "${this / 1024} KiB" - in 1048576..1073741823 -> "${this / 1048576} MiB" - else -> "${this / 1073741824} GiB" - } - override fun failed(uri: FreenetURI) { failureCache.put(uri.toString().decode().normalize(), true) removeLoadingLink(uri) @@ -110,7 +87,29 @@ class DefaultElementLoader(private val freenetInterface: FreenetInterface, ticke return LinkedElement(link, loading = true) } - private fun String.decode() = URLDecoder.decode(this, "UTF-8")!! - private fun String.normalize() = Normalizer.normalize(this, Normalizer.Form.NFC)!! +} + +private fun String.decode() = URLDecoder.decode(this, "UTF-8")!! +private fun String.normalize() = Normalizer.normalize(this, Normalizer.Form.NFC)!! +private val String?.emptyToNull get() = if (this == "") null else this + +private val Document.metaDescription: String? + get() = head().getElementsByTag("meta") + .map { it.attr("name") to it.attr("content") } + .firstOrNull { it.first == "description" } + ?.second + +private val Document.firstNonHeadingParagraph: String? + get() = body().children() + .filter { it.children().all { it is TextNode } } + .map { it to it.text() } + .filterNot { it.second == "" } + .firstOrNull { !it.first.tagName().startsWith("h", ignoreCase = true) } + ?.second +private val Int.human get() = when (this) { + in 0..1023 -> "$this B" + in 1024..1048575 -> "${this / 1024} KiB" + in 1048576..1073741823 -> "${this / 1048576} MiB" + else -> "${this / 1073741824} GiB" } -- 2.7.4