pull/32/head
kunfei 5 years ago
parent f98b1f9730
commit 216489288f
  1. 12
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByRegex.kt
  2. 30
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt

@ -0,0 +1,12 @@
package io.legado.app.model.analyzeRule
object AnalyzeByRegex {
fun getElement() {
}
fun getElements() {
}
}

@ -293,14 +293,14 @@ class AnalyzeRule(private var book: BaseBook? = null) {
*/ */
@Throws(Exception::class) @Throws(Exception::class)
private fun splitPutRule(ruleStr: String): String { private fun splitPutRule(ruleStr: String): String {
var ruleStr = ruleStr var vRuleStr = ruleStr
val putMatcher = putPattern.matcher(ruleStr) val putMatcher = putPattern.matcher(vRuleStr)
while (putMatcher.find()) { while (putMatcher.find()) {
ruleStr = ruleStr.replace(putMatcher.group(), "") vRuleStr = vRuleStr.replace(putMatcher.group(), "")
val map = GSON.fromJsonObject<Map<String, String>>(putMatcher.group(1)) val map = GSON.fromJsonObject<Map<String, String>>(putMatcher.group(1))
map?.let { putRule(map) } map?.let { putRule(map) }
} }
return ruleStr return vRuleStr
} }
/** /**
@ -323,21 +323,21 @@ class AnalyzeRule(private var book: BaseBook? = null) {
* 正则替换 * 正则替换
*/ */
private fun replaceRegex(result: String, rule: SourceRule): String { private fun replaceRegex(result: String, rule: SourceRule): String {
var result = result var vResult = result
if (rule.replaceRegex.isNotEmpty()) { if (rule.replaceRegex.isNotEmpty()) {
result = if (rule.replaceFirst) { vResult = if (rule.replaceFirst) {
val pattern = Pattern.compile(rule.replaceRegex) val pattern = Pattern.compile(rule.replaceRegex)
val matcher = pattern.matcher(result) val matcher = pattern.matcher(vResult)
if (matcher.find()) { if (matcher.find()) {
matcher.group(0).replaceFirst(rule.replaceRegex.toRegex(), rule.replacement) matcher.group(0).replaceFirst(rule.replaceRegex.toRegex(), rule.replacement)
} else { } else {
"" ""
} }
} else { } else {
result.replace(rule.replaceRegex.toRegex(), rule.replacement) vResult.replace(rule.replaceRegex.toRegex(), rule.replacement)
} }
} }
return result return vResult
} }
/** /**
@ -346,11 +346,11 @@ class AnalyzeRule(private var book: BaseBook? = null) {
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
@Throws(Exception::class) @Throws(Exception::class)
private fun replaceJs(ruleStr: String): String { private fun replaceJs(ruleStr: String): String {
var ruleStr = ruleStr var vRuleStr = ruleStr
if (ruleStr.contains("{{") && ruleStr.contains("}}")) { if (vRuleStr.contains("{{") && vRuleStr.contains("}}")) {
var jsEval: Any var jsEval: Any
val sb = StringBuffer(ruleStr.length) val sb = StringBuffer(vRuleStr.length)
val expMatcher = EXP_PATTERN.matcher(ruleStr) val expMatcher = EXP_PATTERN.matcher(vRuleStr)
while (expMatcher.find()) { while (expMatcher.find()) {
jsEval = evalJS(expMatcher.group(1), content) jsEval = evalJS(expMatcher.group(1), content)
if (jsEval is String) { if (jsEval is String) {
@ -362,9 +362,9 @@ class AnalyzeRule(private var book: BaseBook? = null) {
} }
} }
expMatcher.appendTail(sb) expMatcher.appendTail(sb)
ruleStr = sb.toString() vRuleStr = sb.toString()
} }
return ruleStr return vRuleStr
} }
/** /**

Loading…
Cancel
Save