From d5e49b313b3bb3dea8deb3584521e70628296682 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 18 Jul 2021 23:55:48 +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 --- .../java/io/legado/app/utils/HtmlFormatter.kt | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 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 664525973..a88a59995 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -8,6 +8,10 @@ object HtmlFormatter { private val wrapHtmlRegex = "]*>".toRegex() private val notImgHtmlRegex = "])[^<>]*>".toRegex() private val otherHtmlRegex = "])[^<>]*>".toRegex() + private val formatImagePattern = Pattern.compile( + "]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|]*data-[^=]*= *\"([^\"]*)\"[^>]*>|]*src *= *\"([^\"]*)\"[^>]*>", + Pattern.CASE_INSENSITIVE + ) fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String { html ?: return "" @@ -27,24 +31,32 @@ object HtmlFormatter { .replace("\\n\\s*$|^\\s*\\n".toRegex(), "") .replace("\\n\\s*\\n".toRegex(), "\n") //正则的“|”处于顶端而不处于()中时,具有类似||的熔断效果,故以此机制简化原来的代码 - val matcher = Pattern.compile("]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|]*data-[^=]*= *\"([^\"]*)\"[^>]*>|]*src *= *\"([^\"]*)\"[^>]*>", Pattern.CASE_INSENSITIVE).matcher(keepImgHtml) + val matcher = formatImagePattern.matcher(keepImgHtml) var appendPos = 0 val sb = StringBuffer() - while (matcher.find()){ + while (matcher.find()) { var param = "" - sb.append( keepImgHtml.substring(appendPos, matcher.start()),"") + sb.append( + keepImgHtml.substring(appendPos, matcher.start()), "" + ) appendPos = matcher.end() } - if (appendPos < keepImgHtml.length) sb.append(keepImgHtml.substring( appendPos, keepImgHtml.length )) + if (appendPos < keepImgHtml.length) sb.append( + keepImgHtml.substring( + appendPos, + keepImgHtml.length + ) + ) return sb.toString() } }