优化图片格式化

pull/1126/head
bushixuanqi 3 years ago
parent b86e17bc06
commit f7a4a94914
  1. 7
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  2. 33
      app/src/main/java/io/legado/app/utils/HtmlFormatter.kt

@ -52,7 +52,7 @@ object BookContent {
var contentData = analyzeContent( var contentData = analyzeContent(
book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl
) )
content.append(contentData.content).append("\n") content.append(contentData.content)
if (contentData.nextUrl.size == 1) { if (contentData.nextUrl.size == 1) {
var nextUrl = contentData.nextUrl[0] var nextUrl = contentData.nextUrl[0]
while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) { while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) {
@ -74,7 +74,7 @@ object BookContent {
) )
nextUrl = nextUrl =
if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else "" if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else ""
content.append(contentData.content).append("\n") content.append("\n").append(contentData.content)
} }
} }
Debug.log(bookSource.bookSourceUrl, "◇本章总页数:${nextUrlList.size}") Debug.log(bookSource.bookSourceUrl, "◇本章总页数:${nextUrlList.size}")
@ -98,11 +98,10 @@ object BookContent {
} }
asyncArray.forEach { coroutine -> asyncArray.forEach { coroutine ->
scope.ensureActive() scope.ensureActive()
content.append(coroutine.await()).append("\n") content.append("\n").append(coroutine.await())
} }
} }
} }
content.deleteCharAt(content.length - 1)
var contentStr = content.toString() var contentStr = content.toString()
val replaceRegex = contentRule.replaceRegex val replaceRegex = contentRule.replaceRegex
if (!replaceRegex.isNullOrEmpty()) { if (!replaceRegex.isNullOrEmpty()) {

@ -27,42 +27,31 @@ object HtmlFormatter {
.replace("\\s*\\n+\\s*".toRegex(), "\n") .replace("\\s*\\n+\\s*".toRegex(), "\n")
val sb = StringBuffer() val sb = StringBuffer()
val hasDataType:Boolean //是否有数据属性 var endPos = 0
for(pattern in listOf("<img[^>]*data-[^=]*= *\"([^\"])\"[^>]*>","<img[^>]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>")){
//图片有data-开头的数据属性时优先用数据属性作为src,没有数据属性时匹配src
val imgPattern = Pattern.compile(
if(keepImgHtml.matches("<img[^>]*data-".toRegex())) {
hasDataType = true
"<img[^>]*data-[^=]*= *\"([^\"])\"[^>]*>"
}
else {
hasDataType = false
"<img[^>]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>"
}, Pattern.CASE_INSENSITIVE
)
val matcher = imgPattern.matcher(keepImgHtml)
var appendPos = 0 var appendPos = 0
val matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(keepImgHtml)
while (matcher.find()) { while (matcher.find()) {
val url = matcher.group(1)!! val url = matcher.group(1)!!
val urlBefore = url.substringBefore(',') val urlBefore = url.substringBefore(',')
sb.append( keepImgHtml.substring(appendPos, matcher.start()).replace("\n","\n  ") ) //缩进换行下个非图片段落 sb.append(
keepImgHtml.substring(appendPos, matcher.start()).replace("\n", "\n  ")
) //缩进换行下个非图片段落
sb.append( sb.append(
"<img src=\"${ "<img src=\"${
NetworkUtils.getAbsoluteURL( NetworkUtils.getAbsoluteURL(
redirectUrl, redirectUrl,
urlBefore urlBefore
) ) + url.substring(urlBefore.length)
}${
url.substring(urlBefore.length)
}\">" }\">"
) )
appendPos = matcher.end() appendPos = matcher.end()
} }
if(appendPos != 0)endPos = appendPos
}
if (appendPos < keepImgHtml.length) { if (endPos < keepImgHtml.length) {
sb.append( keepImgHtml.substring( appendPos, keepImgHtml.length ).replace("\n","\n  ") ) //缩进换行下个非图片段落 sb.append( keepImgHtml.substring( endPos, keepImgHtml.length ).replace("\n","\n  ") ) //缩进换行下个非图片段落
} }
return sb.toString() return sb.toString()
} }

Loading…
Cancel
Save