From 44e68420255485dc2e46e6f1de79c0d5636fca83 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 9 May 2022 12:14:11 +0300 Subject: [PATCH] Add toAbsoluteUrl util function --- .../koitharu/kotatsu/parsers/MangaParser.kt | 24 +++++-------------- .../koitharu/kotatsu/parsers/util/ParseExt.kt | 6 +++++ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt index 9793f162..7b7c889b 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/MangaParser.kt @@ -3,6 +3,7 @@ package org.koitharu.kotatsu.parsers import org.koitharu.kotatsu.parsers.config.ConfigKey import org.koitharu.kotatsu.parsers.exception.ParseException import org.koitharu.kotatsu.parsers.model.* +import org.koitharu.kotatsu.parsers.util.toAbsoluteUrl abstract class MangaParser(val source: MangaSource) { @@ -61,25 +62,12 @@ abstract class MangaParser(val source: MangaSource) { return h } - protected fun String.withDomain(subdomain: String? = null) = when { - this.startsWith("//") -> buildString { - append("https") - append(":") - append(this@withDomain) + protected fun String.withDomain(subdomain: String? = null): String { + var domain = getDomain() + if (subdomain != null) { + domain = subdomain + "." + domain.removePrefix("www.") } - this.startsWith("/") -> buildString { - append("https") - append("://") - if (subdomain != null) { - append(subdomain) - append('.') - append(getDomain().removePrefix("www.")) - } else { - append(getDomain()) - } - append(this@withDomain) - } - else -> this + return toAbsoluteUrl(domain) } protected fun parseFailed(message: String? = null): Nothing { diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/ParseExt.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/ParseExt.kt index 0ef9eabe..d729d9cf 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/ParseExt.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/ParseExt.kt @@ -80,6 +80,12 @@ fun String.toRelativeUrl(domain: String): String { return replace(Regex("^[^/]{2,6}://${Regex.escape(domain)}+/", RegexOption.IGNORE_CASE), "/") } +fun String.toAbsoluteUrl(domain: String): String = when { + this.startsWith("//") -> "https:$this" + this.startsWith("/") -> "https://$domain$this" + else -> this +} + fun Element.relUrl(attributeKey: String): String { val attr = attr(attributeKey).trim() if (attr.isEmpty()) {