Merge branch 'master' into master

pull/1128/head
kunfei 3 years ago committed by GitHub
commit ee0a6e63bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 111
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  2. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  3. 59
      app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt
  4. 3
      app/src/main/java/io/legado/app/service/help/ReadBook.kt

@ -360,48 +360,21 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions {
fun splitSourceRule(ruleStr: String?, isList: Boolean = false): List<SourceRule> {
if (ruleStr.isNullOrEmpty()) return ArrayList<SourceRule>()
val ruleList = ArrayList<SourceRule>()
//检测Mode
var mMode: Mode = Mode.Default
fun mode(ruleStr0:String)=when {
ruleStr0.startsWith("@@") -> {
mMode = Mode.Default
ruleStr0.substring(2)
}
ruleStr0.startsWith("@XPath:", true) -> {
mMode = Mode.XPath
ruleStr0.substring(7)
}
ruleStr0.startsWith("/") -> {//XPath特征很明显,无需配置单独的识别标头
mMode = Mode.XPath
ruleStr0
}
ruleStr0.startsWith("@Json:", true) -> {
mMode = Mode.Json
ruleStr0.substring(6)
}
( ruleStr0[1] == '.' || ruleStr0[1] == '[') && ruleStr0[0] == '$' || isJSON -> {
mMode = Mode.Json
ruleStr0
}
else -> {
mMode = Mode.Default
ruleStr0
}
}
//拆分为规则列表
var start = if(isList && ruleStr.startsWith(":")){ //仅首字符为:时为AllInOne,其实:与伪类选择器冲突,建议改成?更合理
var start = 0
//仅首字符为:时为AllInOne,其实:与伪类选择器冲突,建议改成?更合理
if (isList && ruleStr.startsWith(":")) {
mMode = Mode.Regex
isRegex = true
1
}else 0
start = 1
}
var tmp: String
val jsMatcher = JS_PATTERN.matcher(ruleStr)
while (jsMatcher.find()) {
if (jsMatcher.start() > start) {
tmp = ruleStr.substring(start, jsMatcher.start()).trim { it <= ' ' }
if (tmp.isNotEmpty()) {
ruleList.add(SourceRule(mode(tmp), mMode))
ruleList.add(SourceRule(tmp, mMode))
}
}
ruleList.add(SourceRule(jsMatcher.group(2) ?: jsMatcher.group(1), Mode.Js))
@ -411,7 +384,7 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions {
if (ruleStr.length > start) {
tmp = ruleStr.substring(start).trim { it <= ' ' }
if (tmp.isNotEmpty()) {
ruleList.add(SourceRule(mode(tmp), mMode))
ruleList.add(SourceRule(tmp, mMode))
}
}
@ -437,43 +410,56 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions {
init {
this.mode = mainMode
rule = when {
ruleStr.startsWith("@CSS:", true) -> {
mode = Mode.Default
ruleStr
}
ruleStr.startsWith("@@") -> {
mode = Mode.Default
ruleStr.substring(2)
}
ruleStr.startsWith("@XPath:", true) -> {
mode = Mode.XPath
ruleStr.substring(7)
}
ruleStr.startsWith("/") -> {//XPath特征很明显,无需配置单独的识别标头
mode = Mode.XPath
ruleStr
}
ruleStr.startsWith("@Json:", true) -> {
mode = Mode.Json
ruleStr.substring(6)
}
(ruleStr[1] == '.' || ruleStr[1] == '[') && ruleStr.startsWith("$.") || isJSON -> {
mode = Mode.Json
ruleStr
}
else -> {
ruleStr
}
}
//分离put
rule = splitPutRule(ruleStr, putMap)
rule = splitPutRule(rule, putMap)
//@get,{{ }}, 拆分
var start = 0
var tmp: String
val evalMatcher = evalPattern.matcher(rule)
if (evalMatcher.find()) {
var modeX = mode == Mode.Js || mode == Mode.Regex
if (evalMatcher.start() > 0 ) {
tmp = rule.substring(0, evalMatcher.start())
modeX = modeX || tmp.contains("##")
splitRegex(tmp)
}
if(!modeX)mode = Mode.Regex
tmp = evalMatcher.group()
when {
tmp.startsWith("@get:", true) -> {
ruleType.add(getRuleType)
ruleParam.add(tmp.substring(6, tmp.lastIndex))
}
tmp.startsWith("{{") -> {
ruleType.add(jsRuleType)
ruleParam.add(tmp.substring(2, tmp.length - 2))
}
else -> {
splitRegex(tmp)
}
if (mode != Mode.Js && mode != Mode.Regex
&& evalMatcher.start() == 0
) {
mode = Mode.Regex
}
start = evalMatcher.end()
while (evalMatcher.find()){
do {
if (evalMatcher.start() > start) {
tmp = rule.substring(start, evalMatcher.start())
if (mode != Mode.Js && mode != Mode.Regex
&& start == 0 && !tmp.contains("##")
) {
mode = Mode.Regex
}
splitRegex(tmp)
}
tmp = evalMatcher.group()
@ -491,9 +477,8 @@ class AnalyzeRule(val ruleData: RuleDataInterface) : JsExtensions {
}
}
start = evalMatcher.end()
} while (evalMatcher.find())
}
}
if (rule.length > start) {
tmp = rule.substring(start)
splitRegex(tmp)

@ -143,7 +143,6 @@ class AnalyzeUrl(
* 处理URL
*/
private fun initUrl() { //replaceKeyPageJs已经替换掉额外内容,此处url是基础形式,可以直接切首个‘,’之前字符串。
val urlMatcher = paramPattern.matcher(ruleUrl)
urlHasQuery = if(urlMatcher.find())ruleUrl.substring(0,urlMatcher.start()) else ruleUrl
url = NetworkUtils.getAbsoluteURL(baseUrl,urlHasQuery )
@ -152,7 +151,6 @@ class AnalyzeUrl(
}
if(urlHasQuery.length != ruleUrl.length ) {
GSON.fromJsonObject<UrlOption>(ruleUrl.substring(urlMatcher.end()))?.let { option ->
option.method?.let {
if (it.equals("POST", true)) method = RequestMethod.POST
}

@ -1,8 +1,9 @@
package io.legado.app.model.analyzeRule
import io.legado.app.utils.isJson
import java.util.ArrayList
import java.util.*
import java.util.regex.Pattern
import kotlin.collections.HashMap
//通用的规则切分处理
@ -70,6 +71,7 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
}
return false
}
/**
* 从剩余字串中拉出一个字符串直到但不包括匹配序列匹配参数列表中一项即为匹配或剩余字串用完
* @param seq 匹配字符串序列
@ -380,7 +382,12 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
val posPre = pos //记录consumeTo匹配位置
if (consumeTo(endStr)) {
val frv = fr(queue.substring(posPre, pos))
st.append(queue.substring(startX, posPre - startStr.length) + frv) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串
st.append(
queue.substring(
startX,
posPre - startStr.length
) + frv
) //压入内嵌规则前的内容,及内嵌规则解析得到的字符串
pos += endStr.length //跳过结束字符串
startX = pos //记录下次规则起点
}
@ -394,8 +401,10 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
//-----------此处向下的函数和变量都未被使用,但以后要用--------
val ruleTypeList = ArrayList<String>()
//设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced
val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced
enum class Mode {
XPath, Json, Default, Js, Regex
}
@ -538,12 +547,13 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
/**
* 参数字符串
*/
private val STARTSTRURL = arrayOf(",{",)
private val STARTSTRURL = arrayOf(",{")
private val regexPattern = Pattern.compile("\\$\\d{1,2}")
private val putPattern = Pattern.compile("@put:(\\{[^}]+?\\})", Pattern.CASE_INSENSITIVE)
private val getPattern = Pattern.compile("@get:\\{([^}]+?)\\}", Pattern.CASE_INSENSITIVE)
private val evalPattern = Pattern.compile("\\{\\{[\\w\\W]*?\\}\\}", Pattern.CASE_INSENSITIVE)
private val evalPattern =
Pattern.compile("\\{\\{[\\w\\W]*?\\}\\}", Pattern.CASE_INSENSITIVE)
val ENDSTR = mapOf(
"<js>" to "</js>",
@ -553,18 +563,35 @@ class RuleAnalyzer(data: String, code: Boolean = false) {
/**
* 规则起始字符串
*/
private val STARTSTR = arrayOf("@js:","<js>","</js>","##","@@","@"
,"{{@", "{{","}}"
,"}"
, "{@", "{/", "{$"
, "{class"
, "{id"
, "{tag"
, "{text"
, "{children"
,"/","$","@xpath:","@json:","@css:"
,"||", "&&", "%%"
,"@get:{","@put:{"
private val STARTSTR = arrayOf(
"@js:",
"<js>",
"</js>",
"##",
"@@",
"@",
"{{@",
"{{",
"}}",
"}",
"{@",
"{/",
"{$",
"{class",
"{id",
"{tag",
"{text",
"{children",
"/",
"$",
"@xpath:",
"@json:",
"@css:",
"||",
"&&",
"%%",
"@get:{",
"@put:{"
)
/**

@ -415,7 +415,8 @@ object ReadBook {
else -> chapter.title
}
val contents = contentProcessor!!.getContent(book, chapter.title, content)
val textChapter = ChapterProvider.getTextChapter(book, chapter,contents,chapterSize)
val textChapter =
ChapterProvider.getTextChapter(book, chapter, contents, chapterSize)
val offset = chapter.index - durChapterIndex
if (upContent) callBack?.upContent(offset, resetPageOffset)

Loading…
Cancel
Save