HapeLee пре 1 година
родитељ
комит
d247f6d3a2

+ 31 - 25
app/src/main/java/io/legado/app/help/book/BookExtensions.kt

@@ -313,41 +313,47 @@ fun Book.getExportFileName(suffix: String): String {
     if (template.isNullOrBlank()) {
         return "$name 作者:${getRealAuthor()}.$suffix"
     }
+
     return try {
-        val fields = template.split(",").map { it.trim() }.filter { it.isNotEmpty() }
-        val parts = fields.map { field ->
-            when (field.lowercase()) {
-                "name" -> name
-                "author" -> getRealAuthor()
-                "group" -> group
-                "source" -> originName
-                else -> field
+        val regex = Regex("\\{([^}]*)\\}")
+        val result = StringBuilder()
+        var lastEnd = 0
+
+        for (match in regex.findAll(template)) {
+            result.append(template.substring(lastEnd, match.range.first))
+
+            val inside = match.groupValues[1]
+
+            val field = when {
+                inside.equals("name", ignoreCase = true) -> name
+                inside.equals("author", ignoreCase = true) -> getRealAuthor()
+                inside.equals("group", ignoreCase = true) -> group
+                inside.equals("source", ignoreCase = true) -> originName
+                else -> null
             }
-        }
-        val fileName = buildString {
-            var prevIsVariable = false
-            parts.forEachIndexed { index, part ->
-                val isVariable = part == name || part == getRealAuthor() || part == group || part == originName
-
-                if (index > 0) {
-                    if (prevIsVariable && isVariable) {
-                        append("-")
-                    } else if (!isVariable) {
-                        append("_")
-                    }
-                }
-
-                append(part)
-                prevIsVariable = isVariable
+
+            if (field != null) {
+                result.append(field)
+            } else {
+                result.append(inside)
             }
+
+            lastEnd = match.range.last + 1
+        }
+        
+        if (lastEnd < template.length) {
+            result.append(template.substring(lastEnd))
         }
-        "$fileName.$suffix"
+
+        "${result}.$suffix"
+
     } catch (e: Exception) {
         AppLog.put("导出书名规则错误,使用默认规则\n${e.localizedMessage}", e)
         "$name 作者:${getRealAuthor()}.$suffix"
     }
 }
 
+
 /**
  * 获取分割文件后的文件名
  */

+ 17 - 2
app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt

@@ -509,13 +509,27 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
     @SuppressLint("SetTextI18n")
     private fun alertExportFileName() {
         alert(R.string.export_file_name) {
-            val message = "支持变量:name(书籍名),author(作者),group(分组),source(书源名), 使用半角逗号分割,按照输入排序导出,其他字段视作备注。"
+            val message = """
+支持变量:{name}(书名)、{author}(作者)、{group}(分组)、{source}(书源)。
+可以在字段前后加任意字符。
+
+示例:
+书名:《{name}》 作者:{author}
+输出:书名:《三体》 作者:刘慈欣
+
+书名:《{name}》-作者:{author}_备注
+输出:书名:《三体》-作者:刘慈欣_备注
+        """.trimIndent()
+
             setMessage(message)
+
             val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
-                editLayout.hint = "name,author,xxx"
+                editLayout.hint = "书名:《{name}》 作者:{author}"
                 editView.setText(AppConfig.bookExportFileName)
             }
+
             customView { alertBinding.root }
+
             okButton {
                 AppConfig.bookExportFileName = alertBinding.editView.text?.toString()
             }
@@ -524,6 +538,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
     }
 
 
+
     private fun getTypeName(): String {
         return exportTypes.getOrElse(AppConfig.exportType) {
             exportTypes[0]