优化代码

pull/346/head
gedoor 4 years ago
parent f8701a88d6
commit 280f2f0a3d
  1. 7
      app/src/main/java/io/legado/app/constant/AppPattern.kt
  2. 5
      app/src/main/java/io/legado/app/data/entities/Book.kt
  3. 34
      app/src/main/java/io/legado/app/help/BookHelp.kt
  4. 6
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt

@ -6,8 +6,11 @@ object AppPattern {
val JS_PATTERN: Pattern =
Pattern.compile("(<js>[\\w\\W]*?</js>|@js:[\\w\\W]*$)", Pattern.CASE_INSENSITIVE)
val EXP_PATTERN: Pattern = Pattern.compile("\\{\\{([\\w\\W]*?)\\}\\}")
val imgPattern =
Pattern.compile("<img .*?src.*?=.*?\"(.*?(?:,\\{.*\\})?)\".*?>", Pattern.CASE_INSENSITIVE)
val authorRegex = "\\s*者\\s*[::]".toRegex()
val nameRegex = Regex("\\s+作\\s*者.*")
val authorRegex = Regex(".*?作\\s*?者[::]")
val fileNameRegex = Regex("[\\\\/:*?\"<>|.]")
val splitGroupRegex = Regex("[,;]")
}

@ -11,6 +11,7 @@ import io.legado.app.constant.BookType
import io.legado.app.help.AppConfig
import io.legado.app.service.help.ReadBook
import io.legado.app.utils.GSON
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.fromJsonObject
import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize
@ -115,6 +116,10 @@ data class Book(
return charset(charset ?: "UTF-8")
}
fun getFolderName(): String {
return name.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(bookUrl)
}
fun toSearchBook(): SearchBook {
return SearchBook(
name = name,

@ -2,6 +2,7 @@ package io.legado.app.help
import com.hankcs.hanlp.HanLP
import io.legado.app.App
import io.legado.app.constant.AppPattern
import io.legado.app.constant.EventBus
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
@ -16,19 +17,12 @@ import kotlinx.coroutines.withContext
import org.apache.commons.text.similarity.JaccardSimilarity
import org.jetbrains.anko.toast
import java.io.File
import java.util.regex.Pattern
import kotlin.math.min
object BookHelp {
private const val cacheFolderName = "book_cache"
private const val cacheImageFolderName = "images"
private val downloadDir: File = App.INSTANCE.externalFilesDir
private val srcPattern =
Pattern.compile("<img .*?src.*?=.*?\"(.*?(?:,\\{.*\\})?)\".*?>", Pattern.CASE_INSENSITIVE)
fun bookFolderName(book: Book): String {
return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl)
}
fun formatChapterName(bookChapter: BookChapter): String {
return String.format(
@ -51,7 +45,7 @@ object BookHelp {
Coroutine.async {
val bookFolderNames = arrayListOf<String>()
App.db.bookDao().all.forEach {
bookFolderNames.add(bookFolderName(it))
bookFolderNames.add(it.getFolderName())
}
val file = FileUtils.getDirFile(downloadDir, cacheFolderName)
file.listFiles()?.forEach { bookFile ->
@ -69,11 +63,11 @@ object BookHelp {
FileUtils.createFileIfNotExist(
downloadDir,
formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, bookFolderName(book))
subDirs = arrayOf(cacheFolderName, book.getFolderName())
).writeText(content)
//保存图片
content.split("\n").forEach {
val matcher = srcPattern.matcher(it)
val matcher = AppPattern.imgPattern.matcher(it)
if (matcher.find()) {
var src = matcher.group(1)
src = NetworkUtils.getAbsoluteURL(bookChapter.url, src)
@ -91,7 +85,7 @@ object BookHelp {
FileUtils.createFileIfNotExist(
downloadDir,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}",
subDirs = arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName)
subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName)
).writeBytes(it)
}
}
@ -100,7 +94,7 @@ object BookHelp {
return FileUtils.getFile(
downloadDir,
"${MD5Utils.md5Encode16(src)}${getImageSuffix(src)}",
subDirs = arrayOf(cacheFolderName, bookFolderName(book), cacheImageFolderName)
subDirs = arrayOf(cacheFolderName, book.getFolderName(), cacheImageFolderName)
)
}
@ -116,7 +110,7 @@ object BookHelp {
val fileNameList = arrayListOf<String>()
FileUtils.createFolderIfNotExist(
downloadDir,
subDirs = arrayOf(cacheFolderName, bookFolderName(book))
subDirs = arrayOf(cacheFolderName, book.getFolderName())
).list()?.let {
fileNameList.addAll(it)
}
@ -130,7 +124,7 @@ object BookHelp {
FileUtils.exists(
downloadDir,
formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, bookFolderName(book))
subDirs = arrayOf(cacheFolderName, book.getFolderName())
)
}
}
@ -142,7 +136,7 @@ object BookHelp {
val file = FileUtils.getFile(
downloadDir,
formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, bookFolderName(book))
subDirs = arrayOf(cacheFolderName, book.getFolderName())
)
if (file.exists()) {
return file.readText()
@ -158,24 +152,20 @@ object BookHelp {
FileUtils.createFileIfNotExist(
downloadDir,
formatChapterName(bookChapter),
subDirs = arrayOf(cacheFolderName, bookFolderName(book))
subDirs = arrayOf(cacheFolderName, book.getFolderName())
).delete()
}
}
private fun formatFolderName(folderName: String): String {
return folderName.replace("[\\\\/:*?\"<>|.]".toRegex(), "")
}
fun formatBookName(name: String): String {
return name
.replace("\\s+作\\s*者.*".toRegex(), "")
.replace(AppPattern.nameRegex, "")
.trim { it <= ' ' }
}
fun formatBookAuthor(author: String): String {
return author
.replace(".*?作\\s*?者[::]".toRegex(), "")
.replace(AppPattern.authorRegex, "")
.trim { it <= ' ' }
}

@ -7,6 +7,7 @@ import android.text.StaticLayout
import android.text.TextPaint
import android.text.TextUtils
import io.legado.app.App
import io.legado.app.constant.AppPattern
import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
@ -18,7 +19,6 @@ import io.legado.app.ui.book.read.page.entities.TextLine
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.*
import java.util.*
import java.util.regex.Pattern
@Suppress("DEPRECATION")
@ -38,8 +38,6 @@ object ChapterProvider {
var typeface: Typeface = Typeface.SANS_SERIF
lateinit var titlePaint: TextPaint
lateinit var contentPaint: TextPaint
private val srcPattern =
Pattern.compile("<img .*?src.*?=.*?\"(.*?(?:,\\{.*\\})?)\".*?>", Pattern.CASE_INSENSITIVE)
init {
upStyle()
@ -62,7 +60,7 @@ object ChapterProvider {
var durY = 0f
textPages.add(TextPage())
contents.forEachIndexed { index, text ->
val matcher = srcPattern.matcher(text)
val matcher = AppPattern.imgPattern.matcher(text)
if (matcher.find()) {
var src = matcher.group(1)
if (!book.isEpub()) {

Loading…
Cancel
Save