|
|
@ -10,7 +10,11 @@ import io.legado.app.utils.* |
|
|
|
import splitties.init.appCtx |
|
|
|
import splitties.init.appCtx |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
import java.util.regex.Pattern |
|
|
|
import java.util.regex.Pattern |
|
|
|
|
|
|
|
import io.legado.app.constant.AppConst |
|
|
|
|
|
|
|
import io.legado.app.constant.AppPattern |
|
|
|
|
|
|
|
import io.legado.app.help.AppConfig |
|
|
|
|
|
|
|
import javax.script.SimpleBindings |
|
|
|
|
|
|
|
import java.util.regex.Matcher |
|
|
|
|
|
|
|
|
|
|
|
object LocalBook { |
|
|
|
object LocalBook { |
|
|
|
private const val folderName = "bookTxt" |
|
|
|
private const val folderName = "bookTxt" |
|
|
@ -39,7 +43,7 @@ object LocalBook { |
|
|
|
|
|
|
|
|
|
|
|
fun importFile(uri: Uri): Book { |
|
|
|
fun importFile(uri: Uri): Book { |
|
|
|
val path: String |
|
|
|
val path: String |
|
|
|
val fileName = if (uri.isContentScheme()) { |
|
|
|
val fileName = (if (uri.isContentScheme()) { |
|
|
|
path = uri.toString() |
|
|
|
path = uri.toString() |
|
|
|
val doc = DocumentFile.fromSingleUri(appCtx, uri) |
|
|
|
val doc = DocumentFile.fromSingleUri(appCtx, uri) |
|
|
|
doc?.let { |
|
|
|
doc?.let { |
|
|
@ -55,66 +59,53 @@ object LocalBook { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
path = uri.path!! |
|
|
|
path = uri.path!! |
|
|
|
File(path).name |
|
|
|
File(path).name |
|
|
|
} |
|
|
|
}).replace(Regex("\\.txt$"), "") |
|
|
|
|
|
|
|
|
|
|
|
val name: String |
|
|
|
val name: String |
|
|
|
val author: String |
|
|
|
val author: String |
|
|
|
|
|
|
|
|
|
|
|
if (("《" in fileName && "》" in fileName) |
|
|
|
|
|
|
|
|| "作者" in fileName |
|
|
|
|
|
|
|
|| (fileName.contains(" by ", true)) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt |
|
|
|
//匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt |
|
|
|
val m1 = Pattern |
|
|
|
val m1 = Pattern |
|
|
|
.compile("《(.*?)》.*?作者:(.*?)\\.txt") |
|
|
|
.compile("(.*?)《([^《》]+)》(.*)") |
|
|
|
.matcher(fileName) |
|
|
|
.matcher(fileName) |
|
|
|
|
|
|
|
|
|
|
|
//匹配 书名 by 作者名.txt |
|
|
|
//匹配 书名 by 作者名.txt |
|
|
|
val m2 = Pattern |
|
|
|
val m2 = Pattern |
|
|
|
.compile("txt\\.(.*?) yb (.*?)$") |
|
|
|
.compile("(^)(.+) by (.+)$") |
|
|
|
.matcher(fileName.reversed()) |
|
|
|
.matcher(fileName) |
|
|
|
|
|
|
|
|
|
|
|
if (m1.find()) { |
|
|
|
|
|
|
|
name = m1.group(1) ?: fileName.replace(".txt", "") |
|
|
|
|
|
|
|
author = m1.group(2) ?: "" |
|
|
|
|
|
|
|
BookHelp.formatBookAuthor(author) |
|
|
|
|
|
|
|
} else if (m2.find()) { |
|
|
|
|
|
|
|
var temp = m2.group(2) |
|
|
|
|
|
|
|
name = if (temp==null||temp == "") { |
|
|
|
|
|
|
|
fileName.replace(".txt", "") |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
temp.reversed() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
temp = m2.group(1) ?: "" |
|
|
|
|
|
|
|
author = temp.reversed() |
|
|
|
|
|
|
|
BookHelp.formatBookAuthor(author) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val st = fileName.indexOf("《") |
|
|
|
(m1.takeIf { m1.find() } |
|
|
|
val e = fileName.indexOf("》") |
|
|
|
?: m2.takeIf { m2.find() } |
|
|
|
name = if (e > st && st != -1) { |
|
|
|
). run{ |
|
|
|
fileName.substring(st + 1, e) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
fileName.replace(".txt", "") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(this is Matcher) { |
|
|
|
|
|
|
|
|
|
|
|
val s = fileName.indexOf("作者") |
|
|
|
//按默认格式将文件名分解成书名、作者名 |
|
|
|
author = if (s != -1 && s + 2 < fileName.length) { |
|
|
|
name = group(2)!! |
|
|
|
fileName.substring(s + 2).replace(".txt", "") |
|
|
|
author = BookHelp.formatBookAuthor((group(1) ?: "") + (group(3) ?: "")) |
|
|
|
} else { |
|
|
|
|
|
|
|
"" |
|
|
|
} else if(!AppConfig.bookImportFileName.isNullOrBlank()) { |
|
|
|
} |
|
|
|
|
|
|
|
BookHelp.formatBookAuthor(author) |
|
|
|
//在脚本中定义如何分解文件名成书名、作者名 |
|
|
|
|
|
|
|
val bindings = SimpleBindings() |
|
|
|
|
|
|
|
bindings["src"] = fileName |
|
|
|
|
|
|
|
val jsonStr = AppConst.SCRIPT_ENGINE.eval( |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//在用户脚本后添加捕获author、name的代码,只要脚本中author、name有值就会被捕获,未定义则赋值为空字符串 |
|
|
|
|
|
|
|
AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})" |
|
|
|
|
|
|
|
, bindings).toString() |
|
|
|
|
|
|
|
val bookMess =GSON.fromJsonObject<HashMap<String, String>>(jsonStr) ?: HashMap() |
|
|
|
|
|
|
|
name = bookMess["name"]?: fileName |
|
|
|
|
|
|
|
author = bookMess["author"]?.takeIf { it.length != fileName.length } ?: "" |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
name = fileName.replace(".txt", "") |
|
|
|
name = fileName.replace(AppPattern.nameRegex,"") |
|
|
|
author = "" |
|
|
|
author = fileName.replace(AppPattern.authorRegex,"").takeIf { it.length != fileName.length } ?: "" |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val book = Book( |
|
|
|
val book = Book( |
|
|
|
bookUrl = path, |
|
|
|
bookUrl = path, |
|
|
|