From 6a961052a268544a23185390433feab6d12a03dd Mon Sep 17 00:00:00 2001 From: bushixuanqi <57338301+bushixuanqi@users.noreply.github.com> Date: Mon, 19 Jul 2021 14:51:38 +0800 Subject: [PATCH] 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 | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 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 a88a59995..afc2d0cc8 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -9,17 +9,17 @@ object HtmlFormatter { private val notImgHtmlRegex = "])[^<>]*>".toRegex() private val otherHtmlRegex = "])[^<>]*>".toRegex() private val formatImagePattern = Pattern.compile( - "]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|]*data-[^=]*= *\"([^\"]*)\"[^>]*>|]*src *= *\"([^\"]*)\"[^>]*>", - Pattern.CASE_INSENSITIVE + "]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|]*data-[^=]*= *\"([^\"]*)\"[^>]*>|]*src *= *\"([^\"]*)\"[^>]*>", + Pattern.CASE_INSENSITIVE ) 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) @@ -27,9 +27,9 @@ object HtmlFormatter { fun formatKeepImg(html: String?, redirectUrl: URL?): String { html ?: return "" val keepImgHtml = html.replace(wrapHtmlRegex, "\n") - .replace(notImgHtmlRegex, "") - .replace("\\n\\s*$|^\\s*\\n".toRegex(), "") - .replace("\\n\\s*\\n".toRegex(), "\n") + .replace(notImgHtmlRegex, "") + .replace("\\n\\s*$|^\\s*\\n".toRegex(), "") + .replace("\\n\\s*\\n".toRegex(), "\n") //正则的“|”处于顶端而不处于()中时,具有类似||的熔断效果,故以此机制简化原来的代码 val matcher = formatImagePattern.matcher(keepImgHtml) var appendPos = 0 @@ -37,8 +37,8 @@ object HtmlFormatter { while (matcher.find()) { var param = "" sb.append( - keepImgHtml.substring(appendPos, matcher.start()), "" + ) + param + }\">" ) appendPos = matcher.end() } if (appendPos < keepImgHtml.length) sb.append( - keepImgHtml.substring( - appendPos, - keepImgHtml.length - ) + keepImgHtml.substring( + appendPos, + keepImgHtml.length + ) ) return sb.toString() }