pull/898/head
gedoor 4 years ago
parent 26b0948b89
commit f689ea5c11
  1. 2
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  2. 4
      app/src/main/java/io/legado/app/model/Debug.kt
  3. 2
      app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
  4. 4
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  5. 4
      app/src/main/java/io/legado/app/model/webBook/BookInfo.kt
  6. 4
      app/src/main/java/io/legado/app/model/webBook/BookList.kt
  7. 27
      app/src/main/java/io/legado/app/utils/HtmlFormat.kt
  8. 13
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -227,7 +227,7 @@ interface JsExtensions {
}
fun htmlFormat(str: String): String {
return str.htmlFormat()
return HtmlFormat.formatKeepImg(str)
}
/**

@ -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.HtmlFormat
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 = printMsg.htmlFormat()
printMsg = HtmlFormat.format(msg)
}
if (showTime) {
printMsg =

@ -181,7 +181,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
if (book.getDelTag(tag)) {
html = html.replace("<ruby>\\s?([\\u4e00-\\u9fa5])\\s?.*?</ruby>".toRegex(), "$1")
}
return html.htmlFormat()
return HtmlFormat.formatKeepImg(html)
}
return null
}

@ -10,8 +10,8 @@ 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.NetworkUtils
import io.legado.app.utils.htmlFormat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
import splitties.init.appCtx
@ -109,7 +109,7 @@ object BookContent {
}
}
content.deleteCharAt(content.length - 1)
var contentStr = content.toString().htmlFormat()
var contentStr = HtmlFormat.formatKeepImg(content.toString())
val replaceRegex = contentRule.replaceRegex
if (!replaceRegex.isNullOrEmpty()) {
contentStr = analyzeRule.getString(replaceRegex, value = contentStr)

@ -6,9 +6,9 @@ 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.NetworkUtils
import io.legado.app.utils.StringUtils.wordCountFormat
import io.legado.app.utils.htmlFormat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ensureActive
import splitties.init.appCtx
@ -93,7 +93,7 @@ object BookInfo {
scope.ensureActive()
Debug.log(bookSource.bookSourceUrl, "┌获取简介")
analyzeRule.getString(infoRule.intro).let {
if (it.isNotEmpty()) book.intro = it.htmlFormat()
if (it.isNotEmpty()) book.intro = HtmlFormat.format(it)
}
Debug.log(bookSource.bookSourceUrl, "${book.intro}")
scope.ensureActive()

@ -9,9 +9,9 @@ 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.NetworkUtils
import io.legado.app.utils.StringUtils.wordCountFormat
import io.legado.app.utils.htmlFormat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ensureActive
import splitties.init.appCtx
@ -197,7 +197,7 @@ object BookList {
Debug.log(bookSource.bookSourceUrl, "${searchBook.latestChapterTitle}", log)
scope.ensureActive()
Debug.log(bookSource.bookSourceUrl, "┌获取简介", log)
searchBook.intro = analyzeRule.getString(ruleIntro).htmlFormat()
searchBook.intro = HtmlFormat.format(analyzeRule.getString(ruleIntro))
Debug.log(bookSource.bookSourceUrl, "${searchBook.intro}", log)
scope.ensureActive()
Debug.log(bookSource.bookSourceUrl, "┌获取封面链接", log)

@ -0,0 +1,27 @@
package io.legado.app.utils
object HtmlFormat {
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()
fun format(html: String?): String {
html ?: return ""
return html.replace(wrapHtmlRegex, "\n")
.replace(otherHtmlRegex, "")
.replace("\\s*\\n+\\s*".toRegex(), "\n  ")
.replace("^[\\n\\s]+".toRegex(), "  ")
.replace("[\\n\\s]+$".toRegex(), "")
}
fun formatKeepImg(html: String?): String {
html ?: return ""
return html.replace(wrapHtmlRegex, "\n")
.replace(notImgHtmlRegex, "")
.replace("\\s*\\n+\\s*".toRegex(), "\n  ")
.replace("^[\\n\\s]+".toRegex(), "  ")
.replace("[\\n\\s]+$".toRegex(), "")
}
}

@ -8,9 +8,6 @@ import android.net.Uri
import java.io.File
import java.util.*
val removeHtmlRegex = "</?(?:div|p|br|hr|h\\d|article|dd|dl)[^>]*>".toRegex()
val notImgHtmlRegex = "</?(?!img)[a-zA-Z]+(?=[ >])[^<>]*>".toRegex()
fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim()
fun String?.isContentScheme(): Boolean = this?.startsWith("content://") == true
@ -50,16 +47,6 @@ fun String?.isJsonArray(): Boolean =
str.startsWith("[") && str.endsWith("]")
} ?: false
fun String?.htmlFormat(): String {
this ?: return ""
return this
.replace(removeHtmlRegex, "\n")
.replace(notImgHtmlRegex, "")
.replace("\\s*\\n+\\s*".toRegex(), "\n  ")
.replace("^[\\n\\s]+".toRegex(), "  ")
.replace("[\\n\\s]+$".toRegex(), "")
}
fun String.splitNotBlank(vararg delimiter: String): Array<String> = run {
this.split(*delimiter).map { it.trim() }.filterNot { it.isBlank() }.toTypedArray()
}

Loading…
Cancel
Save