|
|
@ -26,73 +26,25 @@ object HtmlFormatter { |
|
|
|
.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") |
|
|
|
|
|
|
|
//正则的“|”处于顶端而不处于()中时,具有类似||的熔断效果,故以此机制简化原来的代码 |
|
|
|
var str = StringBuffer() |
|
|
|
val matcher = Pattern.compile("<img[^>]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>|<img[^>]*data-[^=]*= *\"([^\"]*)\"[^>]*>|<img[^>]*src *= *\"([^\"]*)\"[^>]*>", Pattern.CASE_INSENSITIVE).matcher(keepImgHtml) |
|
|
|
var endPos = 0 |
|
|
|
var appendPos = 0 |
|
|
|
var hasMatch = true //普通图片标签是否还未处理过 |
|
|
|
val sb = StringBuffer() |
|
|
|
var hasMatchX = false //是否存在带参数或带数据属性的图片标签 |
|
|
|
while (matcher.find()){ |
|
|
|
var pos = -1 |
|
|
|
var param = "" |
|
|
|
val list = listOf("<img[^>]*src *= *\"([^\"{]*\\{(?:[^{}]|\\{[^}]+\\})+\\})\"[^>]*>","<img[^>]*data-[^=]*= *\"([^\"]*)\"[^>]*>","<img[^>]*src *= *\"([^\"]*)\"[^>]*>") //优先匹配用户处理过所以带参数的图片标签,其次匹配带数据属性的图片标签 |
|
|
|
sb.append( keepImgHtml.substring(appendPos, matcher.start()),"<img src=\"${ |
|
|
|
while(++pos<3){ |
|
|
|
(NetworkUtils.getAbsoluteURL(redirectUrl, |
|
|
|
if(pos == 2) { |
|
|
|
matcher.group(1)?.let { |
|
|
|
if(hasMatchX)break //普通图片标签只在不存在存在带参数或带数据属性的图片标签的时候匹配 |
|
|
|
val urlMatcher = AnalyzeUrl.paramPattern.matcher(it) |
|
|
|
else hasMatch = false //匹配带参数或带数据属性的图片标签同时也会格式化普通图片标签 |
|
|
|
if(urlMatcher.find()) { |
|
|
|
} |
|
|
|
param = ',' + it.substring(urlMatcher.end()) |
|
|
|
var appendPos = 0 |
|
|
|
it.substring(0,urlMatcher.start()) |
|
|
|
val matcher = |
|
|
|
} else it |
|
|
|
Pattern.compile(list[pos], Pattern.CASE_INSENSITIVE).matcher(keepImgHtml) |
|
|
|
}?: matcher.group(2) ?: matcher.group(3)!!)) + param |
|
|
|
val sb = StringBuffer() |
|
|
|
}\">") |
|
|
|
if (matcher.find()) { |
|
|
|
appendPos = matcher.end() |
|
|
|
do { |
|
|
|
|
|
|
|
var strBefore = keepImgHtml.substring(appendPos, matcher.start()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (hasMatch) { //格式化不带参数和数据属性的普通图片标签 |
|
|
|
|
|
|
|
var appendPos0 = 0 |
|
|
|
|
|
|
|
val matcher0 = Pattern.compile("<img[^>]*src *= *\"([^\"]*)\"[^>]*>", Pattern.CASE_INSENSITIVE).matcher(strBefore) //格式化普通图片标签 |
|
|
|
|
|
|
|
while (matcher0.find()) { |
|
|
|
|
|
|
|
sb.append(strBefore.substring(appendPos0, matcher0.start()),"<img src=\"${NetworkUtils.getAbsoluteURL(redirectUrl,matcher0.group(1)!!)}\">") |
|
|
|
|
|
|
|
appendPos0 = matcher0.end() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
strBefore = strBefore.substring(appendPos0, strBefore.length) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sb.append( strBefore,"<img src=\"${ |
|
|
|
|
|
|
|
if (pos == 0) { |
|
|
|
|
|
|
|
val url = matcher.group(1)!! |
|
|
|
|
|
|
|
val urlMatcher = AnalyzeUrl.paramPattern.matcher(url) |
|
|
|
|
|
|
|
if(urlMatcher.find()) NetworkUtils.getAbsoluteURL(redirectUrl,url.substring(0,urlMatcher.start())) + ',' + url.substring(urlMatcher.end()) |
|
|
|
|
|
|
|
else NetworkUtils.getAbsoluteURL(redirectUrl,url) |
|
|
|
|
|
|
|
}else NetworkUtils.getAbsoluteURL(redirectUrl,matcher.group(1)!!) |
|
|
|
|
|
|
|
}\">") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
appendPos = matcher.end() |
|
|
|
|
|
|
|
} while (matcher.find()) |
|
|
|
|
|
|
|
hasMatch = false //普通图片标签已经处理过 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (appendPos != 0) { |
|
|
|
|
|
|
|
hasMatchX = true //存在带参数或带数据属性的图片标签 |
|
|
|
|
|
|
|
endPos = appendPos //存在匹配,更新位置 |
|
|
|
|
|
|
|
str = sb //存在匹配,更新字符串 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (endPos < keepImgHtml.length) { |
|
|
|
|
|
|
|
str.append( |
|
|
|
|
|
|
|
if(hasMatchX){ //处理末尾的普通图片标签 |
|
|
|
|
|
|
|
var appendPos0 = 0 |
|
|
|
|
|
|
|
val strBefore = keepImgHtml.substring(endPos, keepImgHtml.length) |
|
|
|
|
|
|
|
val matcher0 = |
|
|
|
|
|
|
|
Pattern.compile("<img[^>]*src *= *\"([^\"]*)\"[^>]*>", Pattern.CASE_INSENSITIVE) |
|
|
|
|
|
|
|
.matcher(strBefore) //格式化普通图片标签 |
|
|
|
|
|
|
|
while (matcher0.find()) { |
|
|
|
|
|
|
|
str.append(strBefore.substring(appendPos0, matcher0.start()),"<img src=\"${ NetworkUtils.getAbsoluteURL( redirectUrl, matcher0.group(1)!! ) }\">") |
|
|
|
|
|
|
|
appendPos0 = matcher0.end() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
strBefore.substring( appendPos0, strBefore.length ) |
|
|
|
|
|
|
|
}else keepImgHtml.substring( endPos, keepImgHtml.length ) |
|
|
|
|
|
|
|
) //缩进图片之后的非空白段落 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return str.toString() |
|
|
|
if (appendPos < keepImgHtml.length) sb.append(keepImgHtml.substring( appendPos, keepImgHtml.length )) |
|
|
|
|
|
|
|
return sb.toString() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|