This commit is contained in:
HapeLee committed 2025-11-15 13:24:49 +08:00
1 parent d2be7ca683
commit d247f6d3a2
2 files changed
+46 -25

No files matched your search

@@ -313,41 +313,47 @@ fun Book.getExportFileName(suffix: String): String {
if (template.isNullOrBlank()) { if (template.isNullOrBlank()) {
return "$name 作者:${getRealAuthor()}.$suffix" return "$name 作者:${getRealAuthor()}.$suffix"
} }
return try { return try {
val fields = template.split(",").map { it.trim() }.filter { it.isNotEmpty() } val regex = Regex("\\{([^}]*)\\}")
val parts = fields.map { field -> val result = StringBuilder()
when (field.lowercase()) { var lastEnd = 0
"name" -> name
"author" -> getRealAuthor()
"group" -> group
"source" -> originName
else -> field
}
}
val fileName = buildString {
var prevIsVariable = false
parts.forEachIndexed { index, part ->
val isVariable = part == name || part == getRealAuthor() || part == group || part == originName
if (index > 0) { for (match in regex.findAll(template)) {
if (prevIsVariable && isVariable) { result.append(template.substring(lastEnd, match.range.first))
append("-")
} else if (!isVariable) { val inside = match.groupValues[1]
append("_")
} 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
} }
append(part) if (field != null) {
prevIsVariable = isVariable result.append(field)
} else {
result.append(inside)
} }
lastEnd = match.range.last + 1
} }
"$fileName.$suffix"
if (lastEnd < template.length) {
result.append(template.substring(lastEnd))
}
"${result}.$suffix"
} catch (e: Exception) { } catch (e: Exception) {
AppLog.put("导出书名规则错误,使用默认规则\n${e.localizedMessage}", e) AppLog.put("导出书名规则错误,使用默认规则\n${e.localizedMessage}", e)
"$name 作者:${getRealAuthor()}.$suffix" "$name 作者:${getRealAuthor()}.$suffix"
} }
} }
/** /**
* 获取分割文件后的文件名 * 获取分割文件后的文件名
*/ */
@@ -509,13 +509,27 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun alertExportFileName() { private fun alertExportFileName() {
alert(R.string.export_file_name) { 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) setMessage(message)
val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply { val alertBinding = DialogEditTextBinding.inflate(layoutInflater).apply {
editLayout.hint = "name,author,xxx" editLayout.hint = "书名:《{name}》 作者:{author}"
editView.setText(AppConfig.bookExportFileName) editView.setText(AppConfig.bookExportFileName)
} }
customView { alertBinding.root } customView { alertBinding.root }
okButton { okButton {
AppConfig.bookExportFileName = alertBinding.editView.text?.toString() AppConfig.bookExportFileName = alertBinding.editView.text?.toString()
} }
@@ -524,6 +538,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
} }
private fun getTypeName(): String { private fun getTypeName(): String {
return exportTypes.getOrElse(AppConfig.exportType) { return exportTypes.getOrElse(AppConfig.exportType) {
exportTypes[0] exportTypes[0]