فهرست منبع

Merge remote-tracking branch 'origin/master'

kunfei 3 سال پیش
والد
کامیت
54ee360369

+ 16 - 17
app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@@ -198,36 +198,35 @@ 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 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()) {
         if (!AppConfig.bookImportFileName.isNullOrBlank()) {
             try {
             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变量中
+                    //将文件名注入到脚本的src变量中
                     SimpleBindings().also { it["src"] = tempFileName }
                     SimpleBindings().also { it["src"] = tempFileName }
                 ).toString()
                 ).toString()
                 val bookMess = GSON.fromJsonObject<HashMap<String, String>>(jsonStr)
                 val bookMess = GSON.fromJsonObject<HashMap<String, String>>(jsonStr)
                     .getOrThrow()
                     .getOrThrow()
-                name = bookMess["name"] ?: tempFileName
+                name = bookMess["name"] ?: ""
                 author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: ""
                 author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: ""
             } catch (e: Exception) {
             } 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)
             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 } ?: ""

+ 1 - 1
app/src/main/java/io/legado/app/ui/book/import/local/ImportBookActivity.kt

@@ -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)