|
|
@ -1,13 +1,14 @@ |
|
|
|
package io.legado.app.model.localBook |
|
|
|
package io.legado.app.model.localBook |
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
import android.content.Context |
|
|
|
|
|
|
|
import android.net.Uri |
|
|
|
import android.text.TextUtils |
|
|
|
import android.text.TextUtils |
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
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.Book |
|
|
|
import nl.siegmann.epublib.domain.TOCReference |
|
|
|
import nl.siegmann.epublib.domain.TOCReference |
|
|
|
import nl.siegmann.epublib.epub.EpubReader |
|
|
|
import nl.siegmann.epublib.epub.EpubReader |
|
|
|
import nl.siegmann.epublib.service.MediatypeService |
|
|
|
|
|
|
|
import org.jsoup.Jsoup |
|
|
|
import org.jsoup.Jsoup |
|
|
|
import java.io.File |
|
|
|
import java.io.File |
|
|
|
import java.io.IOException |
|
|
|
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 |
|
|
|
private lateinit var mCharset: Charset |
|
|
|
|
|
|
|
|
|
|
|
init { |
|
|
|
init { |
|
|
|
|
|
|
|
try { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun readBook(file: File?): Book? { |
|
|
|
|
|
|
|
return try { |
|
|
|
|
|
|
|
val epubReader = EpubReader() |
|
|
|
val epubReader = EpubReader() |
|
|
|
val lazyTypes = |
|
|
|
val inputStream = if (book.bookUrl.isContentPath()) { |
|
|
|
arrayOf( |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
MediatypeService.CSS, |
|
|
|
context.contentResolver.openInputStream(uri) |
|
|
|
MediatypeService.GIF, |
|
|
|
} else { |
|
|
|
MediatypeService.JPG, |
|
|
|
File(book.bookUrl).inputStream() |
|
|
|
MediatypeService.PNG, |
|
|
|
} |
|
|
|
MediatypeService.MP3, |
|
|
|
epubBook = epubReader.readEpub(inputStream) |
|
|
|
MediatypeService.MP4 |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
val zipFile = ZipFile(file) |
|
|
|
|
|
|
|
epubReader.readEpubLazy(zipFile, "utf-8", Arrays.asList(*lazyTypes)) |
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
} 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<BookChapter> { |
|
|
|
fun getChapterList(epubBook: Book): ArrayList<BookChapter> { |
|
|
|
val metadata = epubBook.metadata |
|
|
|
val metadata = epubBook.metadata |
|
|
|
book.name = metadata.firstTitle |
|
|
|
book.name = metadata.firstTitle |
|
|
@ -113,7 +129,7 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { |
|
|
|
chapter.url = ref.completeHref |
|
|
|
chapter.url = ref.completeHref |
|
|
|
chapterList.add(chapter) |
|
|
|
chapterList.add(chapter) |
|
|
|
} |
|
|
|
} |
|
|
|
if (ref.children != null && !ref.children.isEmpty()) { |
|
|
|
if (ref.children != null && ref.children.isNotEmpty()) { |
|
|
|
parseMenu(chapterList, ref.children, level + 1) |
|
|
|
parseMenu(chapterList, ref.children, level + 1) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|