From 8a0ff325c5550959f210811fcdb8261b5bb1b44f Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 21 Sep 2020 13:57:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=A7=84=E5=88=99=E8=A7=A3?= =?UTF-8?q?=E6=9E=90bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/analyzeRule/AnalyzeRule.kt | 53 ++++++------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 3077cbf51..b72bcded3 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/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 for (sourceRule in ruleList) { putRule(sourceRule.putMap) + sourceRule.makeUpRule(result) result?.let { result = when (sourceRule.mode) { Mode.Regex -> AnalyzeByRegex.getElement( @@ -324,37 +325,17 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { */ private fun replaceRegex(result: String, rule: SourceRule): String { var vResult = result - val stringBuffer = StringBuffer() - 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()) { + if (rule.replaceRegex.isNotEmpty()) { vResult = if (rule.replaceFirst) { - val pattern = Pattern.compile(replaceRegex) + val pattern = Pattern.compile(rule.replaceRegex) val matcher = pattern.matcher(vResult) if (matcher.find()) { - matcher.group(0)!!.replaceFirst(replaceRegex.toRegex(), rule.replacement) + matcher.group(0)!!.replaceFirst(rule.replaceRegex.toRegex(), rule.replacement) } else { "" } } else { - vResult.replace(replaceRegex.toRegex(), rule.replacement) + vResult.replace(rule.replaceRegex.toRegex(), rule.replacement) } } return vResult @@ -465,18 +446,6 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { } //分离put 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, 拆分 var start = 0 var tmp: String @@ -566,6 +535,18 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { } } 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 + } } }