修复替换里的@get:{key}和{{js}}

pull/379/head
gedoor 4 years ago
parent 9f8d9123a3
commit 65190d6fce
  1. 32
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  2. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -324,17 +324,37 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
*/
private fun replaceRegex(result: String, rule: SourceRule): String {
var vResult = result
if (rule.replaceRegex.isNotEmpty()) {
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, jsEval)
} else if (jsEval is Double && jsEval % 1.0 == 0.0) {
evalMatcher.appendReplacement(stringBuffer, String.format("%.0f", jsEval))
} else {
evalMatcher.appendReplacement(stringBuffer, jsEval.toString())
}
}
evalMatcher.appendTail(stringBuffer)
val replaceRegex = stringBuffer.toString()
if (replaceRegex.isNotEmpty()) {
vResult = if (rule.replaceFirst) {
val pattern = Pattern.compile(rule.replaceRegex)
val pattern = Pattern.compile(replaceRegex)
val matcher = pattern.matcher(vResult)
if (matcher.find()) {
matcher.group(0)!!.replaceFirst(rule.replaceRegex.toRegex(), rule.replacement)
matcher.group(0)!!.replaceFirst(replaceRegex.toRegex(), rule.replacement)
} else {
""
}
} else {
vResult.replace(rule.replaceRegex.toRegex(), rule.replacement)
vResult.replace(replaceRegex.toRegex(), rule.replacement)
}
}
return vResult
@ -644,6 +664,10 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
"@get:\\{[^}]+?\\}|\\{\\{[\\w\\W]*?\\}\\}|\\$\\d{1,2}",
Pattern.CASE_INSENSITIVE
)
private val replacePattern = Pattern.compile(
"@get:\\{[^}]+?\\}|\\{\\{[\\w\\W]*?\\}\\}",
Pattern.CASE_INSENSITIVE
)
}
}

@ -150,7 +150,7 @@ class AnalyzeUrl(
//js
if (ruleUrl.contains("{{") && ruleUrl.contains("}}")) {
var jsEval: Any
val sb = StringBuffer(ruleUrl.length)
val sb = StringBuffer()
val simpleBindings = SimpleBindings()
simpleBindings["java"] = this
simpleBindings["baseUrl"] = baseUrl

Loading…
Cancel
Save