Add toAbsoluteUrl util function
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
Reference in New Issue
Block a user