From 2b4242b4d8e9d892056a6562ff664f1055eeafad Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Mon, 12 Jul 2021 22:35:51 +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=20,{c?= =?UTF-8?q?=E5=8F=82=E6=95=B0}=20=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E4=B8=AD=E4=B9=9F=E5=AD=98=E5=9C=A8=20,{=20=EF=BC=8C=E5=AF=BC?= =?UTF-8?q?=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部分内含 < 或 > 就会切割错误的问题 优化格式化图片标签操作和匹配图片标签操作,加快图片处理速度 --- .../java/io/legado/app/constant/AppPattern.kt | 5 ++-- .../legado/app/data/entities/BookChapter.kt | 5 ++-- .../app/model/analyzeRule/AnalyzeRule.kt | 2 +- .../io/legado/app/service/help/ReadBook.kt | 30 +++++++------------ .../read/page/provider/ChapterProvider.kt | 24 +++++++-------- .../java/io/legado/app/utils/HtmlFormatter.kt | 29 +++++++----------- 6 files changed, 38 insertions(+), 57 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 f082f5898..c38e1359c 100644 --- a/app/src/main/java/io/legado/app/constant/AppPattern.kt +++ b/app/src/main/java/io/legado/app/constant/AppPattern.kt @@ -8,9 +8,8 @@ object AppPattern { Pattern.compile("([\\w\\W]*?|@js:[\\w\\W]*$)", Pattern.CASE_INSENSITIVE) val EXP_PATTERN: Pattern = Pattern.compile("\\{\\{([\\w\\W]*?)\\}\\}") - //非格式化时,不需要原来那么复杂的正则表达式 - val imgPattern: Pattern = - Pattern.compile("]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>", Pattern.CASE_INSENSITIVE) + //只匹配格式化后的图片 + val imgPattern: Pattern = Pattern.compile("") val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著") val authorRegex = Regex("^.*?作\\s*者[::\\s]*|\\s+著") 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 0cb7342fe..6162779cb 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 @@ -7,6 +7,7 @@ import androidx.room.Ignore import androidx.room.Index import io.legado.app.utils.GSON import io.legado.app.utils.MD5Utils +import io.legado.app.utils.NetworkUtils import io.legado.app.utils.fromJsonObject import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.Parcelize @@ -60,9 +61,7 @@ data class BookChapter( return false } - fun getAbsoluteURL():String{ - return url.replace("^\\s*([^,\\s]+)".toRex(),NetworkUtils.getAbsoluteURL(baseUrl,"$1")) - } + fun getAbsoluteURL() = url.replace("^\\s*([^,\\s]+)\\s*".toRegex()){ NetworkUtils.getAbsoluteURL(baseUrl,it.groupValues[1]) } 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 049d69a24..6bbd6f770 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 @@ -66,7 +66,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).trim{ it < '!'} ) + redirectUrl = URL( if(pos == -1)url else url.substring(pos+1).trim{ it < '!'} ) } return redirectUrl } 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 } } } 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 703daea42..baee65926 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 @@ -110,19 +110,11 @@ object ChapterProvider { isTitle, textPaint, srcList ) } - } else if (book.getImageStyle() != Book.imgStyleText) { - content.replace(AppPattern.imgPattern.toRegex(), "\n\$0\n") + } else { + content.replace(AppPattern.imgPattern.toRegex(), "\n\b\$0\n") .split("\n").forEach { text -> if (text.isNotBlank()) { - val matcher = AppPattern.imgPattern.matcher(text) - if (matcher.find()) { - matcher.group(1)?.let { src -> - durY = setTypeImage( - book, bookChapter, src, - durY, textPages, book.getImageStyle() - ) - } - } else { + if(text[0] != '\b'){ //非图片 val isTitle = index == 0 val textPaint = if (isTitle) titlePaint else contentPaint if (!(isTitle && ReadBookConfig.titleMode == 2)) { @@ -131,6 +123,11 @@ object ChapterProvider { stringBuilder, isTitle, textPaint ) } + } else { //图片 + durY = setTypeImage( + book, bookChapter, text.substring(1), + durY, textPages, book.getImageStyle() + ) } } } @@ -149,7 +146,10 @@ object ChapterProvider { return TextChapter( bookChapter.index, bookChapter.title, - bookChapter.getAbsoluteURL().split(',',limit = 1)[0], //bookChapter.getAbsoluteURL已经处理过,直接按','就行 + bookChapter.getAbsoluteURL().run{ + val pos = indexOf(',') + if(pos == -1) this else substring(0,pos) + }, 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 aa2f0f79a..26fa58429 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -25,31 +25,22 @@ object HtmlFormatter { val keepImgHtml = formatKeepImg(html) val sb = StringBuffer() - //图片有data-开头的数据属性时优先用数据属性作为src,没有数据属性时才匹配src - val hasData = keepImgHtml.matches("]*data-".toRegex()) - - val imgPatternX = if(hasData) Pattern.compile("]*data-[^=]*= *\"([^\"])\"[^>]*>", Pattern.CASE_INSENSITIVE) else imgPattern + //图片有data-开头的数据属性时优先用数据属性作为src,没有数据属性时匹配src + val imgPatternX = Pattern.compile( + if(keepImgHtml.matches("]*data-".toRegex())) "]*data-[^=]*= *\"([^\"])\"[^>]*>" + else "]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>", Pattern.CASE_INSENSITIVE + ) val matcher = imgPatternX.matcher(keepImgHtml) var appendPos = 0 while (matcher.find()) { - - var url = matcher.group(1)!! - val param:String - val pos = url.indexOf(',') - - url = NetworkUtils.getAbsoluteURL(redirectUrl, if(pos == -1){ - param = "" - url.trim{ it <'!'} - } else { - param = url.substring(pos+1).trim{ it <'!'} - url.substring(0,pos).trim{ it <'!'} - }) - sb.append(keepImgHtml.substring(appendPos, matcher.start())) - sb.append("") + sb.append("") appendPos = matcher.end() - } if (appendPos < keepImgHtml.length) { sb.append(keepImgHtml.substring(appendPos, keepImgHtml.length))