mirror of
https://github.com/gedoor/legado.git
synced 2024-09-01 09:34:25 +08:00
优化
This commit is contained in:
parent
649e481d72
commit
843fa18985
@ -1,8 +1,12 @@
|
||||
package io.legado.app.help.book
|
||||
|
||||
import io.legado.app.data.entities.ReplaceRule
|
||||
|
||||
data class BookContent(
|
||||
val sameTitleRemoved: Boolean,
|
||||
val textList: List<String>
|
||||
val textList: List<String>,
|
||||
//起效的替换规则
|
||||
val effectiveReplaceRules: List<ReplaceRule>?
|
||||
) {
|
||||
|
||||
override fun toString(): String {
|
||||
|
@ -97,6 +97,7 @@ class ContentProcessor private constructor(
|
||||
): BookContent {
|
||||
var mContent = content
|
||||
var sameTitleRemoved = false
|
||||
var effectiveReplaceRules: ArrayList<ReplaceRule>? = null
|
||||
if (content != "null") {
|
||||
//去除重复标题
|
||||
val fileName = chapter.getFileName("nr")
|
||||
@ -142,7 +143,35 @@ class ContentProcessor private constructor(
|
||||
}
|
||||
if (useReplace && book.getUseReplaceRule()) {
|
||||
//替换
|
||||
mContent = replaceContent(mContent)
|
||||
effectiveReplaceRules = arrayListOf()
|
||||
mContent = mContent.lines().joinToString("\n") { it.trim() }
|
||||
getContentReplaceRules().forEach { item ->
|
||||
if (item.pattern.isNotEmpty()) {
|
||||
try {
|
||||
val tmp = if (item.isRegex) {
|
||||
mContent.replace(
|
||||
item.pattern.toRegex(),
|
||||
item.replacement,
|
||||
item.getValidTimeoutMillisecond()
|
||||
)
|
||||
} else {
|
||||
mContent.replace(item.pattern, item.replacement)
|
||||
}
|
||||
if (mContent != tmp) {
|
||||
effectiveReplaceRules.add(item)
|
||||
mContent = tmp
|
||||
}
|
||||
} catch (e: RegexTimeoutException) {
|
||||
item.isEnabled = false
|
||||
appDb.replaceRuleDao.update(item)
|
||||
mContent = item.name + e.stackTraceStr
|
||||
} catch (_: CancellationException) {
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("替换净化: 规则 ${item.name}替换出错.\n${mContent}", e)
|
||||
appCtx.toastOnUi("替换净化: 规则 ${item.name}替换出错")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (includeTitle) {
|
||||
@ -165,37 +194,7 @@ class ContentProcessor private constructor(
|
||||
}
|
||||
}
|
||||
}
|
||||
return BookContent(sameTitleRemoved, contents)
|
||||
}
|
||||
|
||||
private fun replaceContent(content: String): String {
|
||||
var mContent = content
|
||||
mContent = mContent.lines().joinToString("\n") { it.trim() }
|
||||
getContentReplaceRules().forEach { item ->
|
||||
if (item.pattern.isNotEmpty()) {
|
||||
try {
|
||||
mContent = if (item.isRegex) {
|
||||
mContent.replace(
|
||||
item.pattern.toRegex(),
|
||||
item.replacement,
|
||||
item.getValidTimeoutMillisecond()
|
||||
)
|
||||
} else {
|
||||
mContent.replace(item.pattern, item.replacement)
|
||||
}
|
||||
} catch (e: RegexTimeoutException) {
|
||||
item.isEnabled = false
|
||||
appDb.replaceRuleDao.update(item)
|
||||
return item.name + e.stackTraceStr
|
||||
} catch (e: CancellationException) {
|
||||
return mContent
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("替换净化: 规则 ${item.name}替换出错\n替换内容\n${mContent}", e)
|
||||
appCtx.toastOnUi("替换净化: 规则 ${item.name}替换出错")
|
||||
}
|
||||
}
|
||||
}
|
||||
return mContent
|
||||
return BookContent(sameTitleRemoved, contents, effectiveReplaceRules)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user