pull/2628/head
kunfei 2 years ago
parent a41cd5e92f
commit e0dc54f381
  1. 4
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 12
      app/src/main/java/io/legado/app/help/book/BookContent.kt
  3. 18
      app/src/main/java/io/legado/app/help/book/ContentProcessor.kt
  4. 5
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/read/ContentEditDialog.kt
  6. 1
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt
  7. 5
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  8. 2
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt

@ -173,7 +173,7 @@ object BookController {
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
content = runBlocking { content = runBlocking {
contentProcessor.getContent(book, chapter, content!!, includeTitle = false) contentProcessor.getContent(book, chapter, content!!, includeTitle = false)
.joinToString("\n") .toString()
} }
return returnData.setData(content) return returnData.setData(content)
} }
@ -184,7 +184,7 @@ object BookController {
WebBook.getContentAwait(bookSource, book, chapter).let { WebBook.getContentAwait(bookSource, book, chapter).let {
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
contentProcessor.getContent(book, chapter, it, includeTitle = false) contentProcessor.getContent(book, chapter, it, includeTitle = false)
.joinToString("\n") .toString()
} }
} }
returnData.setData(content) returnData.setData(content)

@ -0,0 +1,12 @@
package io.legado.app.help.book
data class BookContent(
val sameTitleRemoved: Boolean,
val textList: List<String>
) {
override fun toString(): String {
return textList.joinToString("\n")
}
}

@ -9,6 +9,7 @@ import io.legado.app.data.entities.ReplaceRule
import io.legado.app.exception.RegexTimeoutException import io.legado.app.exception.RegexTimeoutException
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.replace import io.legado.app.utils.replace
import io.legado.app.utils.stackTraceStr import io.legado.app.utils.stackTraceStr
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
@ -16,6 +17,7 @@ import kotlinx.coroutines.CancellationException
import splitties.init.appCtx import splitties.init.appCtx
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.CopyOnWriteArrayList
import java.util.regex.Pattern
class ContentProcessor private constructor( class ContentProcessor private constructor(
private val bookName: String, private val bookName: String,
@ -78,9 +80,21 @@ class ContentProcessor private constructor(
useReplace: Boolean = true, useReplace: Boolean = true,
chineseConvert: Boolean = true, chineseConvert: Boolean = true,
reSegment: Boolean = true reSegment: Boolean = true
): List<String> { ): BookContent {
var mContent = content var mContent = content
var sameTitleRemoved = false
if (content != "null") { if (content != "null") {
//去除重复标题
val key = "NRT" + MD5Utils.md5Encode(chapter.bookUrl + chapter.url)
if (appDb.cacheDao.get(key) == null) try {
val name = Pattern.quote(book.name)
val title = Pattern.quote(chapter.title)
val titleRegex = "^(\\s|\\p{P}|${name})*${title}(\\s)*".toRegex()
mContent = mContent.replace(titleRegex, "")
sameTitleRemoved = true
} catch (e: Exception) {
AppLog.put("去除重复标题出错\n${e.localizedMessage}", e)
}
if (reSegment && book.getReSegment()) { if (reSegment && book.getReSegment()) {
//重新分段 //重新分段
mContent = ContentHelp.reSegment(mContent, chapter.title) mContent = ContentHelp.reSegment(mContent, chapter.title)
@ -121,7 +135,7 @@ class ContentProcessor private constructor(
} }
} }
} }
return contents return BookContent(sameTitleRemoved, contents)
} }
suspend fun replaceContent(content: String): String { suspend fun replaceContent(content: String): String {

@ -232,7 +232,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
useReplace = useReplace, useReplace = useReplace,
chineseConvert = false, chineseConvert = false,
reSegment = false reSegment = false
).joinToString("\n") ).toString()
if (AppConfig.exportPictureFile) { if (AppConfig.exportPictureFile) {
//txt导出图片文件 //txt导出图片文件
val srcList = arrayListOf<Triple<String, Int, String>>() val srcList = arrayListOf<Triple<String, Int, String>>()
@ -515,8 +515,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
useReplace = useReplace, useReplace = useReplace,
chineseConvert = false, chineseConvert = false,
reSegment = false reSegment = false
) ).toString()
.joinToString("\n")
val title = chapter.getDisplayTitle( val title = chapter.getDisplayTitle(
contentProcessor.getTitleReplaceRules(), contentProcessor.getTitleReplaceRules(),
useReplace = useReplace useReplace = useReplace

@ -152,7 +152,7 @@ class ContentEditDialog : BaseDialogFragment(R.layout.dialog_content_edit) {
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
val content = BookHelp.getContent(book, chapter) ?: return@let null val content = BookHelp.getContent(book, chapter) ?: return@let null
contentProcessor.getContent(book, chapter, content, includeTitle = false) contentProcessor.getContent(book, chapter, content, includeTitle = false)
.joinToString("\n") .toString()
} }
}.onStart { }.onStart {
loadStateLiveData.postValue(true) loadStateLiveData.postValue(true)

@ -15,6 +15,7 @@ data class TextChapter(
val url: String, val url: String,
val pages: List<TextPage>, val pages: List<TextPage>,
val chaptersSize: Int, val chaptersSize: Int,
val sameTitleRemoved: Boolean,
val isVip: Boolean, val isVip: Boolean,
val isPay: Boolean, val isPay: Boolean,
) { ) {

@ -12,6 +12,7 @@ import io.legado.app.constant.AppPattern
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.book.BookContent
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
@ -117,9 +118,10 @@ object ChapterProvider {
book: Book, book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,
displayTitle: String, displayTitle: String,
contents: List<String>, bookContent: BookContent,
chapterSize: Int, chapterSize: Int,
): TextChapter { ): TextChapter {
val contents = bookContent.textList
val textPages = arrayListOf<TextPage>() val textPages = arrayListOf<TextPage>()
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
var absStartX = paddingLeft var absStartX = paddingLeft
@ -217,6 +219,7 @@ object ChapterProvider {
bookChapter.index, displayTitle, bookChapter.index, displayTitle,
bookChapter.getAbsoluteURL(), bookChapter.getAbsoluteURL(),
textPages, chapterSize, textPages, chapterSize,
bookContent.sameTitleRemoved,
bookChapter.isVip, bookChapter.isPay bookChapter.isVip, bookChapter.isPay
) )
} }

@ -56,7 +56,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
coroutineContext.ensureActive() coroutineContext.ensureActive()
mContent = contentProcessor!!.getContent( mContent = contentProcessor!!.getContent(
book, chapter, chapterContent book, chapter, chapterContent
).joinToString("") ).toString()
} }
val positions = searchPosition(mContent, query) val positions = searchPosition(mContent, query)
positions.forEachIndexed { index, position -> positions.forEachIndexed { index, position ->

Loading…
Cancel
Save