pull/1238/head^2 3.21.081308
gedoor 3 years ago
parent ba1e515ccb
commit 39cced7cb0
  1. 32
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt

@ -7,7 +7,6 @@ import org.jsoup.select.Collector
import org.jsoup.select.Elements import org.jsoup.select.Elements
import org.jsoup.select.Evaluator import org.jsoup.select.Evaluator
import org.seimicrawler.xpath.JXNode import org.seimicrawler.xpath.JXNode
import java.util.*
/** /**
* Created by GKF on 2018/1/25. * Created by GKF on 2018/1/25.
@ -71,7 +70,7 @@ class AnalyzeByJSoup(doc: Any) {
val results = ArrayList<List<String>>() val results = ArrayList<List<String>>()
for (ruleStrX in ruleStrS) { for (ruleStrX in ruleStrS) {
val temp: List<String>? = val temp: ArrayList<String>? =
if (sourceRule.isCss) { if (sourceRule.isCss) {
val lastIndex = ruleStrX.lastIndexOf('@') val lastIndex = ruleStrX.lastIndexOf('@')
getResultLast( getResultLast(
@ -83,11 +82,8 @@ class AnalyzeByJSoup(doc: Any) {
} }
if (!temp.isNullOrEmpty()) { if (!temp.isNullOrEmpty()) {
results.add(temp)
results.add(temp) //!temp.isNullOrEmpty()时,results.isNotEmpty()为true
if (ruleAnalyzes.elementsType == "||") break if (ruleAnalyzes.elementsType == "||") break
} }
} }
if (results.size > 0) { if (results.size > 0) {
@ -181,7 +177,7 @@ class AnalyzeByJSoup(doc: Any) {
/** /**
* 获取内容列表 * 获取内容列表
*/ */
private fun getResultList(ruleStr: String): List<String>? { private fun getResultList(ruleStr: String): ArrayList<String>? {
if (ruleStr.isEmpty()) return null if (ruleStr.isEmpty()) return null
@ -210,33 +206,43 @@ class AnalyzeByJSoup(doc: Any) {
/** /**
* 根据最后一个规则获取内容 * 根据最后一个规则获取内容
*/ */
private fun getResultLast(elements: Elements, lastRule: String): List<String> { private fun getResultLast(elements: Elements, lastRule: String): ArrayList<String> {
val textS = ArrayList<String>() val textS = ArrayList<String>()
try { try {
when (lastRule) { when (lastRule) {
"text" -> for (element in elements) { "text" -> for (element in elements) {
textS.add(element.text()) val text = element.text()
if (text.isNotEmpty()) {
textS.add(text)
}
} }
"textNodes" -> for (element in elements) { "textNodes" -> for (element in elements) {
val tn = arrayListOf<String>() val tn = arrayListOf<String>()
val contentEs = element.textNodes() val contentEs = element.textNodes()
for (item in contentEs) { for (item in contentEs) {
val temp = item.text().trim { it <= ' ' } val text = item.text().trim { it <= ' ' }
if (temp.isNotEmpty()) { if (text.isNotEmpty()) {
tn.add(temp) tn.add(text)
} }
} }
if (tn.isNotEmpty()) {
textS.add(tn.joinToString("\n")) textS.add(tn.joinToString("\n"))
} }
}
"ownText" -> for (element in elements) { "ownText" -> for (element in elements) {
textS.add(element.ownText()) val text = element.ownText()
if (text.isNotEmpty()) {
textS.add(text)
}
} }
"html" -> { "html" -> {
elements.select("script").remove() elements.select("script").remove()
elements.select("style").remove() elements.select("style").remove()
val html = elements.outerHtml() val html = elements.outerHtml()
if (html.isNotEmpty()) {
textS.add(html) textS.add(html)
} }
}
"all" -> textS.add(elements.outerHtml()) "all" -> textS.add(elements.outerHtml())
else -> for (element in elements) { else -> for (element in elements) {

Loading…
Cancel
Save