优化正文

pull/1319/head
gedoor 3 years ago
parent 03b4ef64f5
commit b8cc77a6fd
  1. 4
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 63
      app/src/main/java/io/legado/app/help/ContentProcessor.kt
  3. 2
      app/src/main/java/io/legado/app/model/ReadBook.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentActivity.kt

@ -140,7 +140,7 @@ object BookController {
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
saveBookReadIndex(book, index) saveBookReadIndex(book, index)
return returnData.setData( return returnData.setData(
contentProcessor.getContent(book, chapter.title, content) contentProcessor.getContent(book, chapter, content)
.joinToString("\n") .joinToString("\n")
) )
} }
@ -153,7 +153,7 @@ object BookController {
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
saveBookReadIndex(book, index) saveBookReadIndex(book, index)
returnData.setData( returnData.setData(
contentProcessor.getContent(book, chapter.title, content).joinToString("\n") contentProcessor.getContent(book, chapter, content).joinToString("\n")
) )
} catch (e: Exception) { } catch (e: Exception) {
returnData.setErrorMsg(e.msg) returnData.setErrorMsg(e.msg)

@ -3,6 +3,7 @@ package io.legado.app.help
import com.github.liuyueyi.quick.transfer.ChineseUtils import com.github.liuyueyi.quick.transfer.ChineseUtils
import io.legado.app.data.appDb import io.legado.app.data.appDb
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.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import splitties.init.appCtx import splitties.init.appCtx
@ -53,22 +54,51 @@ class ContentProcessor private constructor(
fun getContent( fun getContent(
book: Book, book: Book,
title: String, //已经经过简繁转换 chapter: BookChapter, //已经经过简繁转换
content: String, content: String,
includeTitle: Boolean = true, includeTitle: Boolean = true,
useReplace: Boolean = true, useReplace: Boolean = true,
chineseConvert: Boolean = true, chineseConvert: Boolean = true,
reSegment: Boolean = true reSegment: Boolean = true
): List<String> { ): List<String> {
var content1 = content //去除无效内容
var mContent = content.trimStart {
it.code <= 0x20 || it == ' ' || it == ',' || it == ',' || it == ','
}
//去除书名
if (mContent.startsWith(book.name)) {
mContent = mContent.substring(book.name.length)
}
//去除带书名号的书名
val cName = "${book.name}"
if (mContent.startsWith(cName)) {
mContent = mContent.substring(cName.length)
}
//去除无效内容
mContent = mContent.trimStart {
it.code <= 0x20 || it == ' ' || it == ',' || it == ',' || it == ','
}
//去除标题
if (mContent.startsWith(chapter.title)) {
mContent = mContent.substring(chapter.title.length)
}
//去除无效内容
mContent = mContent.trimStart {
it.code <= 0x20 || it == ' ' || it == ',' || it == ',' || it == ','
}
if (includeTitle) {
//重新添加标题
mContent = chapter.getDisplayTitle() + "\n" + mContent
}
if (useReplace && book.getUseReplaceRule()) { if (useReplace && book.getUseReplaceRule()) {
//替换
getReplaceRules().forEach { item -> getReplaceRules().forEach { item ->
if (item.pattern.isNotEmpty()) { if (item.pattern.isNotEmpty()) {
try { try {
content1 = if (item.isRegex) { mContent = if (item.isRegex) {
content1.replace(item.pattern.toRegex(), item.replacement) mContent.replace(item.pattern.toRegex(), item.replacement)
} else { } else {
content1.replace(item.pattern, item.replacement) mContent.replace(item.pattern, item.replacement)
} }
} catch (e: Exception) { } catch (e: Exception) {
appCtx.toastOnUi("${item.name}替换出错") appCtx.toastOnUi("${item.name}替换出错")
@ -77,29 +107,26 @@ class ContentProcessor private constructor(
} }
} }
if (reSegment && book.getReSegment()) { if (reSegment && book.getReSegment()) {
content1 = ContentHelp.reSegment(content1, title) //重新分段
mContent = ContentHelp.reSegment(mContent, chapter.title)
} }
if (chineseConvert) { if (chineseConvert) {
//简繁转换
try { try {
when (AppConfig.chineseConverterType) { when (AppConfig.chineseConverterType) {
1 -> content1 = ChineseUtils.t2s(content1) 1 -> mContent = ChineseUtils.t2s(mContent)
2 -> content1 = ChineseUtils.s2t(content1) 2 -> mContent = ChineseUtils.s2t(mContent)
} }
} catch (e: Exception) { } catch (e: Exception) {
appCtx.toastOnUi("简繁转换出错") appCtx.toastOnUi("简繁转换出错")
} }
} }
val contents = arrayListOf<String>() val contents = arrayListOf<String>()
content1.split("\n").forEach { str -> mContent.split("\n").forEach { str ->
val paragraph = str.replace("^[\\n\\r]+".toRegex(), "").trim() val paragraph = str.trim {
if (contents.isEmpty()) { it.code <= 0x20 || it == ' '
if (includeTitle) { }
contents.add(title) if (contents.isNotEmpty()) {
}
if (paragraph != title && paragraph.isNotEmpty()) {
contents.add("${ReadBookConfig.paragraphIndent}$paragraph")
}
} else if (paragraph.isNotEmpty()) {
contents.add("${ReadBookConfig.paragraphIndent}$paragraph") contents.add("${ReadBookConfig.paragraphIndent}$paragraph")
} }
} }

@ -343,7 +343,7 @@ object ReadBook : CoroutineScope by MainScope() {
else -> chapter.title else -> chapter.title
} }
val contents = ContentProcessor.get(book.name, book.origin) val contents = ContentProcessor.get(book.name, book.origin)
.getContent(book, chapter.title, content) .getContent(book, chapter, content)
val textChapter = ChapterProvider val textChapter = ChapterProvider
.getTextChapter(book, chapter, contents, chapterSize) .getTextChapter(book, chapter, contents, chapterSize)
when (val offset = chapter.index - durChapterIndex) { when (val offset = chapter.index - durChapterIndex) {

@ -142,7 +142,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
val content1 = contentProcessor val content1 = contentProcessor
.getContent( .getContent(
book, book,
chapter.title.replace("\\r?\\n".toRegex(), " "), chapter,
content ?: "null", content ?: "null",
includeTitle = !AppConfig.exportNoChapterName, includeTitle = !AppConfig.exportNoChapterName,
useReplace = useReplace, useReplace = useReplace,
@ -396,7 +396,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
content1 = contentProcessor content1 = contentProcessor
.getContent( .getContent(
book, book,
chapter.title, chapter,
content1, content1,
includeTitle = false, includeTitle = false,
useReplace = useReplace, useReplace = useReplace,

@ -183,7 +183,7 @@ class SearchContentActivity :
replaceContents = replaceContents =
viewModel.contentProcessor!!.getContent( viewModel.contentProcessor!!.getContent(
book, book,
chapter.title, chapter,
bookContent, bookContent,
chineseConvert = false, chineseConvert = false,
reSegment = false reSegment = false

Loading…
Cancel
Save