优化代码

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 = val JS_PATTERN: Pattern =
Pattern.compile("(<js>[\\w\\W]*?</js>|@js:[\\w\\W]*$)", Pattern.CASE_INSENSITIVE) Pattern.compile("(<js>[\\w\\W]*?</js>|@js:[\\w\\W]*$)", Pattern.CASE_INSENSITIVE)
val EXP_PATTERN: Pattern = Pattern.compile("\\{\\{([\\w\\W]*?)\\}\\}") 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("[,;]") val splitGroupRegex = Regex("[,;]")
} }

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

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

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

Loading…
Cancel
Save