Updated rule

pull/32/head
atbest 6 years ago
parent 680520d6f9
commit 500363a1a7
  1. 6
      app/src/main/java/io/legado/app/data/entities/rule/BaseRule.kt
  2. 58
      app/src/main/java/io/legado/app/data/entities/rule/Rule.kt
  3. 4
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -1,8 +1,8 @@
package io.legado.app.data.entities.rule
data class BaseRule(
var selector: String,
var template: String?,
var attr: String?,
var selector: String = "",
var template: String? = null,
var attr: String? = null,
var type: RuleType
)

@ -1,5 +1,7 @@
package io.legado.app.data.entities.rule
import io.legado.app.utils.splitNotBlank
data class Rule (
var selectors: List<BaseRule>,
var regex: String?,
@ -8,6 +10,9 @@ data class Rule (
var extra: String?
) {
companion object {
val JS_PATTERN = Regex("""\{\{([^}]+?)\}\}""")
val CONST_PATTERN = Regex("""\{(\$\.[^}]+?)\}""")
fun parse(input: String) = when {
input.startsWith("$.") -> parseJSON(input)
input.startsWith("//") -> parseXPATH(input)
@ -18,15 +23,40 @@ data class Rule (
}
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> {
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> {
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> {
@ -34,7 +64,19 @@ data class Rule (
}
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> {
@ -44,6 +86,12 @@ data class Rule (
}
}
/*
* CONST 规则说明
* {$.xxx} 表示要获取 JSON 变量 $.xxx
* {@.yyy} 表示要获取之前存储的变量 yyy
* {#.zzz} 表示直接输出 zzz可以留空{#.} 什么也不输出
* */
enum class RuleType {
CSS, XPATH, JSON, REGEX, CONST, JS, HYBRID
}
}

@ -4,3 +4,7 @@ fun String?.strim() = if (this.isNullOrBlank()) null else this.trim()
fun String.isAbsUrl() = this.startsWith("http://", true)
|| this.startsWith("https://", true)
fun String.splitNotBlank(delim: String) = if (!this.contains(delim)) sequenceOf(this) else
this.split(delim).asSequence().map { it.trim() }.filterNot { it.isBlank() }

Loading…
Cancel
Save