|
|
@ -1,8 +1,8 @@ |
|
|
|
package io.legado.app.model.localBook |
|
|
|
package io.legado.app.model.localBook |
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
|
|
|
|
import android.net.Uri |
|
|
|
import android.net.Uri |
|
|
|
import android.text.TextUtils |
|
|
|
import android.text.TextUtils |
|
|
|
|
|
|
|
import io.legado.app.App |
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
import io.legado.app.utils.htmlFormat |
|
|
|
import io.legado.app.utils.htmlFormat |
|
|
|
import io.legado.app.utils.isContentPath |
|
|
|
import io.legado.app.utils.isContentPath |
|
|
@ -15,8 +15,30 @@ import java.io.IOException |
|
|
|
import java.nio.charset.Charset |
|
|
|
import java.nio.charset.Charset |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
|
|
class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { |
|
|
|
class EPUBFile(val book: io.legado.app.data.entities.Book) { |
|
|
|
var epubBook: Book? = null |
|
|
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
|
|
|
private var eFile: EPUBFile? = null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Synchronized |
|
|
|
|
|
|
|
fun getEFile(book: io.legado.app.data.entities.Book): EPUBFile { |
|
|
|
|
|
|
|
if (eFile == null || eFile?.book?.bookUrl == book.bookUrl) { |
|
|
|
|
|
|
|
eFile = EPUBFile(book) |
|
|
|
|
|
|
|
return eFile!! |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return eFile!! |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getChapterList(book: io.legado.app.data.entities.Book): ArrayList<BookChapter> { |
|
|
|
|
|
|
|
return getEFile(book).getChapterList() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getContent(book: io.legado.app.data.entities.Book, chapter: BookChapter): String? { |
|
|
|
|
|
|
|
return getEFile(book).getContent(chapter) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var epubBook: Book? = null |
|
|
|
private lateinit var mCharset: Charset |
|
|
|
private lateinit var mCharset: Charset |
|
|
|
|
|
|
|
|
|
|
|
init { |
|
|
|
init { |
|
|
@ -24,7 +46,7 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { |
|
|
|
val epubReader = EpubReader() |
|
|
|
val epubReader = EpubReader() |
|
|
|
val inputStream = if (book.bookUrl.isContentPath()) { |
|
|
|
val inputStream = if (book.bookUrl.isContentPath()) { |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
val uri = Uri.parse(book.bookUrl) |
|
|
|
context.contentResolver.openInputStream(uri) |
|
|
|
App.INSTANCE.contentResolver.openInputStream(uri) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
File(book.bookUrl).inputStream() |
|
|
|
File(book.bookUrl).inputStream() |
|
|
|
} |
|
|
|
} |
|
|
@ -33,88 +55,90 @@ class EPUBFile(context: Context, val book: io.legado.app.data.entities.Book) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getContent(chapter: BookChapter): String { |
|
|
|
fun getContent(chapter: BookChapter): String? { |
|
|
|
val resource = epubBook!!.resources.getByHref(chapter.url) |
|
|
|
epubBook?.let { eBook -> |
|
|
|
val content = StringBuilder() |
|
|
|
val resource = eBook.resources.getByHref(chapter.url) |
|
|
|
val doc = Jsoup.parse(String(resource.data, mCharset)) |
|
|
|
val content = StringBuilder() |
|
|
|
val elements = doc.allElements |
|
|
|
val doc = Jsoup.parse(String(resource.data, mCharset)) |
|
|
|
for (element in elements) { |
|
|
|
val elements = doc.allElements |
|
|
|
val contentEs = element.textNodes() |
|
|
|
for (element in elements) { |
|
|
|
for (i in contentEs.indices) { |
|
|
|
val contentEs = element.textNodes() |
|
|
|
val text = contentEs[i].text().trim { it <= ' ' }.htmlFormat() |
|
|
|
for (i in contentEs.indices) { |
|
|
|
if (elements.size > 1) { |
|
|
|
val text = contentEs[i].text().trim { it <= ' ' }.htmlFormat() |
|
|
|
if (text.isNotEmpty()) { |
|
|
|
if (elements.size > 1) { |
|
|
|
if (content.isNotEmpty()) { |
|
|
|
if (text.isNotEmpty()) { |
|
|
|
content.append("\r\n") |
|
|
|
if (content.isNotEmpty()) { |
|
|
|
|
|
|
|
content.append("\r\n") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
content.append("\u3000\u3000").append(text) |
|
|
|
} |
|
|
|
} |
|
|
|
content.append("\u3000\u3000").append(text) |
|
|
|
} else { |
|
|
|
|
|
|
|
content.append(text) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
content.append(text) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return content.toString() |
|
|
|
} |
|
|
|
} |
|
|
|
return content.toString() |
|
|
|
return null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun getChapterList(epubBook: Book): ArrayList<BookChapter> { |
|
|
|
fun getChapterList(): ArrayList<BookChapter> { |
|
|
|
val metadata = epubBook.metadata |
|
|
|
|
|
|
|
book.name = metadata.firstTitle |
|
|
|
|
|
|
|
if (metadata.authors.size > 0) { |
|
|
|
|
|
|
|
val author = |
|
|
|
|
|
|
|
metadata.authors[0].toString().replace("^, |, $".toRegex(), "") |
|
|
|
|
|
|
|
book.author = author |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (metadata.descriptions.size > 0) { |
|
|
|
|
|
|
|
book.intro = Jsoup.parse(metadata.descriptions[0]).text() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
val chapterList = ArrayList<BookChapter>() |
|
|
|
val chapterList = ArrayList<BookChapter>() |
|
|
|
val refs = |
|
|
|
epubBook?.let { eBook -> |
|
|
|
epubBook.tableOfContents.tocReferences |
|
|
|
val metadata = eBook.metadata |
|
|
|
if (refs == null || refs.isEmpty()) { |
|
|
|
book.name = metadata.firstTitle |
|
|
|
val spineReferences = |
|
|
|
if (metadata.authors.size > 0) { |
|
|
|
epubBook.spine.spineReferences |
|
|
|
val author = |
|
|
|
var i = 0 |
|
|
|
metadata.authors[0].toString().replace("^, |, $".toRegex(), "") |
|
|
|
val size = spineReferences.size |
|
|
|
book.author = author |
|
|
|
while (i < size) { |
|
|
|
} |
|
|
|
val resource = |
|
|
|
if (metadata.descriptions.size > 0) { |
|
|
|
spineReferences[i].resource |
|
|
|
book.intro = Jsoup.parse(metadata.descriptions[0]).text() |
|
|
|
var title = resource.title |
|
|
|
} |
|
|
|
if (TextUtils.isEmpty(title)) { |
|
|
|
|
|
|
|
try { |
|
|
|
val refs = eBook.tableOfContents.tocReferences |
|
|
|
val doc = |
|
|
|
if (refs == null || refs.isEmpty()) { |
|
|
|
Jsoup.parse(String(resource.data, mCharset)) |
|
|
|
val spineReferences = eBook.spine.spineReferences |
|
|
|
val elements = doc.getElementsByTag("title") |
|
|
|
var i = 0 |
|
|
|
if (elements.size > 0) { |
|
|
|
val size = spineReferences.size |
|
|
|
title = elements[0].text() |
|
|
|
while (i < size) { |
|
|
|
|
|
|
|
val resource = |
|
|
|
|
|
|
|
spineReferences[i].resource |
|
|
|
|
|
|
|
var title = resource.title |
|
|
|
|
|
|
|
if (TextUtils.isEmpty(title)) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
val doc = |
|
|
|
|
|
|
|
Jsoup.parse(String(resource.data, mCharset)) |
|
|
|
|
|
|
|
val elements = doc.getElementsByTag("title") |
|
|
|
|
|
|
|
if (elements.size > 0) { |
|
|
|
|
|
|
|
title = elements[0].text() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (e: IOException) { |
|
|
|
|
|
|
|
e.printStackTrace() |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e: IOException) { |
|
|
|
|
|
|
|
e.printStackTrace() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
val chapter = BookChapter() |
|
|
|
|
|
|
|
chapter.index = i |
|
|
|
|
|
|
|
chapter.bookUrl = book.bookUrl |
|
|
|
|
|
|
|
chapter.url = resource.href |
|
|
|
|
|
|
|
if (i == 0 && title.isEmpty()) { |
|
|
|
|
|
|
|
chapter.title = "封面" |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
chapter.title = title |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
chapterList.add(chapter) |
|
|
|
|
|
|
|
i++ |
|
|
|
} |
|
|
|
} |
|
|
|
val chapter = BookChapter() |
|
|
|
} else { |
|
|
|
chapter.index = i |
|
|
|
parseMenu(chapterList, refs, 0) |
|
|
|
chapter.bookUrl = book.bookUrl |
|
|
|
for (i in chapterList.indices) { |
|
|
|
chapter.url = resource.href |
|
|
|
chapterList[i].index = i |
|
|
|
if (i == 0 && title.isEmpty()) { |
|
|
|
|
|
|
|
chapter.title = "封面" |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
chapter.title = title |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
chapterList.add(chapter) |
|
|
|
|
|
|
|
i++ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
parseMenu(chapterList, refs, 0) |
|
|
|
|
|
|
|
for (i in chapterList.indices) { |
|
|
|
|
|
|
|
chapterList[i].index = i |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return chapterList |
|
|
|
return chapterList |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun parseMenu( |
|
|
|
private fun parseMenu( |
|
|
|
chapterList: ArrayList<BookChapter>, |
|
|
|
chapterList: ArrayList<BookChapter>, |
|
|
|
refs: List<TOCReference>?, |
|
|
|
refs: List<TOCReference>?, |
|
|
|