pull/1395/head
gedoor 4 years ago
parent 3b14a5f073
commit d518a08a39
  1. 42
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@ -100,48 +100,44 @@ object LocalBook {
fun analyzeNameAuthor(fileName: String): Pair<String, String> { fun analyzeNameAuthor(fileName: String): Pair<String, String> {
val tempFileName = fileName.substringBeforeLast(".") val tempFileName = fileName.substringBeforeLast(".")
val name: String var name: String
val author: String var author: String
//匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt //匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt
val m1 = Pattern val m1 = Pattern
.compile("(.*?)《([^《》]+)》(.*)") .compile("(.*?)《([^《》]+)》(.*)")
.matcher(tempFileName) .matcher(tempFileName)
//匹配 书名 by 作者名.txt //匹配 书名 by 作者名.txt
val m2 = Pattern val m2 = Pattern
.compile("(^)(.+) by (.+)$") .compile("(^)(.+) by (.+)$")
.matcher(tempFileName) .matcher(tempFileName)
(m1.takeIf { m1.find() } ?: m2.takeIf { m2.find() }).run { (m1.takeIf { m1.find() } ?: m2.takeIf { m2.find() }).run {
if (this is Matcher) { if (this is Matcher) {
//按默认格式将文件名分解成书名、作者名 //按默认格式将文件名分解成书名、作者名
name = group(2)!! name = group(2)!!
author = BookHelp.formatBookAuthor((group(1) ?: "") + (group(3) ?: "")) author = BookHelp.formatBookAuthor((group(1) ?: "") + (group(3) ?: ""))
} else if (!AppConfig.bookImportFileName.isNullOrBlank()) { } else if (!AppConfig.bookImportFileName.isNullOrBlank()) {
try {
//在脚本中定义如何分解文件名成书名、作者名 //在脚本中定义如何分解文件名成书名、作者名
val jsonStr = AppConst.SCRIPT_ENGINE.eval( val jsonStr = AppConst.SCRIPT_ENGINE.eval(
//在用户脚本后添加捕获author、name的代码,只要脚本中author、name有值就会被捕获
//在用户脚本后添加捕获author、name的代码,只要脚本中author、name有值就会被捕获 AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})",
AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})", //将文件名注入到脚步的src变量中
SimpleBindings().also { it["src"] = tempFileName }
//将文件名注入到脚步的src变量中 ).toString()
SimpleBindings().also { it["src"] = tempFileName } val bookMess =
).toString() GSON.fromJsonObject<HashMap<String, String>>(jsonStr) ?: HashMap()
val bookMess = GSON.fromJsonObject<HashMap<String, String>>(jsonStr) ?: HashMap() name = bookMess["name"] ?: tempFileName
name = bookMess["name"] ?: tempFileName author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: ""
author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: "" } catch (e: Exception) {
name = tempFileName.replace(AppPattern.nameRegex, "")
author = tempFileName.replace(AppPattern.authorRegex, "")
.takeIf { it.length != tempFileName.length } ?: ""
}
} else { } else {
name = tempFileName.replace(AppPattern.nameRegex, "") name = tempFileName.replace(AppPattern.nameRegex, "")
author = tempFileName.replace(AppPattern.authorRegex, "") author = tempFileName.replace(AppPattern.authorRegex, "")
.takeIf { it.length != tempFileName.length } ?: "" .takeIf { it.length != tempFileName.length } ?: ""
} }
} }

Loading…
Cancel
Save