mirror of
https://github.com/gedoor/legado.git
synced 2024-07-19 01:17:25 +08:00
优化
This commit is contained in:
parent
ac2eda9809
commit
27535803e4
@ -16,6 +16,9 @@ interface TxtTocRuleDao {
|
|||||||
@get:Query("select * from txtTocRules where enable = 1 order by serialNumber")
|
@get:Query("select * from txtTocRules where enable = 1 order by serialNumber")
|
||||||
val enabled: List<TxtTocRule>
|
val enabled: List<TxtTocRule>
|
||||||
|
|
||||||
|
@get:Query("select * from txtTocRules where enable != 1 order by serialNumber")
|
||||||
|
val disabled: List<TxtTocRule>
|
||||||
|
|
||||||
@get:Query("select ifNull(min(serialNumber), 0) from txtTocRules")
|
@get:Query("select ifNull(min(serialNumber), 0) from txtTocRules")
|
||||||
val minOrder: Int
|
val minOrder: Int
|
||||||
|
|
||||||
|
@ -16,6 +16,28 @@ import java.util.regex.Pattern
|
|||||||
|
|
||||||
class TextFile(private val book: Book) {
|
class TextFile(private val book: Book) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
@Throws(FileNotFoundException::class)
|
||||||
|
fun getChapterList(book: Book): ArrayList<BookChapter> {
|
||||||
|
return TextFile(book).getChapterList()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Throws(FileNotFoundException::class)
|
||||||
|
fun getContent(book: Book, bookChapter: BookChapter): String {
|
||||||
|
val count = (bookChapter.end!! - bookChapter.start!!).toInt()
|
||||||
|
val buffer = ByteArray(count)
|
||||||
|
LocalBook.getBookInputStream(book).use { bis ->
|
||||||
|
bis.skip(bookChapter.start!!)
|
||||||
|
bis.read(buffer)
|
||||||
|
}
|
||||||
|
return String(buffer, book.fileCharset())
|
||||||
|
.substringAfter(bookChapter.title)
|
||||||
|
.replace("^[\\n\\s]+".toRegex(), " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private val blank: Byte = 0x0a
|
private val blank: Byte = 0x0a
|
||||||
|
|
||||||
//默认从文件中获取数据的长度
|
//默认从文件中获取数据的长度
|
||||||
@ -24,7 +46,7 @@ class TextFile(private val book: Book) {
|
|||||||
//没有标题的时候,每个章节的最大长度
|
//没有标题的时候,每个章节的最大长度
|
||||||
private val maxLengthWithNoToc = 10 * 1024
|
private val maxLengthWithNoToc = 10 * 1024
|
||||||
|
|
||||||
private val tocRules = arrayListOf<TxtTocRule>()
|
private val tocRules = arrayListOf<Pattern>()
|
||||||
private var charset: Charset = book.fileCharset()
|
private var charset: Charset = book.fileCharset()
|
||||||
|
|
||||||
@Throws(FileNotFoundException::class)
|
@Throws(FileNotFoundException::class)
|
||||||
@ -45,7 +67,7 @@ class TextFile(private val book: Book) {
|
|||||||
book.charset = EncodingDetect.getEncode(buffer)
|
book.charset = EncodingDetect.getEncode(buffer)
|
||||||
blockContent = String(buffer, 0, length, charset)
|
blockContent = String(buffer, 0, length, charset)
|
||||||
}
|
}
|
||||||
getTocRule(blockContent)?.rule?.toPattern(Pattern.MULTILINE)
|
getTocRule(blockContent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -183,9 +205,8 @@ class TextFile(private val book: Book) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun analyze(): ArrayList<BookChapter> {
|
private fun analyze(): ArrayList<BookChapter> {
|
||||||
tocRules.removeFirstOrNull()
|
nextTocRule()?.let {
|
||||||
tocRules.firstOrNull()?.let {
|
return analyze(it)
|
||||||
return analyze(it.rule.toPattern(Pattern.MULTILINE))
|
|
||||||
}
|
}
|
||||||
val toc = arrayListOf<BookChapter>()
|
val toc = arrayListOf<BookChapter>()
|
||||||
LocalBook.getBookInputStream(book).use { bis ->
|
LocalBook.getBookInputStream(book).use { bis ->
|
||||||
@ -266,13 +287,24 @@ class TextFile(private val book: Book) {
|
|||||||
return toc
|
return toc
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private fun getTocRule(content: String): Pattern? {
|
||||||
* 获取匹配次数最多的目录规则
|
tocRules.addAll(getTocRules(content, true))
|
||||||
*/
|
tocRules.addAll(getTocRules(content, false))
|
||||||
private fun getTocRule(content: String): TxtTocRule? {
|
return tocRules.firstOrNull()
|
||||||
tocRules.clear()
|
}
|
||||||
val rules = getTocRules().reversed()
|
|
||||||
var txtTocRule: TxtTocRule? = null
|
private fun nextTocRule(): Pattern? {
|
||||||
|
tocRules.removeFirstOrNull()
|
||||||
|
return tocRules.firstOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTocRules(content: String, isMain: Boolean): ArrayList<Pattern> {
|
||||||
|
val list = arrayListOf<Pattern>()
|
||||||
|
val rules = if (isMain) {
|
||||||
|
getTocRules().reversed()
|
||||||
|
} else {
|
||||||
|
appDb.txtTocRuleDao.disabled.reversed()
|
||||||
|
}
|
||||||
var maxCs = 0
|
var maxCs = 0
|
||||||
for (tocRule in rules) {
|
for (tocRule in rules) {
|
||||||
val pattern = Pattern.compile(tocRule.rule, Pattern.MULTILINE)
|
val pattern = Pattern.compile(tocRule.rule, Pattern.MULTILINE)
|
||||||
@ -282,48 +314,25 @@ class TextFile(private val book: Book) {
|
|||||||
cs++
|
cs++
|
||||||
}
|
}
|
||||||
if (cs >= maxCs) {
|
if (cs >= maxCs) {
|
||||||
tocRules.add(0, tocRule)
|
|
||||||
maxCs = cs
|
maxCs = cs
|
||||||
txtTocRule = tocRule
|
list.add(0, pattern)
|
||||||
} else if (cs > 0) {
|
} else if (cs > 0) {
|
||||||
tocRules.add(tocRule)
|
list.add(pattern)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return txtTocRule
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
private fun getTocRules(): List<TxtTocRule> {
|
||||||
|
var rules = appDb.txtTocRuleDao.enabled
|
||||||
@Throws(FileNotFoundException::class)
|
if (rules.isEmpty()) {
|
||||||
fun getChapterList(book: Book): ArrayList<BookChapter> {
|
rules = DefaultData.txtTocRules.apply {
|
||||||
return TextFile(book).getChapterList()
|
appDb.txtTocRuleDao.insert(*this.toTypedArray())
|
||||||
}
|
}.filter {
|
||||||
|
it.enable
|
||||||
@Throws(FileNotFoundException::class)
|
|
||||||
fun getContent(book: Book, bookChapter: BookChapter): String {
|
|
||||||
val count = (bookChapter.end!! - bookChapter.start!!).toInt()
|
|
||||||
val buffer = ByteArray(count)
|
|
||||||
LocalBook.getBookInputStream(book).use { bis ->
|
|
||||||
bis.skip(bookChapter.start!!)
|
|
||||||
bis.read(buffer)
|
|
||||||
}
|
}
|
||||||
return String(buffer, book.fileCharset())
|
|
||||||
.substringAfter(bookChapter.title)
|
|
||||||
.replace("^[\\n\\s]+".toRegex(), " ")
|
|
||||||
}
|
}
|
||||||
|
return rules
|
||||||
private fun getTocRules(): List<TxtTocRule> {
|
|
||||||
var rules = appDb.txtTocRuleDao.enabled
|
|
||||||
if (rules.isEmpty()) {
|
|
||||||
rules = DefaultData.txtTocRules.apply {
|
|
||||||
appDb.txtTocRuleDao.insert(*this.toTypedArray())
|
|
||||||
}.filter {
|
|
||||||
it.enable
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return rules
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user