This commit is contained in:
Horis
2023-03-15 11:48:30 +08:00
parent b82f18540b
commit 51d6662d7c
3 changed files with 27 additions and 9 deletions
@@ -201,11 +201,18 @@ class AnalyzeRule(
return getString(ruleList, mContent, isUrl)
}
fun getString(ruleStr: String?, unescape: Boolean): String {
if (TextUtils.isEmpty(ruleStr)) return ""
val ruleList = splitSourceRule(ruleStr)
return getString(ruleList, unescape = unescape)
}
@JvmOverloads
fun getString(
ruleList: List<SourceRule>,
mContent: Any? = null,
isUrl: Boolean = false
isUrl: Boolean = false,
unescape: Boolean = true
): String {
var result: Any? = null
val content = mContent ?: this.content
@@ -239,13 +246,15 @@ class AnalyzeRule(
}
}
if (result == null) result = ""
val str = kotlin.runCatching {
Entities.unescape(result.toString())
}.onFailure {
log("Entities.unescape() error\n${it.localizedMessage}")
}.getOrElse {
result.toString()
}
val str = if (unescape) {
kotlin.runCatching {
Entities.unescape(result.toString())
}.onFailure {
log("Entities.unescape() error\n${it.localizedMessage}")
}.getOrElse {
result.toString()
}
} else result.toString()
if (isUrl) {
return if (str.isBlank()) {
baseUrl ?: ""
@@ -150,7 +150,7 @@ object BookContent {
val nextUrlList = arrayListOf<String>()
analyzeRule.chapter = chapter
//获取正文
var content = analyzeRule.getString(contentRule.content)
var content = analyzeRule.getString(contentRule.content, unescape = false)
content = HtmlFormatter.formatKeepImg(content, rUrl)
//获取下一页链接
val nextUrlRule = contentRule.nextContentUrl
@@ -1,6 +1,8 @@
package io.legado.app.utils
import io.legado.app.constant.AppLog
import io.legado.app.model.analyzeRule.AnalyzeUrl
import org.jsoup.nodes.Entities
import java.net.URL
import java.util.regex.Pattern
@@ -29,6 +31,13 @@ object HtmlFormatter {
.replace("\\s*\\n+\\s*".toRegex(), "\n  ")
.replace("^[\\n\\s]+".toRegex(), "  ")
.replace("[\\n\\s]+$".toRegex(), "")
.let { s ->
kotlin.runCatching {
Entities.unescape(s)
}.onFailure {
AppLog.put("Entities.unescape() error\n${it.localizedMessage}", it)
}.getOrDefault(s)
}
}
fun formatKeepImg(html: String?, redirectUrl: URL? = null): String {