优化
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
package io.legado.app.help.book
|
package io.legado.app.help.book
|
||||||
|
|
||||||
|
import io.legado.app.data.entities.ReplaceRule
|
||||||
|
|
||||||
data class BookContent(
|
data class BookContent(
|
||||||
val sameTitleRemoved: Boolean,
|
val sameTitleRemoved: Boolean,
|
||||||
val textList: List<String>
|
val textList: List<String>,
|
||||||
|
//起效的替换规则
|
||||||
|
val effectiveReplaceRules: List<ReplaceRule>?
|
||||||
) {
|
) {
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ class ContentProcessor private constructor(
|
|||||||
): BookContent {
|
): BookContent {
|
||||||
var mContent = content
|
var mContent = content
|
||||||
var sameTitleRemoved = false
|
var sameTitleRemoved = false
|
||||||
|
var effectiveReplaceRules: ArrayList<ReplaceRule>? = null
|
||||||
if (content != "null") {
|
if (content != "null") {
|
||||||
//去除重复标题
|
//去除重复标题
|
||||||
val fileName = chapter.getFileName("nr")
|
val fileName = chapter.getFileName("nr")
|
||||||
@@ -142,7 +143,35 @@ class ContentProcessor private constructor(
|
|||||||
}
|
}
|
||||||
if (useReplace && book.getUseReplaceRule()) {
|
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) {
|
if (includeTitle) {
|
||||||
@@ -165,37 +194,7 @@ class ContentProcessor private constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return BookContent(sameTitleRemoved, contents)
|
return BookContent(sameTitleRemoved, contents, effectiveReplaceRules)
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user