Update HtmlFormatter.kt

去掉多余的括号
pull/1140/head
bushixuanqi 4 years ago committed by GitHub
parent bfb9afbe6c
commit 6a961052a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      app/src/main/java/io/legado/app/utils/HtmlFormatter.kt

@ -9,17 +9,17 @@ object HtmlFormatter {
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( private val formatImagePattern = Pattern.compile(
"<img[^>]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|<img[^>]*data-[^=]*= *\"([^\"]*)\"[^>]*>|<img[^>]*src *= *\"([^\"]*)\"[^>]*>", "<img[^>]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|<img[^>]*data-[^=]*= *\"([^\"]*)\"[^>]*>|<img[^>]*src *= *\"([^\"]*)\"[^>]*>",
Pattern.CASE_INSENSITIVE Pattern.CASE_INSENSITIVE
) )
fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String { fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String {
html ?: return "" html ?: return ""
return html.replace(wrapHtmlRegex, "\n") return html.replace(wrapHtmlRegex, "\n")
.replace(otherRegex, "") .replace(otherRegex, "")
.replace("\\s*\\n+\\s*".toRegex(), "\n  ") .replace("\\s*\\n+\\s*".toRegex(), "\n  ")
.replace("^[\\n\\s]+".toRegex(), "  ") .replace("^[\\n\\s]+".toRegex(), "  ")
.replace("[\\n\\s]+$".toRegex(), "") .replace("[\\n\\s]+$".toRegex(), "")
} }
fun formatKeepImg(html: String?) = format(html, notImgHtmlRegex) fun formatKeepImg(html: String?) = format(html, notImgHtmlRegex)
@ -27,9 +27,9 @@ object HtmlFormatter {
fun formatKeepImg(html: String?, redirectUrl: URL?): String { fun formatKeepImg(html: String?, redirectUrl: URL?): String {
html ?: return "" html ?: return ""
val keepImgHtml = html.replace(wrapHtmlRegex, "\n") val keepImgHtml = html.replace(wrapHtmlRegex, "\n")
.replace(notImgHtmlRegex, "") .replace(notImgHtmlRegex, "")
.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 = formatImagePattern.matcher(keepImgHtml) val matcher = formatImagePattern.matcher(keepImgHtml)
var appendPos = 0 var appendPos = 0
@ -37,8 +37,8 @@ object HtmlFormatter {
while (matcher.find()) { while (matcher.find()) {
var param = "" var param = ""
sb.append( sb.append(
keepImgHtml.substring(appendPos, matcher.start()), "<img src=\"${ keepImgHtml.substring(appendPos, matcher.start()), "<img src=\"${
(NetworkUtils.getAbsoluteURL(redirectUrl, NetworkUtils.getAbsoluteURL(redirectUrl,
matcher.group(1)?.let { matcher.group(1)?.let {
val urlMatcher = AnalyzeUrl.paramPattern.matcher(it) val urlMatcher = AnalyzeUrl.paramPattern.matcher(it)
if (urlMatcher.find()) { if (urlMatcher.find()) {
@ -46,16 +46,16 @@ object HtmlFormatter {
it.substring(0, urlMatcher.start()) it.substring(0, urlMatcher.start())
} else it } else it
} ?: matcher.group(2) ?: matcher.group(3)!! } ?: matcher.group(2) ?: matcher.group(3)!!
)) + param ) + param
}\">" }\">"
) )
appendPos = matcher.end() appendPos = matcher.end()
} }
if (appendPos < keepImgHtml.length) sb.append( if (appendPos < keepImgHtml.length) sb.append(
keepImgHtml.substring( keepImgHtml.substring(
appendPos, appendPos,
keepImgHtml.length keepImgHtml.length
) )
) )
return sb.toString() return sb.toString()
} }

Loading…
Cancel
Save