替换规则添加作用于标题和作用于正文

pull/1606/head
kunfei 3 years ago
parent b592d338ca
commit 2d07447dfa
  1. 11
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 2
      app/src/main/java/io/legado/app/data/entities/BookChapter.kt
  3. 27
      app/src/main/java/io/legado/app/help/ContentProcessor.kt
  4. 2
      app/src/main/java/io/legado/app/model/ReadBook.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt

@ -42,11 +42,18 @@ interface ReplaceRuleDao {
fun findByIds(vararg ids: Long): List<ReplaceRule> fun findByIds(vararg ids: Long): List<ReplaceRule>
@Query( @Query(
"""SELECT * FROM replace_rules WHERE isEnabled = 1 """SELECT * FROM replace_rules WHERE isEnabled = 1 and scopeContent = 1
AND (scope LIKE '%' || :name || '%' or scope LIKE '%' || :origin || '%' or scope is null or scope = '') AND (scope LIKE '%' || :name || '%' or scope LIKE '%' || :origin || '%' or scope is null or scope = '')
order by sortOrder""" order by sortOrder"""
) )
fun findEnabledByScope(name: String, origin: String): List<ReplaceRule> fun findEnabledByContentScope(name: String, origin: String): List<ReplaceRule>
@Query(
"""SELECT * FROM replace_rules WHERE isEnabled = 1 and scopeTitle = 1
AND (scope LIKE '%' || :name || '%' or scope LIKE '%' || :origin || '%' or scope is null or scope = '')
order by sortOrder"""
)
fun findEnabledByTitleScope(name: String, origin: String): List<ReplaceRule>
@Query("select * from replace_rules where `group` like '%' || :group || '%'") @Query("select * from replace_rules where `group` like '%' || :group || '%'")
fun getByGroup(group: String): List<ReplaceRule> fun getByGroup(group: String): List<ReplaceRule>

@ -74,7 +74,7 @@ data class BookChapter(
@Suppress("unused") @Suppress("unused")
fun getDisplayTitle( fun getDisplayTitle(
replaceRules: Array<ReplaceRule>? = null, replaceRules: List<ReplaceRule>? = null,
useReplace: Boolean = true, useReplace: Boolean = true,
chineseConvert: Boolean = true, chineseConvert: Boolean = true,
): String { ): String {

@ -37,7 +37,8 @@ class ContentProcessor private constructor(
} }
private val replaceRules = arrayListOf<ReplaceRule>() private val titleReplaceRules = arrayListOf<ReplaceRule>()
private val contentReplaceRules = arrayListOf<ReplaceRule>()
init { init {
upReplaceRules() upReplaceRules()
@ -45,13 +46,25 @@ class ContentProcessor private constructor(
@Synchronized @Synchronized
fun upReplaceRules() { fun upReplaceRules() {
replaceRules.clear() titleReplaceRules.clear()
replaceRules.addAll(appDb.replaceRuleDao.findEnabledByScope(bookName, bookOrigin)) contentReplaceRules.clear()
titleReplaceRules.addAll(appDb.replaceRuleDao.findEnabledByTitleScope(bookName, bookOrigin))
contentReplaceRules.addAll(
appDb.replaceRuleDao.findEnabledByContentScope(
bookName,
bookOrigin
)
)
} }
@Synchronized @Synchronized
fun getReplaceRules(): Array<ReplaceRule> { fun getTitleReplaceRules(): List<ReplaceRule> {
return replaceRules.toTypedArray() return titleReplaceRules
}
@Synchronized
fun getContentReplaceRules(): List<ReplaceRule> {
return contentReplaceRules
} }
fun getContent( fun getContent(
@ -94,7 +107,7 @@ class ContentProcessor private constructor(
} }
if (includeTitle) { if (includeTitle) {
//重新添加标题 //重新添加标题
mContent = chapter.getDisplayTitle(getReplaceRules()) + "\n" + mContent mContent = chapter.getDisplayTitle(getTitleReplaceRules()) + "\n" + mContent
} }
val contents = arrayListOf<String>() val contents = arrayListOf<String>()
mContent.split("\n").forEach { str -> mContent.split("\n").forEach { str ->
@ -114,7 +127,7 @@ class ContentProcessor private constructor(
fun replaceContent(content: String): String { fun replaceContent(content: String): String {
var mContent = content var mContent = content
getReplaceRules().forEach { item -> getContentReplaceRules().forEach { item ->
if (item.pattern.isNotEmpty()) { if (item.pattern.isNotEmpty()) {
try { try {
mContent = if (item.isRegex) { mContent = if (item.isRegex) {

@ -352,7 +352,7 @@ object ReadBook : CoroutineScope by MainScope() {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
val contentProcessor = ContentProcessor.get(book.name, book.origin) val contentProcessor = ContentProcessor.get(book.name, book.origin)
val displayTitle = chapter.getDisplayTitle( val displayTitle = chapter.getDisplayTitle(
contentProcessor.getReplaceRules(), contentProcessor.getContentReplaceRules(),
book.getUseReplaceRule() book.getUseReplaceRule()
) )
val contents = contentProcessor.getContent(book, chapter, content) val contents = contentProcessor.getContent(book, chapter, content)

@ -52,7 +52,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) :
private val replaceRules private val replaceRules
get() = callback.book?.let { get() = callback.book?.let {
ContentProcessor.get(it.name, it.origin).getReplaceRules() ContentProcessor.get(it.name, it.origin).getTitleReplaceRules()
} }
private val useReplace private val useReplace
get() = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true get() = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true

Loading…
Cancel
Save