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 10a7659f4..96dbf045b 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -22,7 +22,10 @@ object HtmlFormatter { fun formatKeepImg(html: String?, redirectUrl: URL?): String { html ?: return "" - val keepImgHtml = format(html, notImgHtmlRegex) + val keepImgHtml = html.replace(wrapHtmlRegex, "\n") + .replace(notImgHtmlRegex, "") + .replace("\\n\\s*$|^\\s*\\n".toRegex(), "") + .replace("\\n\\s*\\n".toRegex(), "\n") var str = StringBuffer() var endPos = 0 @@ -47,28 +50,20 @@ object HtmlFormatter { var appendPos0 = 0 val matcher0 = Pattern.compile("]*src *= *\"([^\"]+)\"[^>]*>", Pattern.CASE_INSENSITIVE).matcher(strBefore) //格式化普通图片标签 while (matcher0.find()) { - val strBefore0 = strBefore.substring(appendPos0, matcher0.start()) - sb.append( if (strBefore0.isBlank()) strBefore0 else strBefore0.replace("\n","\n  ")) - sb.append("") + sb.append(strBefore.substring(appendPos0, matcher0.start()),"") appendPos0 = matcher0.end() } - strBefore = if (appendPos0 < strBefore.length) strBefore.substring(appendPos0, strBefore.length) else "" + strBefore = strBefore.substring(appendPos0, strBefore.length) } - sb.append( - if (strBefore.isBlank()) strBefore else strBefore.replace("\n","\n  ") //缩进图片之间的非空白段落 - ) - - if (pos == 0) { - val url = matcher.group(1)!! - val urlMatcher = AnalyzeUrl.paramPattern.matcher(url) - val find = urlMatcher.find() - sb.append("") - - } else sb.append("") + }else NetworkUtils.getAbsoluteURL(redirectUrl,matcher.group(1)!!) + }\">") appendPos = matcher.end() } while (matcher.find()) @@ -83,7 +78,7 @@ object HtmlFormatter { } if (endPos < keepImgHtml.length) { - str.append( ( + str.append( if(hasMatchX){ //处理末尾的普通图片标签 var appendPos0 = 0 val strBefore = keepImgHtml.substring(endPos, keepImgHtml.length) @@ -91,28 +86,13 @@ object HtmlFormatter { Pattern.compile("]*src *= *\"([^\"]+)\"[^>]*>", Pattern.CASE_INSENSITIVE) .matcher(strBefore) //格式化普通图片标签 while (matcher0.find()) { - val strBefore0 = strBefore.substring(appendPos0, matcher0.start()) - str.append( - if (strBefore0.isBlank()) strBefore0 else strBefore0.replace( - "\n", - "\n  " - ) - ) - str.append( - "" - ) + str.append(strBefore.substring(appendPos0, matcher0.start()),"") appendPos0 = matcher0.end() } - if (appendPos0 < strBefore.length) strBefore.substring( appendPos0, strBefore.length ) else "" + strBefore.substring( appendPos0, strBefore.length ) }else keepImgHtml.substring( endPos, keepImgHtml.length ) - ).replace("\n","\n  ") ) //缩进图片之后的非空白段落 + ) //缩进图片之后的非空白段落 } - return str.toString() } }