pull/1744/head
kunfei 3 years ago
parent dc15374166
commit d4ff2d8fcb
  1. 32
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@ -16,12 +16,17 @@ import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.InputStream
import java.util.regex.Matcher
import java.util.regex.Pattern
import javax.script.SimpleBindings
object LocalBook {
private val nameAuthorPatterns = arrayOf(
Pattern.compile("(.*?)《([^《》]+)》(.*)"),
Pattern.compile("(^)(.+) 作者:(.+)$"),
Pattern.compile("(^)(.+) by (.+)$")
)
@Throws(FileNotFoundException::class, SecurityException::class)
fun getBookInputStream(book: Book): InputStream {
val uri = Uri.parse(book.bookUrl)
@ -117,21 +122,16 @@ object LocalBook {
val tempFileName = fileName.substringBeforeLast(".")
var name: String
var author: String
//匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt
val m1 = Pattern
.compile("(.*?)《([^《》]+)》(.*)")
.matcher(tempFileName)
//匹配 书名 作者:作者名.txt
val m2 = Pattern
.compile("(^)(.+) 作者:(.+)$")
.matcher(tempFileName)
(m1.takeIf { m1.find() } ?: m2.takeIf { m2.find() }).run {
if (this is Matcher) {
//按默认格式将文件名分解成书名、作者名
for (pattern in nameAuthorPatterns) {
pattern.matcher(tempFileName).takeIf { it.find() }?.run {
name = group(2)!!
author = BookHelp.formatBookAuthor((group(1) ?: "") + (group(3) ?: ""))
} else if (!AppConfig.bookImportFileName.isNullOrBlank()) {
val group1 = group(1) ?: ""
val group3 = group(3) ?: ""
author = BookHelp.formatBookAuthor(group1 + group3)
return Pair(name, author)
}
}
if (!AppConfig.bookImportFileName.isNullOrBlank()) {
try {
//在脚本中定义如何分解文件名成书名、作者名
val jsonStr = AppConst.SCRIPT_ENGINE.eval(
@ -154,8 +154,6 @@ object LocalBook {
author = BookHelp.formatBookAuthor(tempFileName.replace(name, ""))
.takeIf { it.length != tempFileName.length } ?: ""
}
}
return Pair(name, author)
}

Loading…
Cancel
Save