阅读界面小标题使用替换规则

pull/1343/head
gedoor 3 years ago
parent 90327f557c
commit 535001acc0
  1. 2
      app/src/main/assets/updateLog.md
  2. 42
      app/src/main/java/io/legado/app/data/entities/BookChapter.kt
  3. 4
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt

@ -16,6 +16,8 @@
1. 阅读界面区域设置添加朗读上一段和朗读下一段 1. 阅读界面区域设置添加朗读上一段和朗读下一段
2. 在线朗读采用平均速度计算及时翻页 2. 在线朗读采用平均速度计算及时翻页
3. 修复听书定时问题
4. 阅读小标题也使用替换
**2021/09/20** **2021/09/20**

@ -8,14 +8,12 @@ import androidx.room.Index
import io.legado.app.R import io.legado.app.R
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.RuleDataInterface import io.legado.app.model.analyzeRule.RuleDataInterface
import io.legado.app.utils.GSON import io.legado.app.utils.*
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.fromJsonObject
import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import splitties.init.appCtx import splitties.init.appCtx
@Suppress("unused")
@Parcelize @Parcelize
@Entity( @Entity(
tableName = "chapters", tableName = "chapters",
@ -71,11 +69,31 @@ data class BookChapter(
return false return false
} }
fun getDisplayTitle(): String { @Suppress("unused")
fun getDisplayTitle(
replaceRules: List<ReplaceRule>? = null,
useReplace: Boolean = true
): String {
var displayTitle = title
if (useReplace && replaceRules != null) {
replaceRules.forEach { item ->
if (item.pattern.isNotEmpty()) {
try {
displayTitle = if (item.isRegex) {
displayTitle.replace(item.pattern.toRegex(), item.replacement)
} else {
displayTitle.replace(item.pattern, item.replacement)
}
} catch (e: Exception) {
appCtx.toastOnUi("${item.name}替换出错")
}
}
}
}
return when { return when {
!isVip -> title !isVip -> displayTitle
isPay -> appCtx.getString(R.string.payed_title, title) isPay -> appCtx.getString(R.string.payed_title, displayTitle)
else -> appCtx.getString(R.string.vip_title, title) else -> appCtx.getString(R.string.vip_title, displayTitle)
} }
} }
@ -83,9 +101,11 @@ data class BookChapter(
val urlMatcher = AnalyzeUrl.paramPattern.matcher(url) val urlMatcher = AnalyzeUrl.paramPattern.matcher(url)
val urlBefore = if (urlMatcher.find()) url.substring(0, urlMatcher.start()) else url val urlBefore = if (urlMatcher.find()) url.substring(0, urlMatcher.start()) else url
val urlAbsoluteBefore = NetworkUtils.getAbsoluteURL(baseUrl, urlBefore) val urlAbsoluteBefore = NetworkUtils.getAbsoluteURL(baseUrl, urlBefore)
return if (urlBefore.length == url.length) urlAbsoluteBefore else urlAbsoluteBefore + ',' + url.substring( return if (urlBefore.length == url.length) {
urlMatcher.end() urlAbsoluteBefore
) } else {
"$urlAbsoluteBefore," + url.substring(urlMatcher.end())
}
} }
fun getFileName(): String = String.format("%05d-%s.nb", index, MD5Utils.md5Encode16(title)) fun getFileName(): String = String.format("%05d-%s.nb", index, MD5Utils.md5Encode16(title))

@ -154,12 +154,12 @@ object ChapterProvider {
item.pageSize = textPages.size item.pageSize = textPages.size
item.chapterIndex = bookChapter.index item.chapterIndex = bookChapter.index
item.chapterSize = chapterSize item.chapterSize = chapterSize
item.title = bookChapter.title item.title = bookChapter.getDisplayTitle()
item.upLinesPosition() item.upLinesPosition()
} }
return TextChapter( return TextChapter(
bookChapter.index, bookChapter.title, bookChapter.index, bookChapter.getDisplayTitle(),
bookChapter.getAbsoluteURL().substringBefore(",{"), //getAbsoluteURL已经格式过 bookChapter.getAbsoluteURL().substringBefore(",{"), //getAbsoluteURL已经格式过
textPages, chapterSize textPages, chapterSize
) )

Loading…
Cancel
Save