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..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,31 +65,38 @@ 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) - 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) + 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 { - 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 { - fileName + fileName.replace(".txt", "") } 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..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 @@ -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 @@ -125,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)) } } @@ -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,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" ) ) @@ -215,13 +217,14 @@ 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() 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,10 +255,42 @@ 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 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 = text1.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)//书籍的名称 - metadata.authors.add(Author(book.author))//书籍的作者 + metadata.authors.add(Author(book.getRealAuthor()))//书籍的作者 metadata.language = "zh"//数据的语言 metadata.dates.add(Date())//数据的创建日期 metadata.publishers.add("Legado APP")//数据的创建者 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..d03018e26 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,9 +62,18 @@ 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(); + //移除多余的开头结尾的空白字符,节省epub的体积 + 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("

"); + } + } } @@ -80,6 +91,7 @@ public class ResourceUtil { /** * 快速从File创建Resource + * * @param file File * @return Resource * @throws IOException IOException 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; + } + }