This commit is contained in:
HapeLee
2025-11-15 13:24:49 +08:00
parent d2be7ca683
commit d247f6d3a2
2 changed files with 47 additions and 26 deletions
@@ -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 fileName = buildString {
var prevIsVariable = false
parts.forEachIndexed { index, part ->
val isVariable = part == name || part == getRealAuthor() || part == group || part == originName
val regex = Regex("\\{([^}]*)\\}")
val result = StringBuilder()
var lastEnd = 0
if (index > 0) {
if (prevIsVariable && isVariable) {
append("-")
} else if (!isVariable) {
append("_")
}
}
for (match in regex.findAll(template)) {
result.append(template.substring(lastEnd, match.range.first))
append(part)
prevIsVariable = isVariable
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
}
if (field != null) {
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) {
AppLog.put("导出书名规则错误,使用默认规则\n${e.localizedMessage}", e)
"$name 作者:${getRealAuthor()}.$suffix"
}
}
/**
* 获取分割文件后的文件名
*/
@@ -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]