From 97ac253f5f933635558778bf7210f5cc4a711e92 Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Sat, 17 Jul 2021 01:49:38 +0800 Subject: [PATCH 01/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=88=86=E5=89=B2=E8=A7=84=E5=88=99=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=99=84=E5=8A=A0=E5=8F=82=E6=95=B0=E4=B8=AD=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=20,{=20=E6=97=B6=E5=AF=BC=E8=87=B4=E8=A7=84=E5=88=99=E5=88=87?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复这种页数列表写法中,js部分内含 < 或 > 会切割错误的问题 格式化图片和匹配图片规则不在使用同一个正则,加快图片处理速度。 优化一下不需要循环执行却被循环执行的操作 --- .../legado/app/data/entities/BookChapter.kt | 9 +++---- .../app/model/analyzeRule/AnalyzeRule.kt | 25 +++++++++---------- .../app/model/analyzeRule/AnalyzeUrl.kt | 7 +++--- .../app/model/analyzeRule/RuleAnalyzer.kt | 7 ++++-- .../read/page/provider/ChapterProvider.kt | 6 +---- .../java/io/legado/app/utils/HtmlFormatter.kt | 6 ++--- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index 67bba2e5d..77811cba7 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -62,12 +62,9 @@ data class BookChapter( } fun getAbsoluteURL():String{ - val pos = url.indexOf(',') - return if(pos == -1) NetworkUtils.getAbsoluteURL(baseUrl,url) - else NetworkUtils.getAbsoluteURL( - baseUrl, - url.substring(0, pos) - ) + url.substring(pos) + val urlBefore = url.substringBefore(",{") + val urlAbsoluteBefore = NetworkUtils.getAbsoluteURL(baseUrl,urlBefore) + return if(urlBefore.length == url.length) urlAbsoluteBefore else urlAbsoluteBefore + url.substring(urlBefore.length) } fun getFileName(): String = String.format("%05d-%s.nb", index, MD5Utils.md5Encode16(title)) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 3efbd2f02..fd814d00d 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -48,6 +48,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { fun setContent(content: Any?, baseUrl: String? = null): AnalyzeRule { if (content == null) throw AssertionError("内容不可空(Content cannot be null)") this.content = content + isJSON = content.toString().isJson() setBaseUrl(baseUrl) objectChangedXP = true objectChangedJS = true @@ -64,8 +65,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { fun setRedirectUrl(url: String): URL? { kotlin.runCatching { - val pos = url.indexOf(',') - redirectUrl = URL( if(pos == -1) url else url.substring(0,pos)) + redirectUrl = URL(url.substringBefore(",{")) } return redirectUrl } @@ -378,12 +378,12 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { mMode = Mode.Json ruleStr0.substring(6) } - ruleStr0.startsWith(":") -> { //:与伪类选择器冲突,改成?更合理 + ruleStr0.startsWith(":") -> { //:与伪类选择器冲突,建议改成?更合理 mMode = Mode.Regex isRegex = true ruleStr0.substring(1) } - ( ruleStr0[1] == '.' || ruleStr0[1] == '[') && ruleStr0[0] == '$' || content.toString().isJson() -> { + ( ruleStr0[1] == '.' || ruleStr0[1] == '[') && ruleStr0[0] == '$' || isJSON -> { mMode = Mode.Json ruleStr0 } @@ -596,15 +596,14 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { } private fun isRule(ruleStr: String): Boolean { - return when { - ruleStr.startsWith("$.") -> true - ruleStr.startsWith("@Json:", true) -> true - ruleStr.startsWith("//") -> true - ruleStr.startsWith("@XPath:", true) -> true - ruleStr.startsWith("@CSS:", true) -> true - ruleStr.startsWith("@@") -> true - else -> false - } + return ruleStr.startsWith('@') //js首个字符不可能是@,除非是装饰器,所以@开头规定为规则 + || ruleStr.startsWith('$') //js首个字符不可能是$,因为并未注入$变量,且$1之类的东西已被替换,不会影响此处判断 + || ruleStr.startsWith("//") + || ruleStr.startsWith("class")//js首个字符串不可能是class,因为并未注入相关字符串开头的变量 + || ruleStr.startsWith("id")//js首个字符串不可能是id,因为并未注入相关字符串开头的变量 + || ruleStr.startsWith("tag")//js首个字符串不可能是tag,因为并未注入相关字符串开头的变量 + || ruleStr.startsWith("text")//js首个字符串不可能是text,因为并未注入相关字符串开头的变量 + || ruleStr.startsWith("children")//js首个字符串不可能是children,因为并未注入相关字符串开头的变量 } } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 919f970e9..13992fc79 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -55,8 +55,7 @@ class AnalyzeUrl( private var retry: Int = 0 init { - val pos = baseUrl.indexOf(',') - if(pos != -1)baseUrl = baseUrl.substring(0,pos) + baseUrl = baseUrl.substringBefore(",{") headerMapF?.let { headerMap.putAll(it) if (it.containsKey("proxy")) { @@ -141,11 +140,11 @@ class AnalyzeUrl( /** * 处理URL */ - private fun initUrl() { + private fun initUrl() { //前面已经替换掉所有额外内容,此处url是基础形式,可以直接切首个‘,’之前字符串 var pos = ruleUrl.indexOf(',') - urlHasQuery = if(pos == -1) ruleUrl else ruleUrl.substring(0,pos) + urlHasQuery = ruleUrl.substringBefore(',') url = NetworkUtils.getAbsoluteURL(baseUrl,urlHasQuery ) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt b/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt index 050625dfc..ab58e5914 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt @@ -391,12 +391,15 @@ class RuleAnalyzer(data: String, code: Boolean = false) { }.toString() } + //-----------此处向下的函数和变量都未被使用,但以后要用-------- + val ruleTypeList = ArrayList() //设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced enum class Mode { XPath, Json, Default, Js, Regex } + /** * 不用正则,不到最后不切片也不用中间变量存储,只在序列中标记当前查找字段的开头结尾,到返回时才切片,高效快速准确切割规则 * 解决jsonPath自带的"&&"和"||"与阅读的规则冲突,以及规则正则或字符串中包含"&&"、"||"、"%%"、"@"导致的冲突 @@ -515,7 +518,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { isUrlList = true } else -> { - isJSON = queue.toString().isJson() + isJSON = queue.isJson() } } @@ -553,7 +556,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) { private val STARTSTR = arrayOf("@js:","","","##","@@","@" ,"{{@", "{{","}}" ,"}" - , "{@", "{/", "{$", "{?" + , "{@", "{/", "{$" , "{class" , "{id" , "{tag" diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index ca3643caa..59a074985 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -12,7 +12,6 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.AppConfig import io.legado.app.help.ReadBookConfig -import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChar import io.legado.app.ui.book.read.page.entities.TextLine @@ -147,10 +146,7 @@ object ChapterProvider { return TextChapter( bookChapter.index, bookChapter.title, - bookChapter.getAbsoluteURL().run{ - val pos = indexOf(',') - if(pos == -1) this else substring(0,pos) - }, + bookChapter.getAbsoluteURL().substringBefore(",{"), textPages, chapterSize ) } diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index e5ab0972d..3bdd322ea 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -13,7 +13,7 @@ object HtmlFormatter { return html.replace(wrapHtmlRegex, "\n") .replace(otherRegex, "") .replace("\\s*\\n+\\s*".toRegex(), "\n  ") - .replace("^[\\n\\s]*".toRegex(), "  ") + .replace("^[\\n\\s]+".toRegex(), "  ") .replace("[\\n\\s]+$".toRegex(), "") } @@ -26,7 +26,7 @@ object HtmlFormatter { .replace("[\\n\\s]+\$|^[\\n\\s]*".toRegex(), "") .replace("\\s*\\n+\\s*".toRegex(), "\n") - val sb = StringBuffer("  ") //前置缩减 + val sb = StringBuffer() val hasDataType:Boolean //是否有数据属性 //图片有data-开头的数据属性时优先用数据属性作为src,没有数据属性时匹配src @@ -48,7 +48,7 @@ object HtmlFormatter { if(hasDataType || matcher.group(1)!!.indexOf(',') == -1) { //图片无参 do{ - sb.append(keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ")) //非图片部分换行缩减 + sb.append(keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ")) //非图片部分换行缩进 sb.append( "" ) From ffdc0d4b174a697a6c9bb58644cb85b1ad67978d Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Sat, 17 Jul 2021 01:57:22 +0800 Subject: [PATCH 02/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=88=86=E5=89=B2=E8=A7=84=E5=88=99=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=99=84=E5=8A=A0=E5=8F=82=E6=95=B0=E4=B8=AD=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=20,{=20=E6=97=B6=E5=AF=BC=E8=87=B4=E8=A7=84=E5=88=99=E5=88=87?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复这种页数列表写法中,js部分内含 < 或 > 会切割错误的问题 格式化图片和匹配图片规则不在使用同一个正则,加快图片处理速度。 优化一下不需要循环执行却被循环执行的操作 --- app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 13992fc79..1ed802c6f 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -140,7 +140,7 @@ class AnalyzeUrl( /** * 处理URL */ - private fun initUrl() { //前面已经替换掉所有额外内容,此处url是基础形式,可以直接切首个‘,’之前字符串 + private fun initUrl() { //replaceKeyPageJs已经替换掉额外内容,此处url是基础形式,可以直接切首个‘,’之前字符串。 var pos = ruleUrl.indexOf(',') From 07dcc01842a126d7187dadfe0ca86fa57d40a7d9 Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Sat, 17 Jul 2021 02:15:39 +0800 Subject: [PATCH 03/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=88=86=E5=89=B2=E8=A7=84=E5=88=99=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=99=84=E5=8A=A0=E5=8F=82=E6=95=B0=E4=B8=AD=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=20,{=20=E6=97=B6=E5=AF=BC=E8=87=B4=E8=A7=84=E5=88=99=E5=88=87?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复这种页数列表写法中,js部分内含 < 或 > 会切割错误的问题 格式化图片和匹配图片规则不在使用同一个正则,加快图片处理速度。 优化一下不需要循环执行却被循环执行的操作 --- .../java/io/legado/app/constant/AppPattern.kt | 10 +++---- .../io/legado/app/service/help/ReadBook.kt | 30 +++++++------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/AppPattern.kt b/app/src/main/java/io/legado/app/constant/AppPattern.kt index 450347735..b664fee7c 100644 --- a/app/src/main/java/io/legado/app/constant/AppPattern.kt +++ b/app/src/main/java/io/legado/app/constant/AppPattern.kt @@ -5,14 +5,14 @@ import java.util.regex.Pattern @Suppress("RegExpRedundantEscape") object AppPattern { val JS_PATTERN: Pattern = - Pattern.compile("([\\w\\W]*?|@js:[\\w\\W]*$)", Pattern.CASE_INSENSITIVE) + Pattern.compile("([\\w\\W]+?)|@js:([\\w\\W]*)", Pattern.CASE_INSENSITIVE) val EXP_PATTERN: Pattern = Pattern.compile("\\{\\{([\\w\\W]*?)\\}\\}") - //图片有data-开头的数据属性时优先用数据属性作为src,没有数据属性时才匹配src - val imgPattern: Pattern = - Pattern.compile("]*data-)[^>]*src|[^>]*data-)[^=]*= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>", Pattern.CASE_INSENSITIVE) + + //匹配格式化后的图片格式 + val imgPattern: Pattern = Pattern.compile("]+)\">") val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著") val authorRegex = Regex("^.*?作\\s*者[::\\s]*|\\s+著") val fileNameRegex = Regex("[\\\\/:*?\"<>|.]") val splitGroupRegex = Regex("[,;,;]") -} +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index 56a8f4bb7..62ab815c0 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -415,30 +415,22 @@ object ReadBook { else -> chapter.title } val contents = contentProcessor!!.getContent(book, chapter.title, content) - when (chapter.index) { - durChapterIndex -> { - curTextChapter = - ChapterProvider.getTextChapter( - book, chapter, contents, chapterSize - ) - if (upContent) callBack?.upContent(resetPageOffset = resetPageOffset) + val textChapter = ChapterProvider.getTextChapter(book, chapter,contents,chapterSize) + + val offset = chapter.index - durChapterIndex + if (upContent) callBack?.upContent(offset ,resetPageOffset) + when (offset) { + 0 -> { + curTextChapter = textChapter callBack?.upView() curPageChanged() callBack?.contentLoadFinish() } - durChapterIndex - 1 -> { - prevTextChapter = - ChapterProvider.getTextChapter( - book, chapter, contents, chapterSize - ) - if (upContent) callBack?.upContent(-1, resetPageOffset) + - 1 -> { + prevTextChapter = textChapter } - durChapterIndex + 1 -> { - nextTextChapter = - ChapterProvider.getTextChapter( - book, chapter, contents, chapterSize - ) - if (upContent) callBack?.upContent(1, resetPageOffset) + 1 -> { + nextTextChapter = textChapter } } } From 0871341b21e5348dd83c11109b57f57704222382 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 02:19:51 +0800 Subject: [PATCH 04/20] Update ReadBook.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- .../io/legado/app/service/help/ReadBook.kt | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index 56a8f4bb7..f2b765b9b 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -415,30 +415,22 @@ object ReadBook { else -> chapter.title } val contents = contentProcessor!!.getContent(book, chapter.title, content) - when (chapter.index) { - durChapterIndex -> { - curTextChapter = - ChapterProvider.getTextChapter( - book, chapter, contents, chapterSize - ) - if (upContent) callBack?.upContent(resetPageOffset = resetPageOffset) + val textChapter = ChapterProvider.getTextChapter(book, chapter,contents,chapterSize) + + val offset = chapter.index - durChapterIndex + if (upContent) callBack?.upContent(offset ,resetPageOffset) + when (offset) { + 0 -> { + curTextChapter = textChapter callBack?.upView() curPageChanged() callBack?.contentLoadFinish() } - durChapterIndex - 1 -> { - prevTextChapter = - ChapterProvider.getTextChapter( - book, chapter, contents, chapterSize - ) - if (upContent) callBack?.upContent(-1, resetPageOffset) + - 1 -> { + prevTextChapter = textChapter } - durChapterIndex + 1 -> { - nextTextChapter = - ChapterProvider.getTextChapter( - book, chapter, contents, chapterSize - ) - if (upContent) callBack?.upContent(1, resetPageOffset) + 1 -> { + nextTextChapter = textChapter } } } @@ -501,4 +493,4 @@ object ReadBook { fun upPageAnim() } -} \ No newline at end of file +} From a5319a8a20bf69e35e8741f5a1709c14e820a82f Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 02:21:14 +0800 Subject: [PATCH 05/20] Update AppPattern.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- app/src/main/java/io/legado/app/constant/AppPattern.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/AppPattern.kt b/app/src/main/java/io/legado/app/constant/AppPattern.kt index 450347735..672959e14 100644 --- a/app/src/main/java/io/legado/app/constant/AppPattern.kt +++ b/app/src/main/java/io/legado/app/constant/AppPattern.kt @@ -5,11 +5,11 @@ import java.util.regex.Pattern @Suppress("RegExpRedundantEscape") object AppPattern { val JS_PATTERN: Pattern = - Pattern.compile("([\\w\\W]*?|@js:[\\w\\W]*$)", Pattern.CASE_INSENSITIVE) + Pattern.compile("([\\w\\W]+?)|@js:([\\w\\W]*)", Pattern.CASE_INSENSITIVE) val EXP_PATTERN: Pattern = Pattern.compile("\\{\\{([\\w\\W]*?)\\}\\}") - //图片有data-开头的数据属性时优先用数据属性作为src,没有数据属性时才匹配src - val imgPattern: Pattern = - Pattern.compile("]*data-)[^>]*src|[^>]*data-)[^=]*= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>", Pattern.CASE_INSENSITIVE) + + //匹配格式化后的图片格式 + val imgPattern: Pattern = Pattern.compile("]+)\">") val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著") val authorRegex = Regex("^.*?作\\s*者[::\\s]*|\\s+著") From 7f7e472bf8a971932d707e0860b5466d8268b58c Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 03:34:54 +0800 Subject: [PATCH 06/20] Update HtmlFormatter.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- app/src/main/java/io/legado/app/utils/HtmlFormatter.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index 3bdd322ea..f7deb29a5 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -59,16 +59,16 @@ object HtmlFormatter { do{ val url = matcher.group(1)!! - val pos = url.indexOf(',') + val urlBefore = url.substringBefore(',') sb.append(keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ")) //非图片部分换行缩减 sb.append( "" ) appendPos = matcher.end() From c74832fb71a17e026622f74ed4fe2bf306738f86 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 03:42:55 +0800 Subject: [PATCH 07/20] Update HtmlFormatter.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- app/src/main/java/io/legado/app/utils/HtmlFormatter.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index f7deb29a5..48fd62243 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -59,16 +59,16 @@ object HtmlFormatter { do{ val url = matcher.group(1)!! - val urlBefore = url.substringBefore(',') + val pos = url.indexOf(',') sb.append(keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ")) //非图片部分换行缩减 sb.append( "" ) appendPos = matcher.end() From 4290ad8fd1f090532c41213dd998e3adf4689814 Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Sat, 17 Jul 2021 04:01:38 +0800 Subject: [PATCH 08/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=88=86=E5=89=B2=E8=A7=84=E5=88=99=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=99=84=E5=8A=A0=E5=8F=82=E6=95=B0=E4=B8=AD=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=20,{=20=E6=97=B6=E5=AF=BC=E8=87=B4=E8=A7=84=E5=88=99=E5=88=87?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复这种页数列表写法中,js部分内含 < 或 > 会切割错误的问题 格式化图片和匹配图片规则不在使用同一个正则,加快图片处理速度。 优化一下不需要循环执行却被循环执行的操作 --- app/src/main/java/io/legado/app/utils/HtmlFormatter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index 3bdd322ea..b4ed4e83e 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -68,7 +68,7 @@ object HtmlFormatter { url.substring(0, pos) ) },${ - url.substring(pos) + url.substring(pos + 1 ) }\">" ) appendPos = matcher.end() From e85a7ac5cbccac9972eb0067cce911a17d836d68 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 04:02:07 +0800 Subject: [PATCH 09/20] Update HtmlFormatter.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 简化 --- app/src/main/java/io/legado/app/utils/HtmlFormatter.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index 48fd62243..b4ed4e83e 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -65,10 +65,10 @@ object HtmlFormatter { "" ) appendPos = matcher.end() From de434c576b9b6c875b86c89c0fb97231ece42a6b Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 04:19:32 +0800 Subject: [PATCH 10/20] Update HtmlFormatter.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- app/src/main/java/io/legado/app/utils/HtmlFormatter.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index b4ed4e83e..0f6aa3d71 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -60,7 +60,8 @@ object HtmlFormatter { do{ val url = matcher.group(1)!! val pos = url.indexOf(',') - sb.append(keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ")) //非图片部分换行缩减 + val beforeStr = keepImgHtml.substring(appendPos, matcher.start()) + sb.append( if(beforeStr == "\n") '\n' else beforeStr.replace("\n","\n  ") ) //缩进换行下个非图片段落 sb.append( " Date: Sat, 17 Jul 2021 04:35:49 +0800 Subject: [PATCH 11/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=88=86=E5=89=B2=E8=A7=84=E5=88=99=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=99=84=E5=8A=A0=E5=8F=82=E6=95=B0=E4=B8=AD=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=20,{=20=E6=97=B6=E5=AF=BC=E8=87=B4=E8=A7=84=E5=88=99=E5=88=87?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复这种页数列表写法中,js部分内含 < 或 > 会切割错误的问题 格式化图片和匹配图片规则不在使用同一个正则,加快图片处理速度。 优化一下不需要循环执行却被循环执行的操作 --- .../read/page/provider/ChapterProvider.kt | 37 ++++++++++--------- .../java/io/legado/app/utils/HtmlFormatter.kt | 14 ++++--- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 59a074985..494f628a4 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -111,26 +111,27 @@ object ChapterProvider { ) } } else if (book.getImageStyle() != Book.imgStyleText) { - content.replace(AppPattern.imgPattern.toRegex(), "\n\$0\n") - .split("\n").forEach { text -> - if (text.isNotBlank()) { - if (!text.startsWith(" + if (!text.startsWith("" ) appendPos = matcher.end() @@ -78,7 +79,8 @@ object HtmlFormatter { } if (appendPos < keepImgHtml.length) { - sb.append(keepImgHtml.substring(appendPos, keepImgHtml.length).replace("\n","\n  ")) //非图片部分换行缩减 + val beforeStr = keepImgHtml.substring(appendPos, keepImgHtml.length) + sb.append( if(beforeStr == "\n") '\n' else beforeStr.replace("\n","\n  ") ) //缩进换行下个非图片段落 } return sb.toString() } From 74754739480ae60e24f2ae91951f93e02e9604ec Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 04:37:20 +0800 Subject: [PATCH 12/20] Update HtmlFormatter.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- .../main/java/io/legado/app/utils/HtmlFormatter.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index 0f6aa3d71..5f69f5b9f 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -59,17 +59,17 @@ object HtmlFormatter { do{ val url = matcher.group(1)!! - val pos = url.indexOf(',') + val urlBefore = url.substringBefore(',') val beforeStr = keepImgHtml.substring(appendPos, matcher.start()) sb.append( if(beforeStr == "\n") '\n' else beforeStr.replace("\n","\n  ") ) //缩进换行下个非图片段落 sb.append( "" ) appendPos = matcher.end() @@ -79,7 +79,8 @@ object HtmlFormatter { } if (appendPos < keepImgHtml.length) { - sb.append(keepImgHtml.substring(appendPos, keepImgHtml.length).replace("\n","\n  ")) //非图片部分换行缩减 + val beforeStr = keepImgHtml.substring(appendPos, keepImgHtml.length) + sb.append( if(beforeStr == "\n") '\n' else beforeStr.replace("\n","\n  ") ) //缩进换行下个非图片段落 } return sb.toString() } From eec9558a18e1da7dcb82c50f601fe6c7ba895e92 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 04:39:02 +0800 Subject: [PATCH 13/20] Update ChapterProvider.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- .../read/page/provider/ChapterProvider.kt | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 59a074985..494f628a4 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -111,26 +111,27 @@ object ChapterProvider { ) } } else if (book.getImageStyle() != Book.imgStyleText) { - content.replace(AppPattern.imgPattern.toRegex(), "\n\$0\n") - .split("\n").forEach { text -> - if (text.isNotBlank()) { - if (!text.startsWith(" + if (!text.startsWith(" Date: Sat, 17 Jul 2021 04:47:54 +0800 Subject: [PATCH 14/20] Update ChapterProvider.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 回退 --- .../read/page/provider/ChapterProvider.kt | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 494f628a4..59a074985 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -111,27 +111,26 @@ object ChapterProvider { ) } } else if (book.getImageStyle() != Book.imgStyleText) { - var contentX = content.replace(AppPattern.imgPattern.toRegex(), "\n\$0\n").split("\n") - val start = contentX.first() == "\n" - val end = contentX.last() == "\n" - if(start || end )contentX = contentX.subList(if(start) 1 else 0,if(end) contentX.size-1 else contentX.size ) - contentX.forEach { text -> - if (!text.startsWith(" + if (text.isNotBlank()) { + if (!text.startsWith(" Date: Sat, 17 Jul 2021 04:55:48 +0800 Subject: [PATCH 15/20] Update HtmlFormatter.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- app/src/main/java/io/legado/app/utils/HtmlFormatter.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index 5f69f5b9f..8f9fffe02 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -60,8 +60,7 @@ object HtmlFormatter { do{ val url = matcher.group(1)!! val urlBefore = url.substringBefore(',') - val beforeStr = keepImgHtml.substring(appendPos, matcher.start()) - sb.append( if(beforeStr == "\n") '\n' else beforeStr.replace("\n","\n  ") ) //缩进换行下个非图片段落 + sb.append( keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ") ) //缩进换行下个非图片段落 sb.append( " Date: Sat, 17 Jul 2021 07:38:08 +0800 Subject: [PATCH 16/20] Update AnalyzeRule.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修正 --- .../java/io/legado/app/model/analyzeRule/AnalyzeRule.kt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index fd814d00d..225be549c 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -597,13 +597,9 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { private fun isRule(ruleStr: String): Boolean { return ruleStr.startsWith('@') //js首个字符不可能是@,除非是装饰器,所以@开头规定为规则 - || ruleStr.startsWith('$') //js首个字符不可能是$,因为并未注入$变量,且$1之类的东西已被替换,不会影响此处判断 + || ruleStr.startsWith("$.") + || ruleStr.startsWith("$[") || ruleStr.startsWith("//") - || ruleStr.startsWith("class")//js首个字符串不可能是class,因为并未注入相关字符串开头的变量 - || ruleStr.startsWith("id")//js首个字符串不可能是id,因为并未注入相关字符串开头的变量 - || ruleStr.startsWith("tag")//js首个字符串不可能是tag,因为并未注入相关字符串开头的变量 - || ruleStr.startsWith("text")//js首个字符串不可能是text,因为并未注入相关字符串开头的变量 - || ruleStr.startsWith("children")//js首个字符串不可能是children,因为并未注入相关字符串开头的变量 } } From 2312e24f0ce92d7e862984323fae15c7b19de199 Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Sat, 17 Jul 2021 12:47:34 +0800 Subject: [PATCH 17/20] Update HtmlFormatter.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- .../java/io/legado/app/utils/HtmlFormatter.kt | 72 ++++++++----------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index 8f9fffe02..4dfb2c35f 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -11,10 +11,10 @@ object HtmlFormatter { fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String { html ?: return "" return html.replace(wrapHtmlRegex, "\n") - .replace(otherRegex, "") - .replace("\\s*\\n+\\s*".toRegex(), "\n  ") - .replace("^[\\n\\s]+".toRegex(), "  ") - .replace("[\\n\\s]+$".toRegex(), "") + .replace(otherRegex, "") + .replace("\\s*\\n+\\s*".toRegex(), "\n  ") + .replace("^[\\n\\s]+".toRegex(), "  ") + .replace("[\\n\\s]+$".toRegex(), "") } fun formatKeepImg(html: String?) = format(html, notImgHtmlRegex) @@ -22,59 +22,43 @@ object HtmlFormatter { fun formatKeepImg(html: String?, redirectUrl: URL?): String { html ?: return "" val keepImgHtml = html.replace(wrapHtmlRegex, "\n") - .replace(notImgHtmlRegex, "") - .replace("[\\n\\s]+\$|^[\\n\\s]*".toRegex(), "") - .replace("\\s*\\n+\\s*".toRegex(), "\n") + .replace(notImgHtmlRegex, "") + .replace("[\\n\\s]+\$|^[\\n\\s]*".toRegex(), "") + .replace("\\s*\\n+\\s*".toRegex(), "\n") val sb = StringBuffer() val hasDataType:Boolean //是否有数据属性 //图片有data-开头的数据属性时优先用数据属性作为src,没有数据属性时匹配src val imgPattern = Pattern.compile( - if(keepImgHtml.matches("]*data-".toRegex())) { - hasDataType = true - "]*data-[^=]*= *\"([^\"])\"[^>]*>" - } - else { - hasDataType = false - "]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>" - }, Pattern.CASE_INSENSITIVE + if(keepImgHtml.matches("]*data-".toRegex())) { + hasDataType = true + "]*data-[^=]*= *\"([^\"])\"[^>]*>" + } + else { + hasDataType = false + "]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>" + }, Pattern.CASE_INSENSITIVE ) val matcher = imgPattern.matcher(keepImgHtml) var appendPos = 0 - if(matcher.find()){ - if(hasDataType || matcher.group(1)!!.indexOf(',') == -1) { //图片无参 - - do{ - sb.append(keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ")) //非图片部分换行缩进 - sb.append( "" ) - appendPos = matcher.end() - }while (matcher.find()) - - }else{ //图片有参 - - do{ - val url = matcher.group(1)!! - val urlBefore = url.substringBefore(',') - sb.append( keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ") ) //缩进换行下个非图片段落 - sb.append( - "" - ) - appendPos = matcher.end() - }while(matcher.find()) - - } + ) + }${ + url.substring(urlBefore.length) + }\">" + ) + appendPos = matcher.end() } if (appendPos < keepImgHtml.length) { From 5a4cbba1d2e241276cb3f1f77490c3cf529c6c03 Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Sun, 18 Jul 2021 00:23:46 +0800 Subject: [PATCH 18/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=88=86=E5=89=B2=E8=A7=84=E5=88=99=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=99=84=E5=8A=A0=E5=8F=82=E6=95=B0=E4=B8=AD=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=20,{=20=E6=97=B6=E5=AF=BC=E8=87=B4=E8=A7=84=E5=88=99=E5=88=87?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复这种页数列表写法中,js部分内含 < 或 > 会切割错误的问题 格式化图片和匹配图片规则不在使用同一个正则,加快图片处理速度。 优化一下不需要循环执行却被循环执行的操作 --- .../legado/app/data/entities/BookChapter.kt | 2 +- .../main/java/io/legado/app/model/Debug.kt | 2 +- .../app/model/analyzeRule/AnalyzeRule.kt | 47 ++++++++++--------- .../app/model/analyzeRule/AnalyzeUrl.kt | 2 +- .../read/page/provider/ChapterProvider.kt | 2 +- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index 77811cba7..c6b4cb9fb 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -62,7 +62,7 @@ data class BookChapter( } fun getAbsoluteURL():String{ - val urlBefore = url.substringBefore(",{") + val urlBefore = url.substringBefore(',') val urlAbsoluteBefore = NetworkUtils.getAbsoluteURL(baseUrl,urlBefore) return if(urlBefore.length == url.length) urlAbsoluteBefore else urlAbsoluteBefore + url.substring(urlBefore.length) } diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt index dc6259cc4..9908e9dc8 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -120,7 +120,7 @@ object Debug { infoDebug(scope, webBook, book) } key.contains("::") -> { - val url = key.substring(key.indexOf("::") + 2) + val url = key.substringAfter("::") log(webBook.sourceUrl, "⇒开始访问发现页:$url") exploreDebug(scope, webBook, url) } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 225be549c..11f967773 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -26,7 +26,7 @@ import kotlin.collections.HashMap @Suppress("unused", "RegExpRedundantEscape") class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { - var book: BaseBook? = if (ruleData is BaseBook) ruleData else null + var book = if (ruleData is BaseBook) ruleData else null var chapter: BookChapter? = null var nextChapterUrl: String? = null @@ -65,7 +65,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { fun setRedirectUrl(url: String): URL? { kotlin.runCatching { - redirectUrl = URL(url.substringBefore(",{")) + redirectUrl = URL(url.substringBefore(',')) } return redirectUrl } @@ -192,9 +192,9 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { @JvmOverloads fun getString( - ruleList: List, - isUrl: Boolean = false, - value: String? = null + ruleList: List, + isUrl: Boolean = false, + value: String? = null ): String { var result: Any? = value val content = this.content @@ -258,8 +258,8 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { result?.let { result = when (sourceRule.mode) { Mode.Regex -> AnalyzeByRegex.getElement( - result.toString(), - sourceRule.rule.splitNotBlank("&&") + result.toString(), + sourceRule.rule.splitNotBlank("&&") ) Mode.Js -> evalJS(sourceRule.rule, it) Mode.Json -> getAnalyzeByJSonPath(it).getObject(sourceRule.rule) @@ -289,8 +289,8 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { result?.let { result = when (sourceRule.mode) { Mode.Regex -> AnalyzeByRegex.getElements( - result.toString(), - sourceRule.rule.splitNotBlank("&&") + result.toString(), + sourceRule.rule.splitNotBlank("&&") ) Mode.Js -> evalJS(sourceRule.rule, result) Mode.Json -> getAnalyzeByJSonPath(it).getList(sourceRule.rule) @@ -378,11 +378,6 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { mMode = Mode.Json ruleStr0.substring(6) } - ruleStr0.startsWith(":") -> { //:与伪类选择器冲突,建议改成?更合理 - mMode = Mode.Regex - isRegex = true - ruleStr0.substring(1) - } ( ruleStr0[1] == '.' || ruleStr0[1] == '[') && ruleStr0[0] == '$' || isJSON -> { mMode = Mode.Json ruleStr0 @@ -393,8 +388,14 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { } } //拆分为规则列表 - var start = 0 + var start = if(ruleStr.startsWith(":")){ //仅首字符为:时为AllInOne,其实:与伪类选择器冲突,建议改成?更合理 + mMode = Mode.Regex + isRegex = true + 1 + }else 0 var tmp: String + + val jsMatcher = JS_PATTERN.matcher(ruleStr) while (jsMatcher.find()){ @@ -566,8 +567,8 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { jsEval == null -> Unit jsEval is String -> infoVal.insert(0, jsEval) jsEval is Double && jsEval % 1.0 == 0.0 -> infoVal.insert( - 0, - String.format("%.0f", jsEval) + 0, + String.format("%.0f", jsEval) ) else -> infoVal.insert(0, jsEval.toString()) } @@ -609,8 +610,8 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { fun put(key: String, value: String): String { chapter?.putVariable(key, value) - ?: book?.putVariable(key, value) - ?: ruleData.putVariable(key, value) + ?: book?.putVariable(key, value) + ?: ruleData.putVariable(key, value) return value } @@ -624,9 +625,9 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { } } return chapter?.variableMap?.get(key) - ?: book?.variableMap?.get(key) - ?: ruleData.variableMap[key] - ?: "" + ?: book?.variableMap?.get(key) + ?: ruleData.variableMap[key] + ?: "" } /** @@ -678,7 +679,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { companion object { private val putPattern = Pattern.compile("@put:(\\{[^}]+?\\})", Pattern.CASE_INSENSITIVE) private val evalPattern = - Pattern.compile("@get:\\{[^}]+?\\}|\\{\\{[\\w\\W]*?\\}\\}", Pattern.CASE_INSENSITIVE) + Pattern.compile("@get:\\{[^}]+?\\}|\\{\\{[\\w\\W]*?\\}\\}", Pattern.CASE_INSENSITIVE) private val regexPattern = Pattern.compile("\\$\\d{1,2}") private val titleNumPattern = Pattern.compile("(第)(.+?)(章)") } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 1ed802c6f..9dc1fcc72 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -55,7 +55,7 @@ class AnalyzeUrl( private var retry: Int = 0 init { - baseUrl = baseUrl.substringBefore(",{") + baseUrl = baseUrl.substringBefore(',') headerMapF?.let { headerMap.putAll(it) if (it.containsKey("proxy")) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 494f628a4..c58c38b9f 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -147,7 +147,7 @@ object ChapterProvider { return TextChapter( bookChapter.index, bookChapter.title, - bookChapter.getAbsoluteURL().substringBefore(",{"), + bookChapter.getAbsoluteURL().substringBefore(','), textPages, chapterSize ) } From 223b72430676e4850651f2a3947d697f1538ea6d Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Sun, 18 Jul 2021 00:54:24 +0800 Subject: [PATCH 19/20] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=88=86=E5=89=B2=E8=A7=84=E5=88=99=EF=BC=8C=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=99=84=E5=8A=A0=E5=8F=82=E6=95=B0=E4=B8=AD=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=20,{=20=E6=97=B6=E5=AF=BC=E8=87=B4=E8=A7=84=E5=88=99=E5=88=87?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复这种页数列表写法中,js部分内含 < 或 > 会切割错误的问题 格式化图片和匹配图片规则不在使用同一个正则,加快图片处理速度。 优化一下不需要循环执行却被循环执行的操作 --- .../app/model/analyzeRule/AnalyzeRule.kt | 3 -- .../app/model/analyzeRule/AnalyzeUrl.kt | 10 ++--- .../read/page/provider/ChapterProvider.kt | 37 +++++++++---------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 11f967773..3753738eb 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -394,10 +394,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { 1 }else 0 var tmp: String - - val jsMatcher = JS_PATTERN.matcher(ruleStr) - while (jsMatcher.find()){ if (jsMatcher.start() > start) { tmp = ruleStr.substring(start, jsMatcher.start()).trim { it <= ' ' } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 9dc1fcc72..90b4be570 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -142,18 +142,14 @@ class AnalyzeUrl( */ private fun initUrl() { //replaceKeyPageJs已经替换掉额外内容,此处url是基础形式,可以直接切首个‘,’之前字符串。 - var pos = ruleUrl.indexOf(',') - urlHasQuery = ruleUrl.substringBefore(',') - url = NetworkUtils.getAbsoluteURL(baseUrl,urlHasQuery ) - NetworkUtils.getBaseUrl(url)?.let { baseUrl = it } - if(pos != -1 ) { - GSON.fromJsonObject(ruleUrl.substring(pos + 1).trim{ it < '!'})?.let { option -> + if(urlHasQuery.length != ruleUrl.length ) { + GSON.fromJsonObject(ruleUrl.substring(urlHasQuery.length + 1).trim{ it < '!'})?.let { option -> option.method?.let { if (it.equals("POST", true)) method = RequestMethod.POST @@ -191,7 +187,7 @@ class AnalyzeUrl( when (method) { RequestMethod.GET -> { if (!useWebView) { - pos = url.indexOf('?') + val pos = url.indexOf('?') if(pos != -1) { analyzeFields(url.substring(pos + 1)) url = url.substring(0,pos) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index c58c38b9f..c3bbf8b6e 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -111,27 +111,26 @@ object ChapterProvider { ) } } else if (book.getImageStyle() != Book.imgStyleText) { - var contentX = content.replace(AppPattern.imgPattern.toRegex(), "\n\$0\n").split("\n") - val start = contentX.first() == "\n" - val end = contentX.last() == "\n" - if(start || end )contentX = contentX.subList(if(start) 1 else 0,if(end) contentX.size-1 else contentX.size ) - contentX.forEach { text -> - if (!text.startsWith(" + if(text.isNotBlank()){ + if (!text.startsWith(" Date: Sun, 18 Jul 2021 01:10:07 +0800 Subject: [PATCH 20/20] Update AnalyzeRule.kt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 --- .../java/io/legado/app/model/analyzeRule/AnalyzeRule.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 3753738eb..1ad285563 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -121,7 +121,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { @JvmOverloads fun getStringList(rule: String?, isUrl: Boolean = false): List? { if (rule.isNullOrEmpty()) return null - val ruleList = splitSourceRule(rule) + val ruleList = splitSourceRule(rule,true) return getStringList(ruleList, isUrl) } @@ -281,7 +281,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { @Suppress("UNCHECKED_CAST") fun getElements(ruleStr: String): List { var result: Any? = null - val ruleList = splitSourceRule(ruleStr) + val ruleList = splitSourceRule(ruleStr,true) content?.let { o -> if (ruleList.isNotEmpty()) result = o for (sourceRule in ruleList) { @@ -356,7 +356,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { /** * 分解规则生成规则列表 */ - fun splitSourceRule(ruleStr: String?): List { + fun splitSourceRule(ruleStr: String?,isList:Boolean = false): List { if (ruleStr.isNullOrEmpty()) return ArrayList() val ruleList = ArrayList() //检测Mode @@ -387,8 +387,9 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions { ruleStr0 } } + //拆分为规则列表 - var start = if(ruleStr.startsWith(":")){ //仅首字符为:时为AllInOne,其实:与伪类选择器冲突,建议改成?更合理 + var start = if(isList && ruleStr.startsWith(":")){ //仅首字符为:时为AllInOne,其实:与伪类选择器冲突,建议改成?更合理 mMode = Mode.Regex isRegex = true 1