From 8f0cd89322d0337656c364ab1e8ef0de4ddbc8ba Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Mon, 5 Apr 2021 06:17:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9epub=E9=BB=98=E8=AE=A4css?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=AF=BC=E5=87=BA=E7=9A=84epub?= =?UTF-8?q?=E7=9A=84=E6=A0=87=E9=A2=98=E6=96=87=E5=AD=97=E8=BF=87=E5=A4=A7?= =?UTF-8?q?=E7=9A=84bug=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/cache/CacheViewModel.kt | 36 ++++++++++--------- .../me/ag2s/epublib/util/ResourceUtil.java | 6 ++-- 2 files changed, 22 insertions(+), 20 deletions(-) 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") From c7bf353bf127baaf2955e8b0e50e4964076fd20e Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Mon, 5 Apr 2021 08:28:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8DlocalBook=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=B9=A6=E5=90=8D=E4=BD=9C=E8=80=85=E5=90=8D=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=88epub=E5=86=8D=E8=BF=99=E9=87=8C?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=98=AF=E5=A4=9A=E4=BD=99=E7=9A=84=EF=BC=89?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8EpubFile=E4=B8=AD=E4=BC=98=E5=85=88=E4=BB=8E?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=AD=E8=AF=BB=E5=8F=96=E4=B9=A6=E5=90=8D?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E6=9E=9C=E8=8E=B7=E5=8F=96=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E4=BA=86=E5=86=8D=E4=BD=BF=E7=94=A8=E6=96=87=E4=BB=B6=E5=90=8D?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/model/localBook/EpubFile.kt | 6 +- .../legado/app/model/localBook/LocalBook.kt | 77 +++++++++++++------ 2 files changed, 57 insertions(+), 26 deletions(-) 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,