From 1adc1db65b62ddd1930d7cc01be936f2a950a2a1 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Mon, 5 Apr 2021 20:34:49 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=AF=BC=E5=87=BAepub=E6=97=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=AF=BC=E5=87=BA=E5=B7=B2=E7=BB=8F=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=9A=84=E6=8F=92=E5=9B=BE=EF=BC=8CLocalBook=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/model/localBook/LocalBook.kt | 12 +++--- .../app/ui/book/cache/CacheViewModel.kt | 41 +++++++++++++++++-- 2 files changed, 44 insertions(+), 9 deletions(-) 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 617caa604..f23172ccd 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 @@ -75,17 +75,17 @@ object LocalBook { .matcher(fileName) if (m1.find()) { - name = m1.group(1) - author = m1.group(2) + name = m1.group(1) ?: fileName.replace(".txt", "") + author = m1.group(2) ?: "" BookHelp.formatBookAuthor(author) } else if (m2.find()) { - name = m2.group(1) - author = m2.group(2) + name = m2.group(1) ?: fileName.replace(".txt", "") + author = m2.group(2) ?: "" BookHelp.formatBookAuthor(author) } else { - val st = fileName.indexOf("《"); - val e = fileName.indexOf("》"); + val st = fileName.indexOf("《") + val e = fileName.indexOf("》") name = if (e > st && st != -1) { fileName.substring(st + 1, e) } else { 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 c84b052f2..0267e7743 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 @@ -13,6 +13,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppPattern import io.legado.app.data.appDb import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookChapter import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.help.ContentProcessor @@ -23,6 +24,7 @@ import me.ag2s.epublib.epub.EpubWriter import me.ag2s.epublib.util.ResourceUtil import java.io.ByteArrayOutputStream import java.io.File +import java.io.FileInputStream import java.io.FileOutputStream import java.nio.charset.Charset @@ -205,7 +207,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { //set css epubBook.resources.add( Resource( - "body{background:white;margin:0;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}".encodeToByteArray(), + "body{background:white;margin:0;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{width:100%;height:100%;max-width: 100%;max-height:100%;}".encodeToByteArray(), "css/style.css" ) ) @@ -222,6 +224,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { resource.compress(Bitmap.CompressFormat.JPEG, 100, stream) val byteArray: ByteArray = stream.toByteArray() resource.recycle() + stream.close() epubBook.coverImage = Resource(byteArray, "cover.jpg") } @@ -232,13 +235,15 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { }) } + private fun setEpubContent(book: Book, epubBook: EpubBook) { val useReplace = AppConfig.exportUseReplace val contentProcessor = ContentProcessor(book.name, book.origin) appDb.bookChapterDao.getChapterList(book.bookUrl).forEach { chapter -> BookHelp.getContent(book, chapter).let { content -> - val content1 = contentProcessor - .getContent(book, chapter.title, content ?: "null", false, useReplace) + var content1 = fixPic(epubBook, book, content ?: "null", chapter) + content1 = contentProcessor + .getContent(book, chapter.title, content1, false, useReplace) .joinToString("\n") .replace(chapter.title, "") @@ -250,6 +255,36 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { } } + private fun setPic(src: String, book: Book, epubBook: EpubBook) { + val vFile = BookHelp.getImage(book, src) + if (vFile.exists()) { + val img = Resource(FileInputStream(vFile), MD5Utils.md5Encode16(src) + ".jpg") + epubBook.resources.add(img) + } + } + + private fun fixPic( + epubBook: EpubBook, + book: Book, + content: String, + chapter: BookChapter + ): String { + val data = StringBuilder("") + content.split("\n").forEach { text -> + var text1 = text + val matcher = AppPattern.imgPattern.matcher(text) + if (matcher.find()) { + matcher.group(1)?.let { + val src = NetworkUtils.getAbsoluteURL(chapter.url, it) + setPic(src, book, epubBook) + text1 = text.replace(src, MD5Utils.md5Encode16(src) + ".jpg") + } + } + data.append(text1).append("\n") + } + return data.toString() + } + private fun setEpubMetadata(book: Book, epubBook: EpubBook) { val metadata = Metadata() metadata.titles.add(book.name)//书籍的名称 From 296f839f7c30473a8e554dbc3fd1dc2d241f816a Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Tue, 6 Apr 2021 06:07:31 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20by=20=E8=A7=84?= =?UTF-8?q?=E5=88=99bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/model/localBook/LocalBook.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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 f23172ccd..a01efaee0 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 @@ -65,22 +65,29 @@ object LocalBook { || (fileName.contains(" by ", true)) ) { + //匹配(知轩藏书常用格式) 《书名》其它信息作者:作者名.txt val m1 = Pattern - .compile("《(.*?)》.*?作者:(.*?).txt") + .compile("《(.*?)》.*?作者:(.*?)\\.txt") .matcher(fileName) //匹配 书名 by 作者名.txt val m2 = Pattern - .compile("(.*?) by (.*?).txt") - .matcher(fileName) + .compile("txt\\.(.*?) yb (.*?)$") + .matcher(fileName.reversed()) if (m1.find()) { name = m1.group(1) ?: fileName.replace(".txt", "") author = m1.group(2) ?: "" BookHelp.formatBookAuthor(author) } else if (m2.find()) { - name = m2.group(1) ?: fileName.replace(".txt", "") - author = m2.group(2) ?: "" + var temp = m2.group(2) + name = if (temp==null||temp == "") { + fileName.replace(".txt", "") + } else { + temp.reversed() + } + temp = m2.group(1) ?: "" + author = temp.reversed() BookHelp.formatBookAuthor(author) } else { @@ -89,7 +96,7 @@ object LocalBook { name = if (e > st && st != -1) { fileName.substring(st + 1, e) } else { - fileName + fileName.replace(".txt", "") } From eee6570f3233cffeafda5f656899012fcb4d0820 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Tue, 6 Apr 2021 09:25:02 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/cache/CacheViewModel.kt | 2 +- .../java/me/ag2s/epublib/util/ResourceUtil.java | 10 ++++++++-- .../java/me/ag2s/epublib/util/StringUtil.java | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 3 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 0267e7743..3620ed00c 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 @@ -207,7 +207,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { //set css epubBook.resources.add( Resource( - "body{background:white;margin:0;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{width:100%;height:100%;max-width: 100%;max-height:100%;}".encodeToByteArray(), + "body,div{background:white;margin:0 auto;}padding:0;outline:none;width:100%;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{display:inline-block;width:100%;height:100%;max-width: 100%;max-height:100%;}".encodeToByteArray(), "css/style.css" ) ) 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 eccf1d657..b9425b4c8 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java @@ -60,9 +60,15 @@ public class ResourceUtil { private static String createHtml(String title, String txt) { StringBuilder body = new StringBuilder(); for (String s : txt.split("\\r?\\n")) { - s = s.trim(); + s=StringUtil.FixTrim(s); if (s.length() != 0) { - body.append("

").append(s).append("

"); + if(s.contains("") + body.append(s);//.append(""); + }else { + body.append("

").append(s).append("

"); + } + } } diff --git a/epublib/src/main/java/me/ag2s/epublib/util/StringUtil.java b/epublib/src/main/java/me/ag2s/epublib/util/StringUtil.java index 677d6a157..b24a283dd 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/StringUtil.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/StringUtil.java @@ -3,6 +3,8 @@ package me.ag2s.epublib.util; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Various String utility functions. @@ -271,4 +273,18 @@ public class StringUtil { } return text.substring(cPos + 1); } + public static String FixTrim(String s) { + if (s==null){ + return "null"; + } + Pattern r = Pattern.compile("^[\\s]{1,9}(.*?)[\\s]{1,9}$"); + Matcher m = r.matcher(s); + if (m.find()) { + s= m.group(1); + } + //移除GBK中文全角空格 + s = s.replace(" ", ""); + return s; + } + } From cc4fa964d47e27e3a8a6b5041e301df7805cf424 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Tue, 6 Apr 2021 09:26:16 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E6=8E=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3620ed00c..bd8471efc 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 @@ -207,7 +207,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { //set css epubBook.resources.add( Resource( - "body,div{background:white;margin:0 auto;}padding:0;outline:none;width:100%;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{display:inline-block;width:100%;height:100%;max-width: 100%;max-height:100%;}".encodeToByteArray(), + "body,div{background:white;margin:0 auto;padding:0;outline:none;width:100%;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{display:inline-block;width:100%;height:100%;max-width: 100%;max-height:100%;}".encodeToByteArray(), "css/style.css" ) ) From 810e48b8c3965cca050698a451c06473717d28df Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Wed, 7 Apr 2021 00:25:55 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8DfixPic=E4=B8=80=E6=AE=B5?= =?UTF-8?q?=E9=87=8C=E6=9C=89=E5=A4=9A=E4=B8=AAimg=E5=8F=AA=E4=BC=9A?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=AC=AC=E4=B8=80=E4=B8=AAimg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/cache/CacheViewModel.kt | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 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 bd8471efc..7c6f2b37e 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 @@ -127,10 +127,10 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { appDb.bookChapterDao.getChapterList(book.bookUrl).forEach { chapter -> BookHelp.getContent(book, chapter)?.let { content -> content.split("\n").forEachIndexed { index, text -> - val matcher = AppPattern.imgPattern.matcher(text) - if (matcher.find()) { - matcher.group(1)?.let { - val src = NetworkUtils.getAbsoluteURL(chapter.url, it) + val matches = AppPattern.imgPattern.toRegex().findAll(input = text) + matches.forEach { matchResult -> + matchResult.groupValues[1].let { + val src=NetworkUtils.getAbsoluteURL(chapter.url, it) srcList.add(Triple(chapter.title, index, src)) } } @@ -272,14 +272,16 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { val data = StringBuilder("") content.split("\n").forEach { text -> var text1 = text - val matcher = AppPattern.imgPattern.matcher(text) - if (matcher.find()) { - matcher.group(1)?.let { - val src = NetworkUtils.getAbsoluteURL(chapter.url, it) + val matches = AppPattern.imgPattern.toRegex().findAll(input = text) + matches.forEach { matchResult -> + matchResult.groupValues[1].let { + val src=NetworkUtils.getAbsoluteURL(chapter.url, it) setPic(src, book, epubBook) - text1 = text.replace(src, MD5Utils.md5Encode16(src) + ".jpg") + text1 = text1.replace(src, MD5Utils.md5Encode16(src) + ".jpg") + } } + data.append(text1).append("\n") } return data.toString() From f8d71f80092ed370a1662cc0da89f6cbfc3d7137 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Wed, 7 Apr 2021 00:31:16 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8DfixPic=E4=B8=80=E6=AE=B5?= =?UTF-8?q?=E9=87=8C=E6=9C=89=E5=A4=9A=E4=B8=AAimg=E5=8F=AA=E4=BC=9A?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=AC=AC=E4=B8=80=E4=B8=AAimg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/ag2s/epublib/util/ResourceUtil.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) 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 b9425b4c8..7c021fae1 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java @@ -29,7 +29,8 @@ import me.ag2s.epublib.epub.EpubProcessorSupport; public class ResourceUtil { /** * 快速创建HTML类型的Resource - * @param title 章节的标题 + * + * @param title 章节的标题 * @param string 章节的正文 * @return 返回Resource */ @@ -42,9 +43,10 @@ public class ResourceUtil { /** * 快速创建HTML类型的Resource - * @param title 章节的标题 + * + * @param title 章节的标题 * @param string 章节的正文 - * @param href Resource的href + * @param href Resource的href * @return 返回Resource */ @@ -60,12 +62,14 @@ public class ResourceUtil { private static String createHtml(String title, String txt) { StringBuilder body = new StringBuilder(); for (String s : txt.split("\\r?\\n")) { - s=StringUtil.FixTrim(s); + s = StringUtil.FixTrim(s); if (s.length() != 0) { - if(s.contains("") - body.append(s);//.append(""); - }else { + body.append(s); + //.append(""); + } else { body.append("

").append(s).append("

"); } @@ -86,6 +90,7 @@ public class ResourceUtil { /** * 快速从File创建Resource + * * @param file File * @return Resource * @throws IOException IOException From 1a56e0a846adaf113d4cb371c76efce689d7d175 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Wed, 7 Apr 2021 00:33:30 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8DfixPic=E4=B8=80=E6=AE=B5?= =?UTF-8?q?=E9=87=8C=E6=9C=89=E5=A4=9A=E4=B8=AAimg=E5=8F=AA=E4=BC=9A?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=AC=AC=E4=B8=80=E4=B8=AAimg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java | 1 + 1 file changed, 1 insertion(+) 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 7c021fae1..d03018e26 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java @@ -62,6 +62,7 @@ public class ResourceUtil { private static String createHtml(String title, String txt) { StringBuilder body = new StringBuilder(); for (String s : txt.split("\\r?\\n")) { + //移除多余的开头结尾的空白字符,节省epub的体积 s = StringUtil.FixTrim(s); if (s.length() != 0) { if (s.contains(" Date: Wed, 7 Apr 2021 00:48:41 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E5=AF=BC=E5=87=BAepub=E6=97=B6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=AE=9E=E9=99=85=E6=98=BE=E7=A4=BA=E7=9A=84=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E4=BB=A3=E6=9B=BF=E4=B9=A6=E6=BA=90=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=9A=84=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/ui/book/cache/CacheViewModel.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 7c6f2b37e..b40fb07cd 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 @@ -217,7 +217,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { Glide.with(context) .asBitmap() - .load(book.coverUrl) + .load(book.getDisplayCover()) .into(object : CustomTarget() { override fun onResourceReady(resource: Bitmap, transition: Transition?) { val stream = ByteArrayOutputStream() @@ -290,7 +290,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { private fun setEpubMetadata(book: Book, epubBook: EpubBook) { val metadata = Metadata() metadata.titles.add(book.name)//书籍的名称 - metadata.authors.add(Author(book.author))//书籍的作者 + metadata.authors.add(Author(book.getRealAuthor()))//书籍的作者 metadata.language = "zh"//数据的语言 metadata.dates.add(Date())//数据的创建日期 metadata.publishers.add("Legado APP")//数据的创建者