From f7a4a94914adb8a3a294819f2baecc21d7a959d7 Mon Sep 17 00:00:00 2001 From: bushixuanqi <2208877960@qq.com> Date: Sun, 18 Jul 2021 13:20:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/model/webBook/BookContent.kt | 7 ++- .../java/io/legado/app/utils/HtmlFormatter.kt | 51 ++++++++----------- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index e6984a548..618d06ad5 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.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()) { diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt index 4dfb2c35f..a15cbedcf 100644 --- a/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt +++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt @@ -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("]*data-".toRegex())) { - hasDataType = true - "]*data-[^=]*= *\"([^\"])\"[^>]*>" - } - else { - hasDataType = false - "]*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("]*data-[^=]*= *\"([^\"])\"[^>]*>","]*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( "" - ) - 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() }