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] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=93=BE=E6=8E=A5=E5=88=86?= =?UTF-8?q?=E5=89=B2=E8=A7=84=E5=88=99=EF=BC=8C=E9=81=BF=E5=85=8D=E9=99=84?= =?UTF-8?q?=E5=8A=A0=E5=8F=82=E6=95=B0=E4=B8=AD=E5=AD=98=E5=9C=A8=20,{=20?= =?UTF-8?q?=E6=97=B6=E5=AF=BC=E8=87=B4=E8=A7=84=E5=88=99=E5=88=87=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( "" )