Update LocalBook.kt

优化本地文件名解析,新增自定义“导入文件名”选项
pull/1049/head
bushixuanqi 3 years ago committed by GitHub
parent da3fb420f2
commit c0d9f0cb3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 97
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@ -10,7 +10,11 @@ import io.legado.app.utils.*
import splitties.init.appCtx
import java.io.File
import java.util.regex.Pattern
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppPattern
import io.legado.app.help.AppConfig
import javax.script.SimpleBindings
import java.util.regex.Matcher
object LocalBook {
private const val folderName = "bookTxt"
@ -39,7 +43,7 @@ object LocalBook {
fun importFile(uri: Uri): Book {
val path: String
val fileName = if (uri.isContentScheme()) {
val fileName = (if (uri.isContentScheme()) {
path = uri.toString()
val doc = DocumentFile.fromSingleUri(appCtx, uri)
doc?.let {
@ -55,66 +59,53 @@ object LocalBook {
} else {
path = uri.path!!
File(path).name
}
}).replace(Regex("\\.txt$"), "")
val name: String
val author: String
if (("" in fileName && "" in fileName)
|| "作者" in fileName
|| (fileName.contains(" by ", true))
) {
//匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt
val m1 = Pattern
.compile("《(.*?)》.*?作者:(.*?)\\.txt")
.matcher(fileName)
//匹配 书名 by 作者名.txt
val m2 = Pattern
.compile("txt\\.(.*?) yb (.*?)$")
.matcher(fileName.reversed())
if (m1.find()) {
name = m1.group(1) ?: fileName.replace(".txt", "")
author = m1.group(2) ?: ""
BookHelp.formatBookAuthor(author)
} else if (m2.find()) {
var temp = m2.group(2)
name = if (temp==null||temp == "") {
fileName.replace(".txt", "")
} else {
temp.reversed()
}
temp = m2.group(1) ?: ""
author = temp.reversed()
BookHelp.formatBookAuthor(author)
} else {
val st = fileName.indexOf("")
val e = fileName.indexOf("")
name = if (e > st && st != -1) {
fileName.substring(st + 1, e)
} else {
fileName.replace(".txt", "")
}
//匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt
val m1 = Pattern
.compile("(.*?)《([^《》]+)》(.*)")
.matcher(fileName)
//匹配 书名 by 作者名.txt
val m2 = Pattern
.compile("(^)(.+) by (.+)$")
.matcher(fileName)
(m1.takeIf { m1.find() }
?: m2.takeIf { m2.find() }
). run{
if(this is Matcher) {
//按默认格式将文件名分解成书名、作者名
name = group(2)!!
author = BookHelp.formatBookAuthor((group(1) ?: "") + (group(3) ?: ""))
} else if(!AppConfig.bookImportFileName.isNullOrBlank()) {
//在脚本中定义如何分解文件名成书名、作者名
val bindings = SimpleBindings()
bindings["src"] = fileName
val jsonStr = AppConst.SCRIPT_ENGINE.eval(
//在用户脚本后添加捕获author、name的代码,只要脚本中author、name有值就会被捕获,未定义则赋值为空字符串
AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})"
, bindings).toString()
val bookMess =GSON.fromJsonObject<HashMap<String, String>>(jsonStr) ?: HashMap()
name = bookMess["name"]?: fileName
author = bookMess["author"]?.takeIf { it.length != fileName.length } ?: ""
val s = fileName.indexOf("作者")
author = if (s != -1 && s + 2 < fileName.length) {
fileName.substring(s + 2).replace(".txt", "")
} else {
""
}
BookHelp.formatBookAuthor(author)
}
} else {
name = fileName.replace(AppPattern.nameRegex,"")
author = fileName.replace(AppPattern.authorRegex,"").takeIf { it.length != fileName.length } ?: ""
name = fileName.replace(".txt", "")
author = ""
}
}
}
val book = Book(
bookUrl = path,
@ -156,4 +147,4 @@ object LocalBook {
}
}
}
}
}

Loading…
Cancel
Save