pull/1352/head
gedoor 3 years ago
parent 7cade2b051
commit 69b80ef8a1
  1. 33
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSonPath.kt
  2. 72
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt
  3. 11
      app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt

@ -41,24 +41,13 @@ class AnalyzeByJSonPath(json: Any) {
result = ruleAnalyzes.innerRule("{$.") { getString(it) } //替换所有{$.rule...} result = ruleAnalyzes.innerRule("{$.") { getString(it) } //替换所有{$.rule...}
if (result.isEmpty()) { //st为空,表明无成功替换的内嵌规则 if (result.isEmpty()) { //st为空,表明无成功替换的内嵌规则
try { try {
val ob = ctx.read<Any>(rule) val ob = ctx.read<Any>(rule)
result = if (ob is List<*>) {
result = (if (ob is List<*>) { ob.joinToString("\n")
} else {
val builder = StringBuilder() ob.toString()
for (o in ob) { }
builder.append(o).append("\n")
}
builder.deleteCharAt(builder.lastIndex) //删除末尾赘余换行
builder
} else ob).toString()
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
@ -90,21 +79,15 @@ class AnalyzeByJSonPath(json: Any) {
if (rules.size == 1) { if (rules.size == 1) {
ruleAnalyzes.reSetPos() //将pos重置为0,复用解析器 ruleAnalyzes.reSetPos() //将pos重置为0,复用解析器
val st = ruleAnalyzes.innerRule("{$.") { getString(it) } //替换所有{$.rule...} val st = ruleAnalyzes.innerRule("{$.") { getString(it) } //替换所有{$.rule...}
if (st.isEmpty()) { //st为空,表明无成功替换的内嵌规则 if (st.isEmpty()) { //st为空,表明无成功替换的内嵌规则
try { try {
val obj = ctx.read<Any>(rule) //kotlin的Any型返回值不包含null ,删除赘余 ?: return result val obj = ctx.read<Any>(rule) //kotlin的Any型返回值不包含null ,删除赘余 ?: return result
if (obj is List<*>) { if (obj is List<*>) {
for (o in obj) result.add(o.toString()) for (o in obj) result.add(o.toString())
} else {
} else result.add(obj.toString()) result.add(obj.toString())
}
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }

@ -1,7 +1,6 @@
package io.legado.app.model.analyzeRule package io.legado.app.model.analyzeRule
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.utils.printOnDebug
import org.jsoup.Jsoup import org.jsoup.Jsoup
import org.jsoup.nodes.Element import org.jsoup.nodes.Element
import org.jsoup.select.Collector import org.jsoup.select.Collector
@ -209,55 +208,50 @@ class AnalyzeByJSoup(doc: Any) {
*/ */
private fun getResultLast(elements: Elements, lastRule: String): ArrayList<String> { private fun getResultLast(elements: Elements, lastRule: String): ArrayList<String> {
val textS = ArrayList<String>() val textS = ArrayList<String>()
try { when (lastRule) {
when (lastRule) { "text" -> for (element in elements) {
"text" -> for (element in elements) { val text = element.text()
val text = element.text() if (text.isNotEmpty()) {
textS.add(text)
}
}
"textNodes" -> for (element in elements) {
val tn = arrayListOf<String>()
val contentEs = element.textNodes()
for (item in contentEs) {
val text = item.text().trim { it <= ' ' }
if (text.isNotEmpty()) { if (text.isNotEmpty()) {
textS.add(text) tn.add(text)
} }
} }
"textNodes" -> for (element in elements) { if (tn.isNotEmpty()) {
val tn = arrayListOf<String>() textS.add(tn.joinToString("\n"))
val contentEs = element.textNodes()
for (item in contentEs) {
val text = item.text().trim { it <= ' ' }
if (text.isNotEmpty()) {
tn.add(text)
}
}
if (tn.isNotEmpty()) {
textS.add(tn.joinToString("\n"))
}
} }
"ownText" -> for (element in elements) { }
val text = element.ownText() "ownText" -> for (element in elements) {
if (text.isNotEmpty()) { val text = element.ownText()
textS.add(text) if (text.isNotEmpty()) {
} textS.add(text)
} }
"html" -> { }
elements.select("script").remove() "html" -> {
elements.select("style").remove() elements.select("script").remove()
val html = elements.outerHtml() elements.select("style").remove()
if (html.isNotEmpty()) { val html = elements.outerHtml()
textS.add(html) if (html.isNotEmpty()) {
} textS.add(html)
} }
"all" -> textS.add(elements.outerHtml()) }
else -> for (element in elements) { "all" -> textS.add(elements.outerHtml())
else -> for (element in elements) {
val url = element.attr(lastRule) val url = element.attr(lastRule)
if (url.isBlank() || textS.contains(url)) continue if (url.isBlank() || textS.contains(url)) continue
textS.add(url) textS.add(url)
}
} }
} catch (e: Exception) {
e.printOnDebug()
} }
return textS return textS
} }

@ -11,7 +11,6 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
private var rule = ArrayList<String>() //分割出的规则列表 private var rule = ArrayList<String>() //分割出的规则列表
private var step: Int = 0 //分割字符的长度 private var step: Int = 0 //分割字符的长度
var elementsType = "" //当前分割字符串 var elementsType = "" //当前分割字符串
var innerType = true //是否为内嵌{{}}
fun trim() { // 修剪当前规则之前的"@"或者空白符 fun trim() { // 修剪当前规则之前的"@"或者空白符
if (queue[pos] == '@' || queue[pos] < '!') { //在while里重复设置start和startX会拖慢执行速度,所以先来个判断是否存在需要修剪的字段,最后再一次性设置start和startX if (queue[pos] == '@' || queue[pos] < '!') { //在while里重复设置start和startX会拖慢执行速度,所以先来个判断是否存在需要修剪的字段,最后再一次性设置start和startX
@ -33,7 +32,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
* @param seq 查找的字符串 **区分大小写** * @param seq 查找的字符串 **区分大小写**
* @return 是否找到相应字段 * @return 是否找到相应字段
*/ */
fun consumeTo(seq: String): Boolean { private fun consumeTo(seq: String): Boolean {
start = pos //将处理到的位置设置为规则起点 start = pos //将处理到的位置设置为规则起点
val offset = queue.indexOf(seq, pos) val offset = queue.indexOf(seq, pos)
return if (offset != -1) { return if (offset != -1) {
@ -47,7 +46,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
* @param seq 匹配字符串序列 * @param seq 匹配字符串序列
* @return 成功返回true并设置间隔失败则直接返回fasle * @return 成功返回true并设置间隔失败则直接返回fasle
*/ */
fun consumeToAny(vararg seq: String): Boolean { private fun consumeToAny(vararg seq: String): Boolean {
var pos = pos //声明新变量记录匹配位置,不更改类本身的位置 var pos = pos //声明新变量记录匹配位置,不更改类本身的位置
@ -89,7 +88,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
/** /**
* 拉出一个非内嵌代码平衡组存在转义文本 * 拉出一个非内嵌代码平衡组存在转义文本
*/ */
fun chompCodeBalanced(open: Char, close: Char): Boolean { private fun chompCodeBalanced(open: Char, close: Char): Boolean {
var pos = pos //声明临时变量记录匹配位置,匹配成功后才同步到类的pos var pos = pos //声明临时变量记录匹配位置,匹配成功后才同步到类的pos
@ -129,7 +128,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
/** /**
* 拉出一个规则平衡组经过仔细测试xpath和jsoup中引号内转义字符无效 * 拉出一个规则平衡组经过仔细测试xpath和jsoup中引号内转义字符无效
*/ */
fun chompRuleBalanced(open: Char, close: Char): Boolean { private fun chompRuleBalanced(open: Char, close: Char): Boolean {
var pos = pos //声明临时变量记录匹配位置,匹配成功后才同步到类的pos var pos = pos //声明临时变量记录匹配位置,匹配成功后才同步到类的pos
var depth = 0 //嵌套深度 var depth = 0 //嵌套深度
@ -365,8 +364,6 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
}.toString() }.toString()
} }
val ruleTypeList = ArrayList<String>()
//设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced //设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced
val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced

Loading…
Cancel
Save