pull/1514/head
gedoor 3 years ago
parent 7d1cf61b78
commit 08cc956f92
  1. 54
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt

@ -49,6 +49,9 @@ class TextFile(private val book: Book) {
private val tocRules = arrayListOf<Pattern>() private val tocRules = arrayListOf<Pattern>()
private var charset: Charset = book.fileCharset() private var charset: Charset = book.fileCharset()
/**
* 获取目录
*/
@Throws(FileNotFoundException::class) @Throws(FileNotFoundException::class)
fun getChapterList(): ArrayList<BookChapter> { fun getChapterList(): ArrayList<BookChapter> {
val rulePattern: Pattern? = if (book.charset == null || book.tocUrl.isEmpty()) { val rulePattern: Pattern? = if (book.charset == null || book.tocUrl.isEmpty()) {
@ -78,6 +81,9 @@ class TextFile(private val book: Book) {
} ?: analyze() } ?: analyze()
} }
/**
* 按规则解析目录
*/
private fun analyze(pattern: Pattern): ArrayList<BookChapter> { private fun analyze(pattern: Pattern): ArrayList<BookChapter> {
val toc = arrayListOf<BookChapter>() val toc = arrayListOf<BookChapter>()
LocalBook.getBookInputStream(book).use { bis -> LocalBook.getBookInputStream(book).use { bis ->
@ -119,7 +125,7 @@ class TextFile(private val book: Book) {
//获取章节内容 //获取章节内容
val chapterContent = blockContent.substring(seekPos, chapterStart) val chapterContent = blockContent.substring(seekPos, chapterStart)
val chapterLength = chapterContent.toByteArray(charset).size val chapterLength = chapterContent.toByteArray(charset).size
val lastStart = toc.lastOrNull()?.start ?: 0 val lastStart = toc.lastOrNull()?.start ?: curOffset
if (curOffset + chapterLength - lastStart > 50000) { if (curOffset + chapterLength - lastStart > 50000) {
bis.close() bis.close()
return analyze() return analyze()
@ -168,18 +174,13 @@ class TextFile(private val book: Book) {
val curChapter = BookChapter() val curChapter = BookChapter()
curChapter.title = matcher.group() curChapter.title = matcher.group()
curChapter.start = curOffset curChapter.start = curOffset
curChapter.end = 0 curChapter.end = curOffset
toc.add(curChapter) toc.add(curChapter)
} }
} }
//设置指针偏移 //设置指针偏移
seekPos += chapterContent.length seekPos += chapterContent.length
} }
if (seekPos == 0 && length > 50000) {
bis.close()
return analyze()
}
//block的偏移点 //block的偏移点
curOffset += length.toLong() curOffset += length.toLong()
//设置上一章的结尾 //设置上一章的结尾
@ -201,6 +202,9 @@ class TextFile(private val book: Book) {
return toc return toc
} }
/**
* 无规则拆分目录
*/
private fun analyze(): ArrayList<BookChapter> { private fun analyze(): ArrayList<BookChapter> {
nextTocRule()?.let { nextTocRule()?.let {
return analyze(it) return analyze(it)
@ -228,14 +232,14 @@ class TextFile(private val book: Book) {
//章节在buffer的偏移量 //章节在buffer的偏移量
var chapterOffset = 0 var chapterOffset = 0
//当前剩余可分配的长度 //当前剩余可分配的长度
var strLength = length var strLength = bufferStart + length
//分章的位置 //分章的位置
var chapterPos = 0 var chapterPos = 0
while (strLength > 0) { while (strLength > 0) {
++chapterPos ++chapterPos
//是否长度超过一章 //是否长度超过一章
if (strLength > maxLengthWithNoToc) { //在buffer中一章的终止点 if (strLength > maxLengthWithNoToc) { //在buffer中一章的终止点
var end = length var end = bufferStart + length
//寻找换行符作为终止点 //寻找换行符作为终止点
for (i in chapterOffset + maxLengthWithNoToc until length) { for (i in chapterOffset + maxLengthWithNoToc until length) {
if (buffer[i] == blank) { if (buffer[i] == blank) {
@ -245,25 +249,33 @@ class TextFile(private val book: Book) {
} }
val chapter = BookChapter() val chapter = BookChapter()
chapter.title = "${blockPos}章($chapterPos)" chapter.title = "${blockPos}章($chapterPos)"
chapter.start = curOffset + chapterOffset chapter.start = toc.lastOrNull()?.end ?: curOffset
chapter.end = curOffset + end chapter.end = chapter.start!! + end - chapterOffset
toc.add(chapter) toc.add(chapter)
//减去已经被分配的长度 //减去已经被分配的长度
strLength -= (end - chapterOffset) strLength -= (end - chapterOffset)
//设置偏移的位置 //设置偏移的位置
chapterOffset = end chapterOffset = end
} else { } else {
buffer.copyInto(buffer, 0, length - strLength, strLength) buffer.copyInto(
buffer,
0,
bufferStart + length - strLength,
bufferStart + length
)
bufferStart = strLength bufferStart = strLength
break
} }
} }
//block的偏移点 //block的偏移点
curOffset += length.toLong() curOffset += length.toLong()
//设置上一章的结尾 //设置上一章的结尾
toc.lastOrNull()?.end = curOffset toc.lastOrNull()?.end = curOffset
} }
} }
if (toc.isEmpty()) {
return analyze()
}
toc.forEachIndexed { index, bookChapter -> toc.forEachIndexed { index, bookChapter ->
bookChapter.index = index bookChapter.index = index
bookChapter.bookUrl = book.bookUrl bookChapter.bookUrl = book.bookUrl
@ -279,20 +291,29 @@ class TextFile(private val book: Book) {
return toc return toc
} }
/**
* 初始化并获取匹配次数最多的规则
*/
private fun getTocRule(content: String): Pattern? { private fun getTocRule(content: String): Pattern? {
tocRules.addAll(getTocRules(content, getTocRules().reversed())) tocRules.addAll(getTocRules(content, getTocRules().reversed()))
tocRules.addAll(getTocRules(content, appDb.txtTocRuleDao.disabled.reversed())) tocRules.addAll(getTocRules(content, appDb.txtTocRuleDao.disabled.reversed()))
return tocRules.firstOrNull() return tocRules.firstOrNull()
} }
/**
* 获取下一个规则并移除上一个
*/
private fun nextTocRule(): Pattern? { private fun nextTocRule(): Pattern? {
tocRules.removeFirstOrNull() tocRules.removeFirstOrNull()
return tocRules.firstOrNull() return tocRules.firstOrNull()
} }
/**
* 获取所有匹配次数大于1的目录规则
*/
private fun getTocRules(content: String, rules: List<TxtTocRule>): ArrayList<Pattern> { private fun getTocRules(content: String, rules: List<TxtTocRule>): ArrayList<Pattern> {
val list = arrayListOf<Pattern>() val list = arrayListOf<Pattern>()
var maxCs = 0 var maxCs = 1
for (tocRule in rules) { for (tocRule in rules) {
val pattern = Pattern.compile(tocRule.rule, Pattern.MULTILINE) val pattern = Pattern.compile(tocRule.rule, Pattern.MULTILINE)
val matcher = pattern.matcher(content) val matcher = pattern.matcher(content)
@ -303,13 +324,16 @@ class TextFile(private val book: Book) {
if (cs >= maxCs) { if (cs >= maxCs) {
maxCs = cs maxCs = cs
list.add(0, pattern) list.add(0, pattern)
} else if (cs > 0) { } else if (cs > 1) {
list.add(pattern) list.add(pattern)
} }
} }
return list return list
} }
/**
* 获取启用的目录规则
*/
private fun getTocRules(): List<TxtTocRule> { private fun getTocRules(): List<TxtTocRule> {
var rules = appDb.txtTocRuleDao.enabled var rules = appDb.txtTocRuleDao.enabled
if (rules.isEmpty()) { if (rules.isEmpty()) {

Loading…
Cancel
Save