|
|
@ -1,5 +1,6 @@ |
|
|
|
package io.legado.app.utils |
|
|
|
package io.legado.app.utils |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.legado.app.model.analyzeRule.AnalyzeUrl |
|
|
|
import java.net.URL |
|
|
|
import java.net.URL |
|
|
|
import java.util.regex.Pattern |
|
|
|
import java.util.regex.Pattern |
|
|
|
|
|
|
|
|
|
|
@ -26,35 +27,68 @@ object HtmlFormatter { |
|
|
|
.replace("[\\n\\s]+\$|^[\\n\\s]+".toRegex(), "") |
|
|
|
.replace("[\\n\\s]+\$|^[\\n\\s]+".toRegex(), "") |
|
|
|
.replace("\\s*\\n+\\s*".toRegex(), "\n") |
|
|
|
.replace("\\s*\\n+\\s*".toRegex(), "\n") |
|
|
|
|
|
|
|
|
|
|
|
val sb = StringBuffer() |
|
|
|
var str = "" |
|
|
|
var endPos = 0 |
|
|
|
var endPos = 0 |
|
|
|
for(pattern in listOf("<img[^>]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>","<img[^>]*data-[^=]*= *\"([^\"])\"[^>]*>")){ |
|
|
|
var hasMatch = true //普通图片标签是否还未处理过 |
|
|
|
|
|
|
|
var hasMatchX = false //是否存在带参数或带数据属性的图片标签 |
|
|
|
|
|
|
|
var pos = -1 |
|
|
|
|
|
|
|
val list = listOf("<img[^>]*src *= *\"([^\"{]+\\{(?:[^{}]|\\{[^}]+})+})\"[^>]*>","<img[^>]*data-[^=]*= *\"([^\"]+)\"[^>]*>","<img[^>]*src *= *\"([^\"]+)\"[^>]*>") //优先匹配用户处理过所以带参数的图片标签,其次匹配带数据属性的图片标签 |
|
|
|
|
|
|
|
while(++pos<3){ |
|
|
|
|
|
|
|
if(pos == 2) { |
|
|
|
|
|
|
|
if(hasMatchX)break //普通图片标签只在不存在存在带参数或带数据属性的图片标签的时候匹配 |
|
|
|
|
|
|
|
else hasMatch = false //匹配带参数或带数据属性的图片标签同时也会格式化普通图片标签 |
|
|
|
|
|
|
|
} |
|
|
|
var appendPos = 0 |
|
|
|
var appendPos = 0 |
|
|
|
val matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(keepImgHtml) |
|
|
|
val matcher = |
|
|
|
while (matcher.find()) { |
|
|
|
Pattern.compile(list[pos], Pattern.CASE_INSENSITIVE).matcher(keepImgHtml) |
|
|
|
val url = matcher.group(1)!! |
|
|
|
val sb = StringBuffer() |
|
|
|
val urlBefore = url.substringBefore(',') |
|
|
|
if (matcher.find()) { |
|
|
|
val strBefore = keepImgHtml.substring(appendPos, matcher.start()) |
|
|
|
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()) { |
|
|
|
|
|
|
|
val strBefore0 = strBefore.substring(appendPos, matcher.start()) |
|
|
|
|
|
|
|
sb.append( if (strBefore0.isBlank()) strBefore0 else strBefore0.replace("\n","\n ")) |
|
|
|
|
|
|
|
sb.append("<img src=\"${NetworkUtils.getAbsoluteURL(redirectUrl,matcher.group(1)!!)}\">") |
|
|
|
|
|
|
|
appendPos0 = matcher.end() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
strBefore = if (appendPos0 < strBefore.length) { |
|
|
|
|
|
|
|
strBefore.substring(appendPos0, strBefore.length).replace("\n", "\n ") //缩进图片之后的非空白段落 |
|
|
|
|
|
|
|
} else "" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sb.append( |
|
|
|
sb.append( |
|
|
|
if (strBefore.isBlank()) strBefore else strBefore.replace("\n","\n ") //缩进图片之间的非空白段落 |
|
|
|
if (strBefore.isBlank()) strBefore else strBefore.replace("\n","\n ") //缩进图片之间的非空白段落 |
|
|
|
) |
|
|
|
) |
|
|
|
sb.append( |
|
|
|
|
|
|
|
"<img src=\"${ |
|
|
|
if (pos == 0) { |
|
|
|
NetworkUtils.getAbsoluteURL( |
|
|
|
val url = matcher.group(1)!! |
|
|
|
redirectUrl, |
|
|
|
val urlMatcher = AnalyzeUrl.paramPattern.matcher(url) |
|
|
|
urlBefore |
|
|
|
val find = urlMatcher.find() |
|
|
|
) + url.substring(urlBefore.length) |
|
|
|
sb.append("<img src=\"${ |
|
|
|
}\">" |
|
|
|
if(find) NetworkUtils.getAbsoluteURL(redirectUrl,url.substring(0,urlMatcher.start())) + url.substring(urlMatcher.end()) |
|
|
|
) |
|
|
|
else url |
|
|
|
|
|
|
|
}\">") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else sb.append("<img src=\"${NetworkUtils.getAbsoluteURL(redirectUrl,matcher.group(1)!!)}\">") |
|
|
|
|
|
|
|
|
|
|
|
appendPos = matcher.end() |
|
|
|
appendPos = matcher.end() |
|
|
|
} |
|
|
|
} while (matcher.find()) |
|
|
|
if(appendPos != 0)endPos = appendPos |
|
|
|
hasMatch = false //普通图片标签已经处理过 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (endPos < keepImgHtml.length) { |
|
|
|
if (appendPos != 0) { |
|
|
|
sb.append( keepImgHtml.substring( endPos, keepImgHtml.length ).replace("\n","\n ") ) //缩进图片之后的非空白段落 |
|
|
|
hasMatchX = true //存在带参数或带数据属性的图片标签 |
|
|
|
|
|
|
|
endPos = appendPos //存在匹配,更新位置 |
|
|
|
|
|
|
|
str = sb.toString() //存在匹配,更新字符串 |
|
|
|
} |
|
|
|
} |
|
|
|
return sb.toString() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return if (endPos < keepImgHtml.length) { |
|
|
|
|
|
|
|
str + keepImgHtml.substring( endPos, keepImgHtml.length ).replace("\n","\n ") //缩进图片之后的非空白段落 |
|
|
|
|
|
|
|
}else str |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|