diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt
index 86cb3aff7..0f9a76f5f 100644
--- a/app/src/main/java/io/legado/app/help/JsExtensions.kt
+++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt
@@ -227,7 +227,7 @@ interface JsExtensions {
}
fun htmlFormat(str: String): String {
- return HtmlFormat.formatKeepImg(str)
+ return HtmlFormatter.formatKeepImg(str)
}
/**
diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt
index a789da1d0..d8e606b0f 100644
--- a/app/src/main/java/io/legado/app/model/Debug.kt
+++ b/app/src/main/java/io/legado/app/model/Debug.kt
@@ -8,7 +8,7 @@ import io.legado.app.data.entities.RssSource
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.rss.Rss
import io.legado.app.model.webBook.WebBook
-import io.legado.app.utils.HtmlFormat
+import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.msg
import kotlinx.coroutines.CoroutineScope
@@ -36,7 +36,7 @@ object Debug {
if (debugSource != sourceUrl || callback == null || !print) return
var printMsg = msg ?: ""
if (isHtml) {
- printMsg = HtmlFormat.format(msg)
+ printMsg = HtmlFormatter.format(msg)
}
if (showTime) {
printMsg =
diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
index b408f1a86..f704c7f24 100644
--- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
+++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
@@ -67,11 +67,11 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions {
return this
}
- fun setRedirectUrl(url: String): AnalyzeRule {
+ fun setRedirectUrl(url: String): URL? {
kotlin.runCatching {
redirectUrl = URL(url.split(AnalyzeUrl.splitUrlRegex, 1)[0])
}
- return this
+ return redirectUrl
}
/**
diff --git a/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt b/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
index 7e5ed9e51..20871ad72 100644
--- a/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
+++ b/app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
@@ -181,7 +181,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
if (book.getDelTag(tag)) {
html = html.replace("\\s?([\\u4e00-\\u9fa5])\\s?.*?".toRegex(), "$1")
}
- return HtmlFormat.formatKeepImg(html)
+ return HtmlFormatter.formatKeepImg(html)
}
return null
}
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 5b451aef6..574a9acdf 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
@@ -10,7 +10,7 @@ import io.legado.app.help.BookHelp
import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
-import io.legado.app.utils.HtmlFormat
+import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
@@ -26,6 +26,7 @@ object BookContent {
bookChapter: BookChapter,
bookSource: BookSource,
baseUrl: String,
+ redirectUrl: String,
nextChapterUrl: String? = null
): String {
body ?: throw Exception(
@@ -44,7 +45,7 @@ object BookContent {
analyzeRule.setRedirectUrl(baseUrl)
analyzeRule.nextChapterUrl = mNextChapterUrl
var contentData = analyzeContent(
- book, baseUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl
+ book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl
)
content.append(contentData.content).append("\n")
@@ -56,20 +57,15 @@ object BookContent {
== NetworkUtils.getAbsoluteURL(baseUrl, mNextChapterUrl)
) break
nextUrlList.add(nextUrl)
- AnalyzeUrl(
+ val res = AnalyzeUrl(
ruleUrl = nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
- ).getStrResponse(bookSource.bookSourceUrl).body?.let { nextBody ->
+ ).getStrResponse(bookSource.bookSourceUrl)
+ res.body?.let { nextBody ->
contentData = analyzeContent(
- book,
- nextUrl,
- nextBody,
- contentRule,
- bookChapter,
- bookSource,
- mNextChapterUrl,
- false
+ book, nextUrl, res.url, nextBody, contentRule,
+ bookChapter, bookSource, mNextChapterUrl, false
)
nextUrl =
if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else ""
@@ -85,20 +81,15 @@ object BookContent {
}
for (item in contentDataList) {
withContext(scope.coroutineContext) {
- AnalyzeUrl(
+ val res = AnalyzeUrl(
ruleUrl = item.nextUrl,
book = book,
headerMapF = bookSource.getHeaderMap()
- ).getStrResponse(bookSource.bookSourceUrl).body?.let {
+ ).getStrResponse(bookSource.bookSourceUrl)
+ res.body?.let { nextBody ->
contentData = analyzeContent(
- book,
- item.nextUrl,
- it,
- contentRule,
- bookChapter,
- bookSource,
- mNextChapterUrl,
- false
+ book, item.nextUrl, res.url, nextBody, contentRule,
+ bookChapter, bookSource, mNextChapterUrl, false
)
item.content = contentData.content
}
@@ -109,7 +100,7 @@ object BookContent {
}
}
content.deleteCharAt(content.length - 1)
- var contentStr = HtmlFormat.formatKeepImg(content.toString())
+ var contentStr = content.toString()
val replaceRegex = contentRule.replaceRegex
if (!replaceRegex.isNullOrEmpty()) {
contentStr = analyzeRule.getString(replaceRegex, value = contentStr)
@@ -128,6 +119,7 @@ object BookContent {
private fun analyzeContent(
book: Book,
baseUrl: String,
+ redirectUrl: String,
body: String,
contentRule: ContentRule,
chapter: BookChapter,
@@ -136,13 +128,14 @@ object BookContent {
printLog: Boolean = true
): ContentData> {
val analyzeRule = AnalyzeRule(book)
- analyzeRule.setContent(body).setBaseUrl(baseUrl)
- analyzeRule.setRedirectUrl(baseUrl)
+ analyzeRule.setContent(body, baseUrl)
+ val rUrl = analyzeRule.setRedirectUrl(redirectUrl)
analyzeRule.nextChapterUrl = nextChapterUrl
val nextUrlList = arrayListOf()
analyzeRule.chapter = chapter
//获取正文
- val content = analyzeRule.getString(contentRule.content)
+ var content = analyzeRule.getString(contentRule.content)
+ content = HtmlFormatter.formatKeepImg(content, rUrl)
//获取下一页链接
val nextUrlRule = contentRule.nextContentUrl
if (!nextUrlRule.isNullOrEmpty()) {
diff --git a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt
index 6c4a0d816..5bd30e2b3 100644
--- a/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt
+++ b/app/src/main/java/io/legado/app/model/webBook/BookInfo.kt
@@ -6,7 +6,7 @@ import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp
import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule
-import io.legado.app.utils.HtmlFormat
+import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.StringUtils.wordCountFormat
import kotlinx.coroutines.CoroutineScope
@@ -93,7 +93,7 @@ object BookInfo {
scope.ensureActive()
Debug.log(bookSource.bookSourceUrl, "┌获取简介")
analyzeRule.getString(infoRule.intro).let {
- if (it.isNotEmpty()) book.intro = HtmlFormat.format(it)
+ if (it.isNotEmpty()) book.intro = HtmlFormatter.format(it)
}
Debug.log(bookSource.bookSourceUrl, "└${book.intro}")
scope.ensureActive()
diff --git a/app/src/main/java/io/legado/app/model/webBook/BookList.kt b/app/src/main/java/io/legado/app/model/webBook/BookList.kt
index 4f54d15c0..c24192ab2 100644
--- a/app/src/main/java/io/legado/app/model/webBook/BookList.kt
+++ b/app/src/main/java/io/legado/app/model/webBook/BookList.kt
@@ -9,7 +9,7 @@ import io.legado.app.help.BookHelp
import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
-import io.legado.app.utils.HtmlFormat
+import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.StringUtils.wordCountFormat
import kotlinx.coroutines.CoroutineScope
@@ -197,7 +197,7 @@ object BookList {
Debug.log(bookSource.bookSourceUrl, "└${searchBook.latestChapterTitle}", log)
scope.ensureActive()
Debug.log(bookSource.bookSourceUrl, "┌获取简介", log)
- searchBook.intro = HtmlFormat.format(analyzeRule.getString(ruleIntro))
+ searchBook.intro = HtmlFormatter.format(analyzeRule.getString(ruleIntro))
Debug.log(bookSource.bookSourceUrl, "└${searchBook.intro}", log)
scope.ensureActive()
Debug.log(bookSource.bookSourceUrl, "┌获取封面链接", log)
diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt
index 8e9e549ed..36ba68e49 100644
--- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt
+++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt
@@ -228,6 +228,7 @@ class WebBook(val bookSource: BookSource) {
bookChapter,
bookSource,
bookChapter.getAbsoluteURL(),
+ bookChapter.getAbsoluteURL(),
nextChapterUrl
)
} else {
@@ -249,6 +250,7 @@ class WebBook(val bookSource: BookSource) {
bookChapter,
bookSource,
bookChapter.getAbsoluteURL(),
+ res.url,
nextChapterUrl
)
}
diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
index e3e5225ef..4712f94b3 100644
--- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
+++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
@@ -90,13 +90,12 @@ object ChapterProvider {
textPages.add(TextPage())
contents.forEachIndexed { index, content ->
if (book.getImageStyle() == Book.imgStyleText) {
- var text = content.replace(srcReplaceChar, "画")
+ var text = content.replace(srcReplaceChar, "▣")
val srcList = LinkedList()
val sb = StringBuffer()
val matcher = AppPattern.imgPattern.matcher(text)
while (matcher.find()) {
- matcher.group(1)?.let { it ->
- val src = NetworkUtils.getAbsoluteURL(bookChapter.getAbsoluteURL(), it)
+ matcher.group(1)?.let { src ->
srcList.add(src)
ImageProvider.getImage(book, bookChapter.index, src)
matcher.appendReplacement(sb, srcReplaceChar)
@@ -118,12 +117,8 @@ object ChapterProvider {
if (text.isNotBlank()) {
val matcher = AppPattern.imgPattern.matcher(text)
if (matcher.find()) {
- matcher.group(1)?.let { it ->
+ matcher.group(1)?.let { src ->
if (!book.isEpub()) {
- val src = NetworkUtils.getAbsoluteURL(
- bookChapter.getAbsoluteURL(),
- it
- )
durY = setTypeImage(
book, bookChapter, src,
durY, textPages, book.getImageStyle()
diff --git a/app/src/main/java/io/legado/app/utils/HtmlFormat.kt b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt
similarity index 52%
rename from app/src/main/java/io/legado/app/utils/HtmlFormat.kt
rename to app/src/main/java/io/legado/app/utils/HtmlFormatter.kt
index 30a3b2d30..d4c32b68c 100644
--- a/app/src/main/java/io/legado/app/utils/HtmlFormat.kt
+++ b/app/src/main/java/io/legado/app/utils/HtmlFormatter.kt
@@ -1,9 +1,13 @@
package io.legado.app.utils
-object HtmlFormat {
+import java.net.URL
+import java.util.regex.Pattern
+
+object HtmlFormatter {
private val wrapHtmlRegex = "?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex()
private val notImgHtmlRegex = "?(?!img)[a-zA-Z]+(?=[ >])[^<>]*>".toRegex()
private val otherHtmlRegex = "?[a-zA-Z]+(?=[ >])[^<>]*>".toRegex()
+ private val imgPattern = Pattern.compile("(]*src=\"([^\"]+)\"[^>]*(>)")
fun format(html: String?): String {
html ?: return ""
@@ -23,5 +27,20 @@ object HtmlFormat {
.replace("[\\n\\s]+$".toRegex(), "")
}
+ fun formatKeepImg(html: String?, redirectUrl: URL?): String {
+ html ?: return ""
+ val sb = StringBuffer()
+ val matcher = imgPattern.matcher(html)
+ while (matcher.find()) {
+ val url = NetworkUtils.getAbsoluteURL(redirectUrl, matcher.group(2)!!)
+ matcher.appendReplacement(sb, "${matcher.group(1)} src=\"$url\"${matcher.group(3)}")
+ }
+ matcher.appendTail(sb)
+ return sb.replace(wrapHtmlRegex, "\n")
+ .replace(notImgHtmlRegex, "")
+ .replace("\\s*\\n+\\s*".toRegex(), "\n ")
+ .replace("^[\\n\\s]+".toRegex(), " ")
+ .replace("[\\n\\s]+$".toRegex(), "")
+ }
}
\ No newline at end of file