|
|
@ -16,12 +16,17 @@ import java.io.File |
|
|
|
import java.io.FileInputStream |
|
|
|
import java.io.FileInputStream |
|
|
|
import java.io.FileNotFoundException |
|
|
|
import java.io.FileNotFoundException |
|
|
|
import java.io.InputStream |
|
|
|
import java.io.InputStream |
|
|
|
import java.util.regex.Matcher |
|
|
|
|
|
|
|
import java.util.regex.Pattern |
|
|
|
import java.util.regex.Pattern |
|
|
|
import javax.script.SimpleBindings |
|
|
|
import javax.script.SimpleBindings |
|
|
|
|
|
|
|
|
|
|
|
object LocalBook { |
|
|
|
object LocalBook { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val nameAuthorPatterns = arrayOf( |
|
|
|
|
|
|
|
Pattern.compile("(.*?)《([^《》]+)》(.*)"), |
|
|
|
|
|
|
|
Pattern.compile("(^)(.+) 作者:(.+)$"), |
|
|
|
|
|
|
|
Pattern.compile("(^)(.+) by (.+)$") |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
@Throws(FileNotFoundException::class, SecurityException::class) |
|
|
|
@Throws(FileNotFoundException::class, SecurityException::class) |
|
|
|
fun getBookInputStream(book: Book): InputStream { |
|
|
|
fun getBookInputStream(book: Book): InputStream { |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
@ -117,44 +122,37 @@ object LocalBook { |
|
|
|
val tempFileName = fileName.substringBeforeLast(".") |
|
|
|
val tempFileName = fileName.substringBeforeLast(".") |
|
|
|
var name: String |
|
|
|
var name: String |
|
|
|
var author: String |
|
|
|
var author: String |
|
|
|
//匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt |
|
|
|
for (pattern in nameAuthorPatterns) { |
|
|
|
val m1 = Pattern |
|
|
|
pattern.matcher(tempFileName).takeIf { it.find() }?.run { |
|
|
|
.compile("(.*?)《([^《》]+)》(.*)") |
|
|
|
|
|
|
|
.matcher(tempFileName) |
|
|
|
|
|
|
|
//匹配 书名 作者:作者名.txt |
|
|
|
|
|
|
|
val m2 = Pattern |
|
|
|
|
|
|
|
.compile("(^)(.+) 作者:(.+)$") |
|
|
|
|
|
|
|
.matcher(tempFileName) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(m1.takeIf { m1.find() } ?: m2.takeIf { m2.find() }).run { |
|
|
|
|
|
|
|
if (this is Matcher) { |
|
|
|
|
|
|
|
//按默认格式将文件名分解成书名、作者名 |
|
|
|
|
|
|
|
name = group(2)!! |
|
|
|
name = group(2)!! |
|
|
|
author = BookHelp.formatBookAuthor((group(1) ?: "") + (group(3) ?: "")) |
|
|
|
val group1 = group(1) ?: "" |
|
|
|
} else if (!AppConfig.bookImportFileName.isNullOrBlank()) { |
|
|
|
val group3 = group(3) ?: "" |
|
|
|
try { |
|
|
|
author = BookHelp.formatBookAuthor(group1 + group3) |
|
|
|
//在脚本中定义如何分解文件名成书名、作者名 |
|
|
|
return Pair(name, author) |
|
|
|
val jsonStr = AppConst.SCRIPT_ENGINE.eval( |
|
|
|
} |
|
|
|
//在用户脚本后添加捕获author、name的代码,只要脚本中author、name有值就会被捕获 |
|
|
|
} |
|
|
|
AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})", |
|
|
|
if (!AppConfig.bookImportFileName.isNullOrBlank()) { |
|
|
|
//将文件名注入到脚步的src变量中 |
|
|
|
try { |
|
|
|
SimpleBindings().also { it["src"] = tempFileName } |
|
|
|
//在脚本中定义如何分解文件名成书名、作者名 |
|
|
|
).toString() |
|
|
|
val jsonStr = AppConst.SCRIPT_ENGINE.eval( |
|
|
|
val bookMess = GSON.fromJsonObject<HashMap<String, String>>(jsonStr) |
|
|
|
//在用户脚本后添加捕获author、name的代码,只要脚本中author、name有值就会被捕获 |
|
|
|
.getOrThrow() ?: HashMap() |
|
|
|
AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})", |
|
|
|
name = bookMess["name"] ?: tempFileName |
|
|
|
//将文件名注入到脚步的src变量中 |
|
|
|
author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: "" |
|
|
|
SimpleBindings().also { it["src"] = tempFileName } |
|
|
|
} catch (e: Exception) { |
|
|
|
).toString() |
|
|
|
name = BookHelp.formatBookName(tempFileName) |
|
|
|
val bookMess = GSON.fromJsonObject<HashMap<String, String>>(jsonStr) |
|
|
|
author = BookHelp.formatBookAuthor(tempFileName.replace(name, "")) |
|
|
|
.getOrThrow() ?: HashMap() |
|
|
|
.takeIf { it.length != tempFileName.length } ?: "" |
|
|
|
name = bookMess["name"] ?: tempFileName |
|
|
|
} |
|
|
|
author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: "" |
|
|
|
} else { |
|
|
|
} catch (e: Exception) { |
|
|
|
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 } ?: "" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
name = BookHelp.formatBookName(tempFileName) |
|
|
|
|
|
|
|
author = BookHelp.formatBookAuthor(tempFileName.replace(name, "")) |
|
|
|
|
|
|
|
.takeIf { it.length != tempFileName.length } ?: "" |
|
|
|
} |
|
|
|
} |
|
|
|
return Pair(name, author) |
|
|
|
return Pair(name, author) |
|
|
|
} |
|
|
|
} |
|
|
|