Merge pull request #933 from ag2s20150909/master

修复导出epub的标题样式过大&&优化导入本地文件识别
pull/940/head
kunfei 4 years ago committed by GitHub
commit 547fd17ac3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/src/main/java/io/legado/app/model/localBook/EpubFile.kt
  2. 71
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  3. 26
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  4. 6
      epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java

@ -196,7 +196,11 @@ class EpubFile(var book: Book) {
book.intro = "书籍导入异常"
} else {
val metadata = epubBook!!.metadata
book.name = book.originName
book.name = metadata.firstTitle
if (book.name.isEmpty()) {
book.name = book.originName.replace(".epub", "")
}
if (metadata.authors.size > 0) {
val author =
metadata.authors[0].toString().replace("^, |, $".toRegex(), "")

@ -9,6 +9,7 @@ import io.legado.app.help.BookHelp
import io.legado.app.utils.*
import splitties.init.appCtx
import java.io.File
import java.util.regex.Pattern
object LocalBook {
@ -55,33 +56,59 @@ object LocalBook {
path = uri.path!!
File(path).name
}
val str = fileName.substringBeforeLast(".")
val authorIndex = str.indexOf("作者")
var name: String
var author: String
if (authorIndex == -1) {
name = str
author = ""
val name: String
val author: String
if (("" in fileName && "" in fileName)
|| "作者" in fileName
|| (fileName.contains(" by ", true))
) {
//匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt
val m1 = Pattern
.compile("《(.*?)》.*?作者:(.*?).txt")
.matcher(fileName)
//匹配 书名 by 作者名.txt
val m2 = Pattern
.compile("(.*?) by (.*?).txt")
.matcher(fileName)
if (m1.find()) {
name = m1.group(1)
author = m1.group(2)
BookHelp.formatBookAuthor(author)
} else if (m2.find()) {
name = m2.group(1)
author = m2.group(2)
BookHelp.formatBookAuthor(author)
} else {
val st = fileName.indexOf("");
val e = fileName.indexOf("");
name = if (e > st && st != -1) {
fileName.substring(st + 1, e)
} else {
name = str.substring(0, authorIndex)
author = str.substring(authorIndex)
author = BookHelp.formatBookAuthor(author)
fileName
}
val smhStart = name.indexOf("")
val smhEnd = name.indexOf("")
if (smhStart != -1 && smhEnd != -1) {
name = (name.substring(smhStart + 1, smhEnd))
val s = fileName.indexOf("作者")
author = if (s != -1 && s + 2 < fileName.length) {
fileName.substring(s + 2).replace(".txt", "")
} else {
""
}
if (author == "" && fileName.contains(" by ")) {
val rstr = fileName.reversed()
// find last ' by ' near '.txt' or '.epub' using reversed string
val pattern = """^(txt|bupe)\.(.*) yb (.*)$""".toRegex()
val matches = pattern.findAll(input = rstr)
matches.forEach { matchResult ->
name = matchResult.groupValues[3].reversed()
author = matchResult.groupValues[2].reversed()
BookHelp.formatBookAuthor(author)
}
} else {
name = fileName.replace(".txt", "")
author = ""
}
val book = Book(
bookUrl = path,
name = name,

@ -170,12 +170,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
setCover(book, epubBook)
//set css
epubBook.resources.add(
Resource(
"h1 {color: blue;}p {text-indent:2em;}".encodeToByteArray(),
"css/style.css"
)
)
setCSS(epubBook)
//设置正文
setEpubContent(book, epubBook)
@ -195,20 +190,27 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
setEpubMetadata(book, epubBook)
//set cover
setCover(book, epubBook)
//set css
setCSS(epubBook)
val bookPath = FileUtils.getPath(file, filename)
val bookFile = FileUtils.createFileWithReplace(bookPath)
//设置正文
setEpubContent(book, epubBook)
EpubWriter().write(epubBook, FileOutputStream(bookFile))
}
private fun setCSS(epubBook: EpubBook) {
//set css
epubBook.resources.add(
Resource(
"h1 {color: blue;}p {text-indent:2em;}".encodeToByteArray(),
"body{background:white;margin:0;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}".encodeToByteArray(),
"css/style.css"
)
)
val bookPath = FileUtils.getPath(file, filename)
val bookFile = FileUtils.createFileWithReplace(bookPath)
//设置正文
setEpubContent(book, epubBook)
EpubWriter().write(epubBook, FileOutputStream(bookFile))
}
private fun setCover(book: Book, epubBook: EpubBook) {
Glide.with(context)

@ -71,7 +71,7 @@ public class ResourceUtil {
html += "<head><title>" + title + "</title>" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"css/style.css\"/>" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head>";
html += "<body><h1>" + title + "</h1>" +
html += "<body><h2>" + title + "</h2>" +
body +
"</body></html>";
@ -81,8 +81,8 @@ public class ResourceUtil {
/**
* 快速从File创建Resource
* @param file File
* @return
* @throws IOException
* @return Resource
* @throws IOException IOException
*/
@SuppressWarnings("unused")

Loading…
Cancel
Save