修复规则解析bug

pull/392/head
gedoor 4 years ago
parent 91b94943e0
commit 8a0ff325c5
  1. 53
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt

@ -238,6 +238,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
if (ruleList.isNotEmpty()) result = o if (ruleList.isNotEmpty()) result = o
for (sourceRule in ruleList) { for (sourceRule in ruleList) {
putRule(sourceRule.putMap) putRule(sourceRule.putMap)
sourceRule.makeUpRule(result)
result?.let { result?.let {
result = when (sourceRule.mode) { result = when (sourceRule.mode) {
Mode.Regex -> AnalyzeByRegex.getElement( Mode.Regex -> AnalyzeByRegex.getElement(
@ -324,37 +325,17 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
*/ */
private fun replaceRegex(result: String, rule: SourceRule): String { private fun replaceRegex(result: String, rule: SourceRule): String {
var vResult = result var vResult = result
val stringBuffer = StringBuffer() if (rule.replaceRegex.isNotEmpty()) {
val evalMatcher = replacePattern.matcher(rule.replaceRegex)
while (evalMatcher.find()) {
val jsEval = evalMatcher.group().let {
if (it.startsWith("@get:", true)) {
get(it.substring(6, it.lastIndex))
} else {
evalJS(it.substring(2, it.length - 2), result)
}
} ?: ""
if (jsEval is String) {
evalMatcher.appendReplacement(stringBuffer, Pattern.quote(jsEval))
} else if (jsEval is Double && jsEval % 1.0 == 0.0) {
evalMatcher.appendReplacement(stringBuffer, String.format("%.0f", jsEval))
} else {
evalMatcher.appendReplacement(stringBuffer, Pattern.quote(jsEval.toString()))
}
}
evalMatcher.appendTail(stringBuffer)
val replaceRegex = stringBuffer.toString()
if (replaceRegex.isNotEmpty()) {
vResult = if (rule.replaceFirst) { vResult = if (rule.replaceFirst) {
val pattern = Pattern.compile(replaceRegex) val pattern = Pattern.compile(rule.replaceRegex)
val matcher = pattern.matcher(vResult) val matcher = pattern.matcher(vResult)
if (matcher.find()) { if (matcher.find()) {
matcher.group(0)!!.replaceFirst(replaceRegex.toRegex(), rule.replacement) matcher.group(0)!!.replaceFirst(rule.replaceRegex.toRegex(), rule.replacement)
} else { } else {
"" ""
} }
} else { } else {
vResult.replace(replaceRegex.toRegex(), rule.replacement) vResult.replace(rule.replaceRegex.toRegex(), rule.replacement)
} }
} }
return vResult return vResult
@ -465,18 +446,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
} }
//分离put //分离put
rule = splitPutRule(rule, putMap) rule = splitPutRule(rule, putMap)
//分离正则表达式
val ruleStrS = rule.trim { it <= ' ' }.split("##")
rule = ruleStrS[0]
if (ruleStrS.size > 1) {
replaceRegex = ruleStrS[1]
}
if (ruleStrS.size > 2) {
replacement = ruleStrS[2]
}
if (ruleStrS.size > 3) {
replaceFirst = true
}
//@get,{{ }},$1, 拆分 //@get,{{ }},$1, 拆分
var start = 0 var start = 0
var tmp: String var tmp: String
@ -566,6 +535,18 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
} }
} }
rule = infoVal.toString() rule = infoVal.toString()
//分离正则表达式
val ruleStrS = rule.trim { it <= ' ' }.split("##")
rule = ruleStrS[0]
if (ruleStrS.size > 1) {
replaceRegex = ruleStrS[1]
}
if (ruleStrS.size > 2) {
replacement = ruleStrS[2]
}
if (ruleStrS.size > 3) {
replaceFirst = true
}
} }
} }

Loading…
Cancel
Save