Update HtmlFormatter.kt

pull/1132/head
bushixuanqi 3 years ago committed by GitHub
parent a49947d815
commit c163d2e087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      app/src/main/java/io/legado/app/utils/HtmlFormatter.kt

@ -24,8 +24,7 @@ object HtmlFormatter {
html ?: return "" html ?: return ""
val keepImgHtml = html.replace(wrapHtmlRegex, "\n") val keepImgHtml = html.replace(wrapHtmlRegex, "\n")
.replace(notImgHtmlRegex, "") .replace(notImgHtmlRegex, "")
.replace("[\\n\\s]+\$|^[\\n\\s]+".toRegex(), "") .replace("\\n\\s*\\n".toRegex(), "\n")
.replace("\\s*\\n+\\s*".toRegex(), "\n")
var str = StringBuffer() var str = StringBuffer()
var endPos = 0 var endPos = 0
@ -50,29 +49,21 @@ object HtmlFormatter {
var appendPos0 = 0 var appendPos0 = 0
val matcher0 = Pattern.compile("<img[^>]*src *= *\"([^\"]+)\"[^>]*>", Pattern.CASE_INSENSITIVE).matcher(strBefore) //格式化普通图片标签 val matcher0 = Pattern.compile("<img[^>]*src *= *\"([^\"]+)\"[^>]*>", Pattern.CASE_INSENSITIVE).matcher(strBefore) //格式化普通图片标签
while (matcher0.find()) { while (matcher0.find()) {
val strBefore0 = strBefore.substring(appendPos0, matcher0.start()) sb.append(strBefore.substring(appendPos0, matcher0.start()),"<img src=\"${NetworkUtils.getAbsoluteURL(redirectUrl,matcher0.group(1)!!)}\">")
sb.append( if (strBefore0.isBlank()) strBefore0 else strBefore0.replace("\n","\n  "))
sb.append("<img src=\"${NetworkUtils.getAbsoluteURL(redirectUrl,matcher0.group(1)!!)}\">")
appendPos0 = matcher0.end() appendPos0 = matcher0.end()
} }
strBefore = if (appendPos0 < strBefore.length) strBefore.substring(appendPos0, strBefore.length) else "" strBefore = strBefore.substring(appendPos0, strBefore.length)
} }
sb.append( sb.append( strBefore,"<img src=\"${
if (strBefore.isBlank()) strBefore else strBefore.replace("\n","\n  ") //缩进图片之间的非空白段落
)
if (pos == 0) { if (pos == 0) {
val url = matcher.group(1)!! val url = matcher.group(1)!!
val urlMatcher = AnalyzeUrl.paramPattern.matcher(url) val urlMatcher = AnalyzeUrl.paramPattern.matcher(url)
val find = urlMatcher.find() if(urlMatcher.find()) NetworkUtils.getAbsoluteURL(redirectUrl,url.substring(0,urlMatcher.start())) + ',' + url.substring(urlMatcher.end())
sb.append("<img src=\"${
if(find) NetworkUtils.getAbsoluteURL(redirectUrl,url.substring(0,urlMatcher.start())) + ',' + url.substring(urlMatcher.end())
else url else url
}else NetworkUtils.getAbsoluteURL(redirectUrl,matcher.group(1)!!)
}\">") }\">")
} else sb.append("<img src=\"${NetworkUtils.getAbsoluteURL(redirectUrl,matcher.group(1)!!)}\">")
appendPos = matcher.end() appendPos = matcher.end()
} while (matcher.find()) } while (matcher.find())
hasMatch = false //普通图片标签已经处理过 hasMatch = false //普通图片标签已经处理过
@ -86,7 +77,7 @@ object HtmlFormatter {
} }
if (endPos < keepImgHtml.length) { if (endPos < keepImgHtml.length) {
str.append( ( str.append(
if(hasMatchX){ //处理末尾的普通图片标签 if(hasMatchX){ //处理末尾的普通图片标签
var appendPos0 = 0 var appendPos0 = 0
val strBefore = keepImgHtml.substring(endPos, keepImgHtml.length) val strBefore = keepImgHtml.substring(endPos, keepImgHtml.length)
@ -94,28 +85,13 @@ object HtmlFormatter {
Pattern.compile("<img[^>]*src *= *\"([^\"]+)\"[^>]*>", Pattern.CASE_INSENSITIVE) Pattern.compile("<img[^>]*src *= *\"([^\"]+)\"[^>]*>", Pattern.CASE_INSENSITIVE)
.matcher(strBefore) //格式化普通图片标签 .matcher(strBefore) //格式化普通图片标签
while (matcher0.find()) { while (matcher0.find()) {
val strBefore0 = strBefore.substring(appendPos0, matcher0.start()) str.append(strBefore.substring(appendPos0, matcher0.start()),"<img src=\"${ NetworkUtils.getAbsoluteURL( redirectUrl, matcher0.group(1)!! ) }\">")
str.append(
if (strBefore0.isBlank()) strBefore0 else strBefore0.replace(
"\n",
"\n  "
)
)
str.append(
"<img src=\"${
NetworkUtils.getAbsoluteURL(
redirectUrl,
matcher0.group(1)!!
)
}\">"
)
appendPos0 = matcher0.end() appendPos0 = matcher0.end()
} }
if (appendPos0 < strBefore.length) strBefore.substring( appendPos0, strBefore.length ) else "" strBefore.substring( appendPos0, strBefore.length )
}else keepImgHtml.substring( endPos, keepImgHtml.length ) }else keepImgHtml.substring( endPos, keepImgHtml.length )
).replace("\n","\n  ") ) //缩进图片之后的非空白段落 ) //缩进图片之后的非空白段落
} }
return str.toString() return str.toString()
} }
} }

Loading…
Cancel
Save