Merge pull request #1126 from bushixuanqi/master

优化图片格式化
pull/1128/head^2
kunfei 3 years ago committed by GitHub
commit a7b2ae3605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  2. 66
      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()) {
@ -150,4 +149,4 @@ object BookContent {
} }
return ContentData(content, nextUrlList) return ContentData(content, nextUrlList)
} }
} }

@ -11,10 +11,10 @@ object HtmlFormatter {
fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String { fun format(html: String?, otherRegex: Regex = otherHtmlRegex): String {
html ?: return "" html ?: return ""
return html.replace(wrapHtmlRegex, "\n") return html.replace(wrapHtmlRegex, "\n")
.replace(otherRegex, "") .replace(otherRegex, "")
.replace("\\s*\\n+\\s*".toRegex(), "\n  ") .replace("\\s*\\n+\\s*".toRegex(), "\n  ")
.replace("^[\\n\\s]+".toRegex(), "  ") .replace("^[\\n\\s]+".toRegex(), "  ")
.replace("[\\n\\s]+$".toRegex(), "") .replace("[\\n\\s]+$".toRegex(), "")
} }
fun formatKeepImg(html: String?) = format(html, notImgHtmlRegex) fun formatKeepImg(html: String?) = format(html, notImgHtmlRegex)
@ -22,47 +22,37 @@ object HtmlFormatter {
fun formatKeepImg(html: String?, redirectUrl: URL?): String { fun formatKeepImg(html: String?, redirectUrl: URL?): String {
html ?: return "" html ?: return ""
val keepImgHtml = html.replace(wrapHtmlRegex, "\n") val keepImgHtml = html.replace(wrapHtmlRegex, "\n")
.replace(notImgHtmlRegex, "") .replace(notImgHtmlRegex, "")
.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() val sb = StringBuffer()
val hasDataType:Boolean //是否有数据属性 var endPos = 0
for(pattern in listOf("<img[^>]*data-[^=]*= *\"([^\"])\"[^>]*>","<img[^>]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>")){
//图片有data-开头的数据属性时优先用数据属性作为src,没有数据属性时匹配src var appendPos = 0
val imgPattern = Pattern.compile( val matcher = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE).matcher(keepImgHtml)
if(keepImgHtml.matches("<img[^>]*data-".toRegex())) { while (matcher.find()) {
hasDataType = true val url = matcher.group(1)!!
"<img[^>]*data-[^=]*= *\"([^\"])\"[^>]*>" val urlBefore = url.substringBefore(',')
} val strBefore = keepImgHtml.substring(appendPos, matcher.start())
else { sb.append(
hasDataType = false if(strBefore.isBlank()) strBefore else strBefore.replace("\n", "\n  ") //缩进图片之间的非空白段落
"<img[^>]*src *= *\"([^\"{]+(?:\\{(?:[^{}]|\\{[^{}]*\\})*\\})?)\"[^>]*>" )
}, Pattern.CASE_INSENSITIVE sb.append(
)
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(
"<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