From 3b71c8e2a8e2b054b68acd5b6b3003ba696f25d7 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 16 May 2023 23:05:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 1 + .../legado/app/data/entities/rule/ContentRule.kt | 1 + .../io/legado/app/model/webBook/BookContent.kt | 16 +++++++++++++++- .../book/source/edit/BookSourceEditActivity.kt | 6 ++++-- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 04ce057de..bbf329dde 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -15,6 +15,7 @@ **2023/05/16** * 添加格式化目录规则,只能写js,不用@js:标志,提供变量index和title,index从1开始 +* 正文添加标题规则,结果更新到目录,有些网站只能在正文获取正确的标题 **2023/05/13** diff --git a/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt b/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt index 135472f2a..aef453668 100644 --- a/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt @@ -11,6 +11,7 @@ import kotlinx.parcelize.Parcelize @Parcelize data class ContentRule( var content: String? = null, + var title: String? = null, //有些网站只能在正文中获取标题 var nextContentUrl: String? = null, var webJs: String? = null, var sourceRegex: String? = null, diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index ccbe104d2..61b9581be 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -51,11 +51,25 @@ object BookContent { val content = StringBuilder() val nextUrlList = arrayListOf(redirectUrl) val contentRule = bookSource.getContentRule() - val analyzeRule = AnalyzeRule(book, bookSource).setContent(body, baseUrl) + val analyzeRule = AnalyzeRule(book, bookSource) + analyzeRule.setContent(body, baseUrl) analyzeRule.setRedirectUrl(redirectUrl) analyzeRule.chapter = bookChapter analyzeRule.nextChapterUrl = mNextChapterUrl coroutineContext.ensureActive() + contentRule.title?.let { + if (it.isNotBlank()) { + val title = analyzeRule.runCatching { + getString(it) + }.onFailure { e -> + Debug.log(bookSource.bookSourceUrl, "获取标题出错, ${e.localizedMessage}") + }.getOrNull() + if (!title.isNullOrBlank()) { + bookChapter.title = title + appDb.bookChapterDao.upDate(bookChapter) + } + } + } var contentData = analyzeContent( book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl ) diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 6e6b2011c..23d56b05e 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -320,6 +320,7 @@ class BookSourceEditActivity : contentEntities.clear() contentEntities.apply { add(EditEntity("content", cr.content, R.string.rule_book_content)) + add(EditEntity("title", cr.title, R.string.rule_chapter_name)) add(EditEntity("nextContentUrl", cr.nextContentUrl, R.string.rule_next_content)) add(EditEntity("webJs", cr.webJs, R.string.rule_web_js)) add(EditEntity("sourceRegex", cr.sourceRegex, R.string.rule_source_regex)) @@ -476,10 +477,11 @@ class BookSourceEditActivity : } contentEntities.forEach { when (it.key) { - "content" -> contentRule.content = - viewModel.ruleComplete(it.value) + "content" -> contentRule.content = viewModel.ruleComplete(it.value) + "title" -> contentRule.title = viewModel.ruleComplete(it.value) "nextContentUrl" -> contentRule.nextContentUrl = viewModel.ruleComplete(it.value, type = 2) + "webJs" -> contentRule.webJs = it.value "sourceRegex" -> contentRule.sourceRegex = it.value "replaceRegex" -> contentRule.replaceRegex = it.value