优化图片格式化

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. 51
      app/src/main/java/io/legado/app/utils/HtmlFormatter.kt

@ -52,7 +52,7 @@ object BookContent {
var contentData = analyzeContent(
book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl
)
content.append(contentData.content).append("\n")
content.append(contentData.content)
if (contentData.nextUrl.size == 1) {
var nextUrl = contentData.nextUrl[0]
while (nextUrl.isNotEmpty() && !nextUrlList.contains(nextUrl)) {
@ -74,7 +74,7 @@ object BookContent {
)
nextUrl =
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}")
@ -98,11 +98,10 @@ object BookContent {
}
asyncArray.forEach { coroutine ->
scope.ensureActive()
content.append(coroutine.await()).append("\n")
content.append("\n").append(coroutine.await())
}
}
}
content.deleteCharAt(content.length - 1)
var contentStr = content.toString()
val replaceRegex = contentRule.replaceRegex
if (!replaceRegex.isNullOrEmpty()) {

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

Loading…
Cancel
Save