pull/1463/head^2
gedoor 3 years ago
parent e9035ada9b
commit 423f61b490
  1. 2
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 92
      app/src/main/java/io/legado/app/model/localBook/EpubFile.kt

@ -183,7 +183,7 @@ object BookHelp {
fun getContent(book: Book, bookChapter: BookChapter): String? { fun getContent(book: Book, bookChapter: BookChapter): String? {
if (book.isLocalTxt() || book.isUmd()) { if (book.isLocalTxt() || book.isUmd()) {
return LocalBook.getContent(book, bookChapter) return LocalBook.getContent(book, bookChapter)
} else if (book.isEpub() && !hasContent(book, bookChapter)) { } else if (book.isEpub()) {
val string = LocalBook.getContent(book, bookChapter) val string = LocalBook.getContent(book, bookChapter)
string?.let { string?.let {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(

@ -12,6 +12,7 @@ import me.ag2s.epublib.domain.EpubBook
import me.ag2s.epublib.domain.TOCReference import me.ag2s.epublib.domain.TOCReference
import me.ag2s.epublib.epub.EpubReader import me.ag2s.epublib.epub.EpubReader
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.select.Elements
import splitties.init.appCtx import splitties.init.appCtx
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File
@ -134,44 +135,48 @@ class EpubFile(var book: Book) {
private fun getContent(chapter: BookChapter): String? { private fun getContent(chapter: BookChapter): String? {
/*获取当前章节文本*/ /*获取当前章节文本*/
epubBook?.let { epubBook?.let { epubBook ->
it.contents val nextUrl = chapter.getVariable("nextUrl")
val body =
Jsoup.parse(String(it.resources.getByHref(chapter.url).data, mCharset)).body()
val startFragmentId = chapter.startFragmentId val startFragmentId = chapter.startFragmentId
val endFragmentId = chapter.endFragmentId val endFragmentId = chapter.endFragmentId
val elements = Elements()
var isChapter = false
/*一些书籍依靠href索引的resource会包含多个章节,需要依靠fragmentId来截取到当前章节的内容*/ /*一些书籍依靠href索引的resource会包含多个章节,需要依靠fragmentId来截取到当前章节的内容*/
/*注:这里较大增加了内容加载的时间,所以首次获取内容后可存储到本地cache,减少重复加载*/ /*注:这里较大增加了内容加载的时间,所以首次获取内容后可存储到本地cache,减少重复加载*/
if (!startFragmentId.isNullOrBlank()) for (res in epubBook.contents) {
body.getElementById(startFragmentId)?.previousElementSiblings()?.remove() if (res.href == chapter.url) {
if (!endFragmentId.isNullOrBlank() && endFragmentId != startFragmentId) isChapter = true
body.getElementById(endFragmentId)?.nextElementSiblings()?.remove() } else if (res.href == nextUrl) {
break
/*选择去除正文中的H标签,部分书籍标题与阅读标题重复待优化*/ }
var tag = Book.hTag if (isChapter) {
if (book.getDelTag(tag)) { val body = Jsoup.parse(String(res.data, mCharset)).body()
body.getElementsByTag("h1").remove() if (!startFragmentId.isNullOrBlank()) {
body.getElementsByTag("h2").remove() body.getElementById(startFragmentId)?.previousElementSiblings()?.remove()
body.getElementsByTag("h3").remove() }
body.getElementsByTag("h4").remove() if (!endFragmentId.isNullOrBlank() && endFragmentId != startFragmentId) {
body.getElementsByTag("h5").remove() body.getElementById(endFragmentId)?.nextElementSiblings()?.remove()
body.getElementsByTag("h6").remove() }
//body.getElementsMatchingOwnText(chapter.title)?.remove() /*选择去除正文中的H标签,部分书籍标题与阅读标题重复待优化*/
} val tag = Book.hTag
if (book.getDelTag(tag)) {
body.getElementsByTag("h1").remove()
body.getElementsByTag("h2").remove()
body.getElementsByTag("h3").remove()
body.getElementsByTag("h4").remove()
body.getElementsByTag("h5").remove()
body.getElementsByTag("h6").remove()
//body.getElementsMatchingOwnText(chapter.title)?.remove()
}
/*选择去除正文中的img标签,目前图片支持效果待优化*/ val children = body.children()
tag = Book.imgTag children.select("script").remove()
if (book.getDelTag(tag)) { children.select("style").remove()
body.getElementsByTag("img").remove() elements.add(body)
}
} }
val elements = body.children()
elements.select("script").remove()
elements.select("style").remove()
/*选择去除正文中的ruby标签,目前注释支持效果待优化*/
tag = Book.rubyTag
var html = elements.outerHtml() var html = elements.outerHtml()
val tag = Book.rubyTag
if (book.getDelTag(tag)) { if (book.getDelTag(tag)) {
html = html.replace("<ruby>\\s?([\\u4e00-\\u9fa5])\\s?.*?</ruby>".toRegex(), "$1") html = html.replace("<ruby>\\s?([\\u4e00-\\u9fa5])\\s?.*?</ruby>".toRegex(), "$1")
} }
@ -275,15 +280,13 @@ class EpubFile(var book: Book) {
var title = content.title var title = content.title
if (TextUtils.isEmpty(title)) { if (TextUtils.isEmpty(title)) {
val elements = Jsoup.parse( val elements = Jsoup.parse(
String( String(epubBook!!.resources.getByHref(content.href).data, mCharset)
epubBook!!.resources.getByHref(content.href).data,
mCharset
)
).getElementsByTag("title") ).getElementsByTag("title")
title = title =
if (elements.size > 0 && elements[0].text() if (elements.size > 0 && elements[0].text().isNotBlank())
.isNotBlank() elements[0].text()
) elements[0].text() else "--卷首--" else
"--卷首--"
} }
chapter.bookUrl = book.bookUrl chapter.bookUrl = book.bookUrl
chapter.title = title chapter.title = title
@ -291,10 +294,9 @@ class EpubFile(var book: Book) {
chapter.startFragmentId = chapter.startFragmentId =
if (content.href.substringAfter("#") == content.href) null if (content.href.substringAfter("#") == content.href) null
else content.href.substringAfter("#") else content.href.substringAfter("#")
if (durIndex > 0) {
val preIndex = durIndex - 1 chapterList.lastOrNull()?.endFragmentId = chapter.startFragmentId
chapterList[preIndex].endFragmentId = chapter.startFragmentId chapterList.lastOrNull()?.putVariable("nextUrl", chapter.url)
}
chapterList.add(chapter) chapterList.add(chapter)
durIndex++ durIndex++
i++ i++
@ -313,10 +315,8 @@ class EpubFile(var book: Book) {
chapter.title = ref.title chapter.title = ref.title
chapter.url = ref.completeHref chapter.url = ref.completeHref
chapter.startFragmentId = ref.fragmentId chapter.startFragmentId = ref.fragmentId
if (durIndex > 0) { chapterList.lastOrNull()?.endFragmentId = chapter.startFragmentId
val preIndex = durIndex - 1 chapterList.lastOrNull()?.putVariable("nextUrl", chapter.url)
chapterList[preIndex].endFragmentId = chapter.startFragmentId
}
chapterList.add(chapter) chapterList.add(chapter)
durIndex++ durIndex++
} }

Loading…
Cancel
Save