mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
全文搜索:搜索到内容后再启用净化
This commit is contained in:
parent
1703c5ed1c
commit
b181d81f17
@ -56,7 +56,7 @@ class ContentProcessor private constructor(
|
||||
|
||||
fun getContent(
|
||||
book: Book,
|
||||
chapter: BookChapter, //已经经过简繁转换
|
||||
chapter: BookChapter,
|
||||
content: String,
|
||||
includeTitle: Boolean = true,
|
||||
useReplace: Boolean = true,
|
||||
@ -83,20 +83,7 @@ class ContentProcessor private constructor(
|
||||
}
|
||||
if (useReplace && book.getUseReplaceRule()) {
|
||||
//替换
|
||||
getReplaceRules().forEach { item ->
|
||||
if (item.pattern.isNotEmpty()) {
|
||||
try {
|
||||
mContent = if (item.isRegex) {
|
||||
mContent.replace(item.pattern.toRegex(), item.replacement)
|
||||
} else {
|
||||
mContent.replace(item.pattern, item.replacement)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("${item.name}替换出错\n${e.localizedMessage}")
|
||||
appCtx.toastOnUi("${item.name}替换出错")
|
||||
}
|
||||
}
|
||||
}
|
||||
mContent = replaceContent(mContent)
|
||||
}
|
||||
if (chineseConvert) {
|
||||
//简繁转换
|
||||
@ -125,4 +112,23 @@ class ContentProcessor private constructor(
|
||||
return contents
|
||||
}
|
||||
|
||||
fun replaceContent(content: String): String {
|
||||
var mContent = content
|
||||
getReplaceRules().forEach { item ->
|
||||
if (item.pattern.isNotEmpty()) {
|
||||
try {
|
||||
mContent = if (item.isRegex) {
|
||||
mContent.replace(item.pattern.toRegex(), item.replacement)
|
||||
} else {
|
||||
mContent.replace(item.pattern, item.replacement)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("${item.name}替换出错\n${e.localizedMessage}")
|
||||
appCtx.toastOnUi("${item.name}替换出错")
|
||||
}
|
||||
}
|
||||
}
|
||||
return mContent
|
||||
}
|
||||
|
||||
}
|
@ -21,6 +21,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
|
||||
var searchResultCounts = 0
|
||||
val cacheChapterNames = hashSetOf<String>()
|
||||
val searchResultList: MutableList<SearchResult> = mutableListOf()
|
||||
var mContent: String = ""
|
||||
|
||||
fun initBook(bookUrl: String, success: () -> Unit) {
|
||||
this.bookUrl = bookUrl
|
||||
@ -40,21 +41,20 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
|
||||
book?.let { book ->
|
||||
val chapterContent = BookHelp.getContent(book, chapter)
|
||||
if (chapterContent != null) {
|
||||
//搜索替换后的正文
|
||||
val replaceContent: String
|
||||
//先搜索没有启用净化的正文
|
||||
withContext(Dispatchers.IO) {
|
||||
chapter.title = when (AppConfig.chineseConverterType) {
|
||||
1 -> ChineseUtils.t2s(chapter.title)
|
||||
2 -> ChineseUtils.s2t(chapter.title)
|
||||
else -> chapter.title
|
||||
}
|
||||
replaceContent = contentProcessor!!.getContent(
|
||||
book, chapter, chapterContent, chineseConvert = false, reSegment = false
|
||||
mContent = contentProcessor!!.getContent(
|
||||
book, chapter, chapterContent, chineseConvert = false, reSegment = false, useReplace = false
|
||||
).joinToString("")
|
||||
}
|
||||
val positions = searchPosition(replaceContent, query)
|
||||
val positions = searchPosition(query)
|
||||
positions.forEachIndexed { index, position ->
|
||||
val construct = getResultAndQueryIndex(replaceContent, position, query)
|
||||
val construct = getResultAndQueryIndex(mContent, position, query)
|
||||
val result = SearchResult(
|
||||
resultCountWithinChapter = index,
|
||||
resultText = construct.second,
|
||||
@ -73,12 +73,16 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
|
||||
return searchResultsWithinChapter
|
||||
}
|
||||
|
||||
private fun searchPosition(chapterContent: String, pattern: String): List<Int> {
|
||||
private fun searchPosition(pattern: String): List<Int> {
|
||||
val position: MutableList<Int> = mutableListOf()
|
||||
var index = chapterContent.indexOf(pattern)
|
||||
while (index >= 0) {
|
||||
position.add(index)
|
||||
index = chapterContent.indexOf(pattern, index + 1)
|
||||
if (mContent.indexOf(pattern) >= 0) {
|
||||
//搜索到内容才启用净化
|
||||
mContent = contentProcessor.replaceContent(mContent)
|
||||
var index = mContent.indexOf(pattern)
|
||||
while (index >= 0) {
|
||||
position.add(index)
|
||||
index = mContent.indexOf(pattern, index + 1)
|
||||
}
|
||||
}
|
||||
return position
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user