Merge remote-tracking branch 'origin/master'

This commit is contained in:
kunfei
2023-03-06 15:14:36 +08:00
2 changed files with 17 additions and 18 deletions

View File

@@ -198,36 +198,35 @@ object LocalBook {
*/
private fun analyzeNameAuthor(fileName: String): Pair<String, String> {
val tempFileName = fileName.substringBeforeLast(".")
var name: String
var author: String
for (pattern in nameAuthorPatterns) {
pattern.matcher(tempFileName).takeIf { it.find() }?.run {
name = group(2)!!
val group1 = group(1) ?: ""
val group3 = group(3) ?: ""
author = BookHelp.formatBookAuthor(group1 + group3)
return Pair(name, author)
}
}
var name = ""
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变量中
//将文件名注入到脚的src变量中
SimpleBindings().also { it["src"] = tempFileName }
).toString()
val bookMess = GSON.fromJsonObject<HashMap<String, String>>(jsonStr)
.getOrThrow()
name = bookMess["name"] ?: tempFileName
name = bookMess["name"] ?: ""
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 } ?: ""
AppLog.put("执行导入文件名规则出错\n${e.localizedMessage}", e)
}
}
if (name.isBlank()) {
for (pattern in nameAuthorPatterns) {
pattern.matcher(tempFileName).takeIf { it.find() }?.run {
name = group(2)!!
val group1 = group(1) ?: ""
val group3 = group(3) ?: ""
author = BookHelp.formatBookAuthor(group1 + group3)
return Pair(name, author)
}
}
} else {
name = BookHelp.formatBookName(tempFileName)
author = BookHelp.formatBookAuthor(tempFileName.replace(name, ""))
.takeIf { it.length != tempFileName.length } ?: ""

View File

@@ -257,7 +257,7 @@ class ImportBookActivity : BaseImportBookActivity<ActivityImportBookBinding, Imp
private fun alertImportFileName() {
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 {
editView.hint = "js"
editView.setText(AppConfig.bookImportFileName)