|
|
@ -1,5 +1,7 @@ |
|
|
|
package io.legado.app.data.entities.rule |
|
|
|
package io.legado.app.data.entities.rule |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.legado.app.utils.splitNotBlank |
|
|
|
|
|
|
|
|
|
|
|
data class Rule ( |
|
|
|
data class Rule ( |
|
|
|
var selectors: List<BaseRule>, |
|
|
|
var selectors: List<BaseRule>, |
|
|
|
var regex: String?, |
|
|
|
var regex: String?, |
|
|
@ -8,6 +10,9 @@ data class Rule ( |
|
|
|
var extra: String? |
|
|
|
var extra: String? |
|
|
|
) { |
|
|
|
) { |
|
|
|
companion object { |
|
|
|
companion object { |
|
|
|
|
|
|
|
val JS_PATTERN = Regex("""\{\{([^}]+?)\}\}""") |
|
|
|
|
|
|
|
val CONST_PATTERN = Regex("""\{(\$\.[^}]+?)\}""") |
|
|
|
|
|
|
|
|
|
|
|
fun parse(input: String) = when { |
|
|
|
fun parse(input: String) = when { |
|
|
|
input.startsWith("$.") -> parseJSON(input) |
|
|
|
input.startsWith("$.") -> parseJSON(input) |
|
|
|
input.startsWith("//") -> parseXPATH(input) |
|
|
|
input.startsWith("//") -> parseXPATH(input) |
|
|
@ -18,15 +23,40 @@ data class Rule ( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun parseCSS(rawRule: String): List<BaseRule> { |
|
|
|
private fun parseCSS(rawRule: String): List<BaseRule> { |
|
|
|
TODO() |
|
|
|
val rules = mutableListOf<BaseRule>() |
|
|
|
|
|
|
|
for (line in rawRule.splitNotBlank("\n")) { |
|
|
|
|
|
|
|
val baseRule = BaseRule(type = RuleType.CSS) |
|
|
|
|
|
|
|
if (line.contains("@@")) { |
|
|
|
|
|
|
|
val temp = line.split("@@") |
|
|
|
|
|
|
|
baseRule.selector = temp[0].trim() |
|
|
|
|
|
|
|
baseRule.attr = temp[1].trim() |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
baseRule.selector = line |
|
|
|
|
|
|
|
baseRule.attr = "text" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
rules.add(baseRule) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return rules |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun parseJSON(rawRule: String): List<BaseRule> { |
|
|
|
private fun parseJSON(rawRule: String): List<BaseRule> { |
|
|
|
TODO() |
|
|
|
val rules = mutableListOf<BaseRule>() |
|
|
|
|
|
|
|
for (line in rawRule.splitNotBlank("\n")) { |
|
|
|
|
|
|
|
val baseRule = BaseRule(type = RuleType.JSON) |
|
|
|
|
|
|
|
baseRule.selector = line |
|
|
|
|
|
|
|
rules.add(baseRule) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return rules |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun parseXPATH(rawRule: String): List<BaseRule> { |
|
|
|
private fun parseXPATH(rawRule: String): List<BaseRule> { |
|
|
|
TODO() |
|
|
|
val rules = mutableListOf<BaseRule>() |
|
|
|
|
|
|
|
for (line in rawRule.splitNotBlank("\n")) { |
|
|
|
|
|
|
|
val baseRule = BaseRule(type = RuleType.XPATH) |
|
|
|
|
|
|
|
baseRule.selector = line |
|
|
|
|
|
|
|
rules.add(baseRule) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return rules |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun parseREGEX(rawRule: String): List<BaseRule> { |
|
|
|
private fun parseREGEX(rawRule: String): List<BaseRule> { |
|
|
@ -34,7 +64,19 @@ data class Rule ( |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun parseCONST(rawRule: String): List<BaseRule> { |
|
|
|
private fun parseCONST(rawRule: String): List<BaseRule> { |
|
|
|
TODO() |
|
|
|
val rules = mutableListOf<BaseRule>() |
|
|
|
|
|
|
|
val subRule = mutableListOf<String>() |
|
|
|
|
|
|
|
for (line in rawRule.splitNotBlank("\n")) { |
|
|
|
|
|
|
|
subRule.clear() |
|
|
|
|
|
|
|
val baseRule = BaseRule(type = RuleType.JSON) |
|
|
|
|
|
|
|
baseRule.template = CONST_PATTERN.replace(line) { match -> |
|
|
|
|
|
|
|
subRule.add(match.groupValues[1]) |
|
|
|
|
|
|
|
"%s" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
baseRule.selector = subRule.joinToString("\n") |
|
|
|
|
|
|
|
rules.add(baseRule) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return rules |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun parseJS(rawRule: String): List<BaseRule> { |
|
|
|
private fun parseJS(rawRule: String): List<BaseRule> { |
|
|
@ -44,6 +86,12 @@ data class Rule ( |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
* CONST 规则说明: |
|
|
|
|
|
|
|
* {$.xxx} 表示要获取 JSON 变量 $.xxx |
|
|
|
|
|
|
|
* {@.yyy} 表示要获取之前存储的变量 yyy |
|
|
|
|
|
|
|
* {#.zzz} 表示直接输出 zzz,可以留空,{#.} 什么也不输出 |
|
|
|
|
|
|
|
* */ |
|
|
|
enum class RuleType { |
|
|
|
enum class RuleType { |
|
|
|
CSS, XPATH, JSON, REGEX, CONST, JS, HYBRID |
|
|
|
CSS, XPATH, JSON, REGEX, CONST, JS, HYBRID |
|
|
|
} |
|
|
|
} |