This commit is contained in:
Horis
2023-03-06 15:12:57 +08:00
parent ee44e37ca0
commit 107a17d45a
2 changed files with 17 additions and 18 deletions
@@ -198,8 +198,26 @@ object LocalBook {
*/ */
private fun analyzeNameAuthor(fileName: String): Pair<String, String> { private fun analyzeNameAuthor(fileName: String): Pair<String, String> {
val tempFileName = fileName.substringBeforeLast(".") val tempFileName = fileName.substringBeforeLast(".")
var name: String var name = ""
var author: String var author = ""
if (!AppConfig.bookImportFileName.isNullOrBlank()) {
try {
//在脚本中定义如何分解文件名成书名、作者名
val jsonStr = AppConst.SCRIPT_ENGINE.eval(
//在用户脚本后添加捕获author、name的代码,只要脚本中author、name有值就会被捕获
AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})",
//将文件名注入到脚本的src变量中
SimpleBindings().also { it["src"] = tempFileName }
).toString()
val bookMess = GSON.fromJsonObject<HashMap<String, String>>(jsonStr)
.getOrThrow()
name = bookMess["name"] ?: ""
author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: ""
} catch (e: Exception) {
AppLog.put("执行导入文件名规则出错\n${e.localizedMessage}", e)
}
}
if (name.isBlank()) {
for (pattern in nameAuthorPatterns) { for (pattern in nameAuthorPatterns) {
pattern.matcher(tempFileName).takeIf { it.find() }?.run { pattern.matcher(tempFileName).takeIf { it.find() }?.run {
name = group(2)!! name = group(2)!!
@@ -209,25 +227,6 @@ object LocalBook {
return Pair(name, author) return Pair(name, author)
} }
} }
if (!AppConfig.bookImportFileName.isNullOrBlank()) {
try {
//在脚本中定义如何分解文件名成书名、作者名
val jsonStr = AppConst.SCRIPT_ENGINE.eval(
//在用户脚本后添加捕获author、name的代码,只要脚本中author、name有值就会被捕获
AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})",
//将文件名注入到脚步的src变量中
SimpleBindings().also { it["src"] = tempFileName }
).toString()
val bookMess = GSON.fromJsonObject<HashMap<String, String>>(jsonStr)
.getOrThrow()
name = bookMess["name"] ?: tempFileName
author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: ""
} catch (e: Exception) {
name = BookHelp.formatBookName(tempFileName)
author = BookHelp.formatBookAuthor(tempFileName.replace(name, ""))
.takeIf { it.length != tempFileName.length } ?: ""
}
} else {
name = BookHelp.formatBookName(tempFileName) name = BookHelp.formatBookName(tempFileName)
author = BookHelp.formatBookAuthor(tempFileName.replace(name, "")) author = BookHelp.formatBookAuthor(tempFileName.replace(name, ""))
.takeIf { it.length != tempFileName.length } ?: "" .takeIf { it.length != tempFileName.length } ?: ""
@@ -257,7 +257,7 @@ class ImportBookActivity : BaseImportBookActivity<ActivityImportBookBinding, Imp
private fun alertImportFileName() { private fun alertImportFileName() {
alert(R.string.import_file_name) { alert(R.string.import_file_name) {
setMessage("""使用js处理文件名变量src返回一个json结构,{"name":"xxx", "author":"yyy"}""") setMessage("""使用js处理文件名变量src,将书名作者分别赋值到变量name author""")
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editView.hint = "js" editView.hint = "js"
editView.setText(AppConfig.bookImportFileName) editView.setText(AppConfig.bookImportFileName)