This commit is contained in:
Horis 2024-10-23 11:12:31 +08:00
parent 1c0a7bc7b4
commit 003ffeb6c6
2 changed files with 32 additions and 3 deletions

View File

@ -1,5 +1,6 @@
package io.legado.app.model.localBook
import io.legado.app.constant.AppLog
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
@ -14,6 +15,7 @@ import java.io.FileNotFoundException
import java.nio.charset.Charset
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException
import kotlin.math.min
class TextFile(private var book: Book) {
@ -395,7 +397,12 @@ class TextFile(private var book: Book) {
var maxCs = 1
var tocPattern: Pattern? = null
for (tocRule in rules) {
val pattern = tocRule.rule.toPattern(Pattern.MULTILINE)
val pattern = try {
tocRule.rule.toPattern(Pattern.MULTILINE)
} catch (e: PatternSyntaxException) {
AppLog.put("TXT目录规则正则语法错误:${tocRule.name}\n$e", e)
continue
}
val matcher = pattern.matcher(content)
var cs = 0
while (matcher.find()) {

View File

@ -10,6 +10,7 @@ import androidx.fragment.app.viewModels
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppLog
import io.legado.app.data.appDb
import io.legado.app.data.entities.TxtTocRule
import io.legado.app.databinding.DialogTocRegexEditBinding
@ -18,6 +19,8 @@ import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException
class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit, true),
Toolbar.OnMenuItemClickListener {
@ -55,8 +58,11 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_save -> {
callback?.saveTxtTocRule(getRuleFromView())
dismissAllowingStateLoss()
val tocRule = getRuleFromView()
if (checkValid(tocRule)) {
callback?.saveTxtTocRule(getRuleFromView())
dismissAllowingStateLoss()
}
}
R.id.menu_copy_rule -> context?.sendToClip(GSON.toJson(getRuleFromView()))
R.id.menu_paste_rule -> viewModel.pasteRule {
@ -66,6 +72,22 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
return true
}
private fun checkValid(tocRule: TxtTocRule): Boolean {
if (tocRule.name.isEmpty()) {
toastOnUi("名称不能为空")
return false
}
try {
Pattern.compile(tocRule.rule, Pattern.MULTILINE)
} catch (ex: PatternSyntaxException) {
AppLog.put("正则语法错误或不支持(txt)${ex.localizedMessage}", ex, true)
return false
}
return true
}
private fun upRuleView(tocRule: TxtTocRule?) {
binding.tvRuleName.setText(tocRule?.name)
binding.tvRuleRegex.setText(tocRule?.rule)