pull/1138/head
gedoor 3 years ago
parent 3306702105
commit d5e49b313b
  1. 38
      app/src/main/java/io/legado/app/utils/HtmlFormatter.kt

@ -8,6 +8,10 @@ object HtmlFormatter {
private val wrapHtmlRegex = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex() private val wrapHtmlRegex = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex()
private val notImgHtmlRegex = "</?(?!img)[a-zA-Z]+(?=[ >])[^<>]*>".toRegex() private val notImgHtmlRegex = "</?(?!img)[a-zA-Z]+(?=[ >])[^<>]*>".toRegex()
private val otherHtmlRegex = "</?[a-zA-Z]+(?=[ >])[^<>]*>".toRegex() private val otherHtmlRegex = "</?[a-zA-Z]+(?=[ >])[^<>]*>".toRegex()
private val formatImagePattern = Pattern.compile(
"<img[^>]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|<img[^>]*data-[^=]*= *\"([^\"]*)\"[^>]*>|<img[^>]*src *= *\"([^\"]*)\"[^>]*>",
Pattern.CASE_INSENSITIVE
)
fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String { fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String {
html ?: return "" html ?: return ""
@ -27,24 +31,32 @@ object HtmlFormatter {
.replace("\\n\\s*$|^\\s*\\n".toRegex(), "") .replace("\\n\\s*$|^\\s*\\n".toRegex(), "")
.replace("\\n\\s*\\n".toRegex(), "\n") .replace("\\n\\s*\\n".toRegex(), "\n")
//正则的“|”处于顶端而不处于()中时,具有类似||的熔断效果,故以此机制简化原来的代码 //正则的“|”处于顶端而不处于()中时,具有类似||的熔断效果,故以此机制简化原来的代码
val matcher = Pattern.compile("<img[^>]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|<img[^>]*data-[^=]*= *\"([^\"]*)\"[^>]*>|<img[^>]*src *= *\"([^\"]*)\"[^>]*>", Pattern.CASE_INSENSITIVE).matcher(keepImgHtml) val matcher = formatImagePattern.matcher(keepImgHtml)
var appendPos = 0 var appendPos = 0
val sb = StringBuffer() val sb = StringBuffer()
while (matcher.find()){ while (matcher.find()) {
var param = "" var param = ""
sb.append( keepImgHtml.substring(appendPos, matcher.start()),"<img src=\"${ sb.append(
(NetworkUtils.getAbsoluteURL(redirectUrl, keepImgHtml.substring(appendPos, matcher.start()), "<img src=\"${
matcher.group(1)?.let { (NetworkUtils.getAbsoluteURL(redirectUrl,
val urlMatcher = AnalyzeUrl.paramPattern.matcher(it) matcher.group(1)?.let {
if(urlMatcher.find()) { val urlMatcher = AnalyzeUrl.paramPattern.matcher(it)
param = ',' + it.substring(urlMatcher.end()) if (urlMatcher.find()) {
it.substring(0,urlMatcher.start()) param = ',' + it.substring(urlMatcher.end())
} else it it.substring(0, urlMatcher.start())
}?: matcher.group(2) ?: matcher.group(3)!!)) + param } else it
}\">") } ?: matcher.group(2) ?: matcher.group(3)!!
)) + param
}\">"
)
appendPos = matcher.end() 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() return sb.toString()
} }
} }

Loading…
Cancel
Save