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. 77
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  3. 36
      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 = "书籍导入异常" book.intro = "书籍导入异常"
} else { } else {
val metadata = epubBook!!.metadata 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) { if (metadata.authors.size > 0) {
val author = val author =
metadata.authors[0].toString().replace("^, |, $".toRegex(), "") metadata.authors[0].toString().replace("^, |, $".toRegex(), "")

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

@ -170,12 +170,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
setCover(book, epubBook) setCover(book, epubBook)
//set css //set css
epubBook.resources.add( setCSS(epubBook)
Resource(
"h1 {color: blue;}p {text-indent:2em;}".encodeToByteArray(),
"css/style.css"
)
)
//设置正文 //设置正文
setEpubContent(book, epubBook) setEpubContent(book, epubBook)
@ -195,32 +190,39 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
setEpubMetadata(book, epubBook) setEpubMetadata(book, epubBook)
//set cover //set cover
setCover(book, epubBook) 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 //set css
epubBook.resources.add( epubBook.resources.add(
Resource( 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" "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) { private fun setCover(book: Book, epubBook: EpubBook) {
Glide.with(context) Glide.with(context)
.asBitmap() .asBitmap()
.load(book.coverUrl) .load(book.coverUrl)
.into(object : CustomTarget<Bitmap>(){ .into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) { override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val stream = ByteArrayOutputStream() val stream = ByteArrayOutputStream()
resource.compress(Bitmap.CompressFormat.JPEG, 100, stream) resource.compress(Bitmap.CompressFormat.JPEG, 100, stream)
val byteArray: ByteArray = stream.toByteArray() val byteArray: ByteArray = stream.toByteArray()
resource.recycle() resource.recycle()
epubBook.coverImage= Resource(byteArray,"cover.jpg") epubBook.coverImage = Resource(byteArray, "cover.jpg")
} }
override fun onLoadCleared(placeholder: Drawable?) { override fun onLoadCleared(placeholder: Drawable?) {
@ -238,7 +240,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
val content1 = contentProcessor val content1 = contentProcessor
.getContent(book, chapter.title, content ?: "null", false, useReplace) .getContent(book, chapter.title, content ?: "null", false, useReplace)
.joinToString("\n") .joinToString("\n")
.replace(chapter.title,"") .replace(chapter.title, "")
epubBook.addSection( epubBook.addSection(
chapter.title, chapter.title,
@ -248,7 +250,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
} }
} }
private fun setEpubMetadata(book: Book,epubBook: EpubBook) { private fun setEpubMetadata(book: Book, epubBook: EpubBook) {
val metadata = Metadata() val metadata = Metadata()
metadata.titles.add(book.name)//书籍的名称 metadata.titles.add(book.name)//书籍的名称
metadata.authors.add(Author(book.author))//书籍的作者 metadata.authors.add(Author(book.author))//书籍的作者
@ -257,7 +259,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
metadata.publishers.add("Legado APP")//数据的创建者 metadata.publishers.add("Legado APP")//数据的创建者
metadata.descriptions.add(book.getDisplayIntro())//书籍的简介 metadata.descriptions.add(book.getDisplayIntro())//书籍的简介
//metadata.subjects.add("")//书籍的主题,在静读天下里面有使用这个分类书籍 //metadata.subjects.add("")//书籍的主题,在静读天下里面有使用这个分类书籍
epubBook.metadata=metadata epubBook.metadata = metadata
} }
//////end of EPUB //////end of EPUB

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

Loading…
Cancel
Save