|
|
|
@ -1,8 +1,8 @@ |
|
|
|
|
package io.legado.app.utils |
|
|
|
|
|
|
|
|
|
import io.legado.app.constant.AppPattern |
|
|
|
|
import io.legado.app.model.analyzeRule.AnalyzeUrl |
|
|
|
|
import java.net.URL |
|
|
|
|
import io.legado.app.constant.AppPattern |
|
|
|
|
|
|
|
|
|
object HtmlFormatter { |
|
|
|
|
private val wrapHtmlRegex = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex() |
|
|
|
@ -22,22 +22,23 @@ object HtmlFormatter { |
|
|
|
|
|
|
|
|
|
fun formatKeepImg(html: String?, redirectUrl: URL?): String { |
|
|
|
|
html ?: return "" |
|
|
|
|
var formatHtml = formatKeepImg(html) |
|
|
|
|
val sb = StringBuffer() |
|
|
|
|
val matcher = AppPattern.imgPattern.matcher(formatHtml) |
|
|
|
|
val matcher = AppPattern.imgPattern.matcher(html) |
|
|
|
|
var appendPos = 0 |
|
|
|
|
while (matcher.find()) { |
|
|
|
|
val urlArray = matcher.group(1)!!.split(AnalyzeUrl.splitUrlRegex) |
|
|
|
|
var url = NetworkUtils.getAbsoluteURL(redirectUrl, urlArray[0]) |
|
|
|
|
if (urlArray.size > 1) { |
|
|
|
|
url = "$url,${urlArray[1]}" |
|
|
|
|
} |
|
|
|
|
//将Matcher上次匹配结尾到本次匹配结尾这段字符串序列追加到sb中,且是先将其中匹配到的部分替换后再追加 |
|
|
|
|
matcher.appendReplacement(sb, "<img src=\"$url\" >") |
|
|
|
|
sb.append(html.substring(appendPos, matcher.start())) |
|
|
|
|
sb.append("<img src=\"$url\" >") |
|
|
|
|
appendPos = matcher.end() |
|
|
|
|
} |
|
|
|
|
//将Matcher最后那个匹配之后的字串匹配到追加到sb中 |
|
|
|
|
matcher.appendTail(sb) |
|
|
|
|
|
|
|
|
|
return sb.toString() |
|
|
|
|
if (appendPos < html.length) { |
|
|
|
|
sb.append(html.substring(appendPos, html.length)) |
|
|
|
|
} |
|
|
|
|
return formatKeepImg(sb.toString()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|