From b181d81f1728b725e8bd0b6a802a4b3d2911cd97 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 28 Jan 2022 17:24:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E6=96=87=E6=90=9C=E7=B4=A2:=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=88=B0=E5=86=85=E5=AE=B9=E5=90=8E=E5=86=8D=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E5=87=80=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/ContentProcessor.kt | 36 +++++++++++-------- .../searchContent/SearchContentViewModel.kt | 26 ++++++++------ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/ContentProcessor.kt b/app/src/main/java/io/legado/app/help/ContentProcessor.kt index 1b905ec71..32f36c09d 100644 --- a/app/src/main/java/io/legado/app/help/ContentProcessor.kt +++ b/app/src/main/java/io/legado/app/help/ContentProcessor.kt @@ -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 + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt index f745dae5a..0de6e302a 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt @@ -21,6 +21,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati var searchResultCounts = 0 val cacheChapterNames = hashSetOf() val searchResultList: MutableList = 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 { + private fun searchPosition(pattern: String): List { val position: MutableList = 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 }