Add toAbsoluteUrl util function

pull/17/head
Koitharu 4 years ago
parent b495e5e457
commit 44e6842025
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -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)
}
this.startsWith("/") -> buildString {
append("https")
append("://")
protected fun String.withDomain(subdomain: String? = null): String {
var domain = getDomain()
if (subdomain != null) {
append(subdomain)
append('.')
append(getDomain().removePrefix("www."))
} else {
append(getDomain())
}
append(this@withDomain)
domain = subdomain + "." + domain.removePrefix("www.")
}
else -> this
return toAbsoluteUrl(domain)
}
protected fun parseFailed(message: String? = null): Nothing {

@ -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()) {

Loading…
Cancel
Save