diff --git a/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt b/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt index 2b50a9e61..d076187f3 100644 --- a/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt @@ -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(), "") diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 142571054..617caa604 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -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 = "" - } else { - name = str.substring(0, authorIndex) - author = str.substring(authorIndex) - author = BookHelp.formatBookAuthor(author) - } - val smhStart = name.indexOf("《") - val smhEnd = name.indexOf("》") - if (smhStart != -1 && smhEnd != -1) { - name = (name.substring(smhStart + 1, smhEnd)) - } - 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() + + 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 { + 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( bookUrl = path, name = name, diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index ef0cc2aea..c84b052f2 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -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,32 +190,39 @@ 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) .asBitmap() .load(book.coverUrl) - .into(object : CustomTarget(){ + .into(object : CustomTarget() { override fun onResourceReady(resource: Bitmap, transition: Transition?) { val stream = ByteArrayOutputStream() resource.compress(Bitmap.CompressFormat.JPEG, 100, stream) val byteArray: ByteArray = stream.toByteArray() resource.recycle() - epubBook.coverImage= Resource(byteArray,"cover.jpg") + epubBook.coverImage = Resource(byteArray, "cover.jpg") } override fun onLoadCleared(placeholder: Drawable?) { @@ -238,7 +240,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { val content1 = contentProcessor .getContent(book, chapter.title, content ?: "null", false, useReplace) .joinToString("\n") - .replace(chapter.title,"") + .replace(chapter.title, "") epubBook.addSection( 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() metadata.titles.add(book.name)//书籍的名称 metadata.authors.add(Author(book.author))//书籍的作者 @@ -257,7 +259,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { metadata.publishers.add("Legado APP")//数据的创建者 metadata.descriptions.add(book.getDisplayIntro())//书籍的简介 //metadata.subjects.add("")//书籍的主题,在静读天下里面有使用这个分类书籍 - epubBook.metadata=metadata + epubBook.metadata = metadata } //////end of EPUB diff --git a/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java b/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java index 96212f253..eccf1d657 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java @@ -71,7 +71,7 @@ public class ResourceUtil { html += "" + title + "" + "" + ""; - html += "

" + title + "

" + + html += "

" + title + "

" + body + ""; @@ -81,8 +81,8 @@ public class ResourceUtil { /** * 快速从File创建Resource * @param file File - * @return - * @throws IOException + * @return Resource + * @throws IOException IOException */ @SuppressWarnings("unused")