kunfei 3 年 前
親
コミット
3af85c1a28

+ 1 - 0
app/src/main/assets/updateLog.md

@@ -15,6 +15,7 @@
 **2023/05/16**
 **2023/05/16**
 
 
 * 添加格式化目录规则,只能写js,不用@js:标志,提供变量index和title,index从1开始
 * 添加格式化目录规则,只能写js,不用@js:标志,提供变量index和title,index从1开始
+* 正文添加标题规则,结果更新到目录,有些网站只能在正文获取正确的标题
 
 
 **2023/05/13**
 **2023/05/13**
 
 

+ 1 - 0
app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt

@@ -11,6 +11,7 @@ import kotlinx.parcelize.Parcelize
 @Parcelize
 @Parcelize
 data class ContentRule(
 data class ContentRule(
     var content: String? = null,
     var content: String? = null,
+    var title: String? = null, //有些网站只能在正文中获取标题
     var nextContentUrl: String? = null,
     var nextContentUrl: String? = null,
     var webJs: String? = null,
     var webJs: String? = null,
     var sourceRegex: String? = null,
     var sourceRegex: String? = null,

+ 15 - 1
app/src/main/java/io/legado/app/model/webBook/BookContent.kt

@@ -51,11 +51,25 @@ object BookContent {
         val content = StringBuilder()
         val content = StringBuilder()
         val nextUrlList = arrayListOf(redirectUrl)
         val nextUrlList = arrayListOf(redirectUrl)
         val contentRule = bookSource.getContentRule()
         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.setRedirectUrl(redirectUrl)
         analyzeRule.chapter = bookChapter
         analyzeRule.chapter = bookChapter
         analyzeRule.nextChapterUrl = mNextChapterUrl
         analyzeRule.nextChapterUrl = mNextChapterUrl
         coroutineContext.ensureActive()
         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(
         var contentData = analyzeContent(
             book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl
             book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl
         )
         )

+ 4 - 2
app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt

@@ -320,6 +320,7 @@ class BookSourceEditActivity :
         contentEntities.clear()
         contentEntities.clear()
         contentEntities.apply {
         contentEntities.apply {
             add(EditEntity("content", cr.content, R.string.rule_book_content))
             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("nextContentUrl", cr.nextContentUrl, R.string.rule_next_content))
             add(EditEntity("webJs", cr.webJs, R.string.rule_web_js))
             add(EditEntity("webJs", cr.webJs, R.string.rule_web_js))
             add(EditEntity("sourceRegex", cr.sourceRegex, R.string.rule_source_regex))
             add(EditEntity("sourceRegex", cr.sourceRegex, R.string.rule_source_regex))
@@ -476,10 +477,11 @@ class BookSourceEditActivity :
         }
         }
         contentEntities.forEach {
         contentEntities.forEach {
             when (it.key) {
             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 =
                 "nextContentUrl" -> contentRule.nextContentUrl =
                     viewModel.ruleComplete(it.value, type = 2)
                     viewModel.ruleComplete(it.value, type = 2)
+
                 "webJs" -> contentRule.webJs = it.value
                 "webJs" -> contentRule.webJs = it.value
                 "sourceRegex" -> contentRule.sourceRegex = it.value
                 "sourceRegex" -> contentRule.sourceRegex = it.value
                 "replaceRegex" -> contentRule.replaceRegex = it.value
                 "replaceRegex" -> contentRule.replaceRegex = it.value