From 62a649052ca2e0e43fef39e22f06a27b99dad0e4 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 9 Jul 2020 21:29:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E8=AF=B4=E9=98=85=E8=AF=BB=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=20=E4=BC=98=E5=8C=96=20=E6=B7=BB=E5=8A=A0=20epub=20?= =?UTF-8?q?=E6=9C=AA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/model/localBook/EPUBFile.kt | 56 ++++++++++++------- .../main/res/layout/dialog_change_source.xml | 3 +- 2 files changed, 38 insertions(+), 21 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 8706b8ad6..33986f094 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 @@ -1,13 +1,14 @@ package io.legado.app.model.localBook import android.content.Context +import android.net.Uri import android.text.TextUtils import io.legado.app.data.entities.BookChapter -import net.sf.jazzlib.ZipFile +import io.legado.app.utils.htmlFormat +import io.legado.app.utils.isContentPath import nl.siegmann.epublib.domain.Book import nl.siegmann.epublib.domain.TOCReference import nl.siegmann.epublib.epub.EpubReader -import nl.siegmann.epublib.service.MediatypeService import org.jsoup.Jsoup import java.io.File import java.io.IOException @@ -19,28 +20,43 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { private lateinit var mCharset: Charset init { - - } - - fun readBook(file: File?): Book? { - return try { + try { val epubReader = EpubReader() - val lazyTypes = - arrayOf( - MediatypeService.CSS, - MediatypeService.GIF, - MediatypeService.JPG, - MediatypeService.PNG, - MediatypeService.MP3, - MediatypeService.MP4 - ) - val zipFile = ZipFile(file) - epubReader.readEpubLazy(zipFile, "utf-8", Arrays.asList(*lazyTypes)) + val inputStream = if (book.bookUrl.isContentPath()) { + val uri = Uri.parse(book.bookUrl) + context.contentResolver.openInputStream(uri) + } else { + File(book.bookUrl).inputStream() + } + epubBook = epubReader.readEpub(inputStream) } catch (e: Exception) { - null } } + fun getContent(chapter: BookChapter): String { + val resource = epubBook!!.resources.getByHref(chapter.url) + val content = StringBuilder() + val doc = Jsoup.parse(String(resource.data, mCharset)) + val elements = doc.allElements + for (element in elements) { + val contentEs = element.textNodes() + for (i in contentEs.indices) { + val text = contentEs[i].text().trim { it <= ' ' }.htmlFormat() + if (elements.size > 1) { + if (text.isNotEmpty()) { + if (content.isNotEmpty()) { + content.append("\r\n") + } + content.append("\u3000\u3000").append(text) + } + } else { + content.append(text) + } + } + } + return content.toString() + } + fun getChapterList(epubBook: Book): ArrayList { val metadata = epubBook.metadata book.name = metadata.firstTitle @@ -113,7 +129,7 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { chapter.url = ref.completeHref chapterList.add(chapter) } - if (ref.children != null && !ref.children.isEmpty()) { + if (ref.children != null && ref.children.isNotEmpty()) { parseMenu(chapterList, ref.children, level + 1) } } diff --git a/app/src/main/res/layout/dialog_change_source.xml b/app/src/main/res/layout/dialog_change_source.xml index cc7486b05..4b8703b37 100644 --- a/app/src/main/res/layout/dialog_change_source.xml +++ b/app/src/main/res/layout/dialog_change_source.xml @@ -3,7 +3,8 @@ xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:background="@color/background">