html规则优化

pull/274/head
gedoor 4 years ago
parent adfc8c9198
commit a41a8d503a
  1. 11
      app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
  2. 2
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -6,10 +6,7 @@ import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import io.legado.app.App import io.legado.app.App
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.utils.FileUtils import io.legado.app.utils.*
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.externalFilesDir
import io.legado.app.utils.isContentPath
import nl.siegmann.epublib.domain.Book import nl.siegmann.epublib.domain.Book
import nl.siegmann.epublib.domain.TOCReference import nl.siegmann.epublib.domain.TOCReference
import nl.siegmann.epublib.epub.EpubReader import nl.siegmann.epublib.epub.EpubReader
@ -95,11 +92,7 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) {
val elements = doc.body().children() val elements = doc.body().children()
elements.select("script").remove() elements.select("script").remove()
elements.select("style").remove() elements.select("style").remove()
return elements.outerHtml() return elements.outerHtml().htmlFormat()
.replace("</?(?:div|p|b|br|hr|h\\d|article|dd|dl|span)[^>]*>".toRegex(), "\n")
.replace("\\s*\\n+\\s*".toRegex(), "\n  ")
.replace("^[\\n\\s]+".toRegex(), "  ")
.replace("[\\n\\s]+$".toRegex(), "")
} }
return null return null
} }

@ -33,7 +33,7 @@ fun String?.isJsonArray(): Boolean =
} ?: false } ?: false
fun String?.htmlFormat(): String = fun String?.htmlFormat(): String =
this?.replace("</?(?:div|p|b|br|hr|h\\d|article|dd|dl|span)[^>]*>".toRegex(), "\n") this?.replace("</?(?:div|p|b|br|hr|h\\d|article|dd|dl|span|link|title)[^>]*>".toRegex(), "\n")
?.replace("<[script>]*.*?>|&nbsp;".toRegex(), "") ?.replace("<[script>]*.*?>|&nbsp;".toRegex(), "")
?.replace("\\s*\\n+\\s*".toRegex(), "\n  ") ?.replace("\\s*\\n+\\s*".toRegex(), "\n  ")
?.replace("^[\\n\\s]+".toRegex(), "  ") ?.replace("^[\\n\\s]+".toRegex(), "  ")

Loading…
Cancel
Save