pull/1111/head
gedoor 3 years ago
parent f805081a51
commit c6c07234f8
  1. 75
      app/src/main/java/io/legado/app/model/analyzeRule/RuleAnalyzer.kt

@ -13,12 +13,16 @@ class RuleAnalyzer(data: String, code:Boolean = false) {
private var innerStr: Boolean = false //true表示以平衡组的起点为规则起始,false表示不这样 private var innerStr: Boolean = false //true表示以平衡组的起点为规则起始,false表示不这样
private var step: Int = 0 //分割字符的长度 private var step: Int = 0 //分割字符的长度
val chompBalanced = if(code) ::chompCodeBalanced else ::chompRuleBalanced //设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced //设置平衡组函数,json或JavaScript时设置成chompCodeBalanced,否则为chompRuleBalanced
val chompBalanced = if (code) ::chompCodeBalanced else ::chompRuleBalanced
var elementsType = "" var elementsType = ""
//当前平衡字段 //当前平衡字段
fun currBalancedString( stepStart:Int = 1 , stepEnd:Int = 1): String { //stepStart平衡字符的起始分隔字串长度,stepEnd平衡字符的结束分隔字串长度 fun currBalancedString(
stepStart: Int = 1,
stepEnd: Int = 1
): String { //stepStart平衡字符的起始分隔字串长度,stepEnd平衡字符的结束分隔字串长度
return queue.substring(startX + stepStart, pos - stepEnd) //当前平衡字段 return queue.substring(startX + stepStart, pos - stepEnd) //当前平衡字段
} }
@ -130,13 +134,15 @@ class RuleAnalyzer(data: String, code:Boolean = false) {
} }
//其中js只要符合语法,就不用避开任何阅读关键字,自由发挥 //其中js只要符合语法,就不用避开任何阅读关键字,自由发挥
fun chompJsBalanced(f: ((Char) -> Boolean?) = { fun chompJsBalanced(
f: ((Char) -> Boolean?) = {
when (it) { when (it) {
'{' -> true //开始嵌套一层 '{' -> true //开始嵌套一层
'}' -> false //闭合一层嵌套 '}' -> false //闭合一层嵌套
else -> null else -> null
} }
} ): Boolean { }
): Boolean {
var pos = pos //声明变量记录临时处理位置 var pos = pos //声明变量记录临时处理位置
var depth = 0 //嵌套深度 var depth = 0 //嵌套深度
var bracketsDepth = 0 //[]嵌套深度 var bracketsDepth = 0 //[]嵌套深度
@ -152,9 +158,12 @@ class RuleAnalyzer(data: String, code:Boolean = false) {
if (pos == queue.length) break if (pos == queue.length) break
var c = queue[pos++] var c = queue[pos++]
if (c != '\\') { //非转义字符 if (c != '\\') { //非转义字符
if (c == '\'' && !commits && !commit && !regex && !inDoubleQuote && !inOtherQuote) inSingleQuote = !inSingleQuote //匹配具有语法功能的单引号 if (c == '\'' && !commits && !commit && !regex && !inDoubleQuote && !inOtherQuote) inSingleQuote =
else if (c == '"' && !commits && !commit && !regex && !inSingleQuote && !inOtherQuote) inDoubleQuote = !inDoubleQuote //匹配具有语法功能的双引号 !inSingleQuote //匹配具有语法功能的单引号
else if (c == '`' && !commits && !commit && !regex && !inSingleQuote && !inDoubleQuote) inOtherQuote = !inOtherQuote //匹配具有语法功能的'`' else if (c == '"' && !commits && !commit && !regex && !inSingleQuote && !inOtherQuote) inDoubleQuote =
!inDoubleQuote //匹配具有语法功能的双引号
else if (c == '`' && !commits && !commit && !regex && !inSingleQuote && !inDoubleQuote) inOtherQuote =
!inOtherQuote //匹配具有语法功能的'`'
else if (c == '/' && !commits && !commit && !regex && !inSingleQuote && !inDoubleQuote && !inOtherQuote) { //匹配注释或正则起点 else if (c == '/' && !commits && !commit && !regex && !inSingleQuote && !inDoubleQuote && !inOtherQuote) { //匹配注释或正则起点
c = queue[pos++] c = queue[pos++]
when (c) { when (c) {
@ -162,12 +171,10 @@ class RuleAnalyzer(data: String, code:Boolean = false) {
'*' -> commits = true //匹配多行注释起点 '*' -> commits = true //匹配多行注释起点
else -> regex = true //匹配正则起点 else -> regex = true //匹配正则起点
} }
} } else if (commits && c == '*') { //匹配多行注释终点
else if(commits && c == '*') { //匹配多行注释终点
c = queue[pos++] c = queue[pos++]
if (c == '/') commits = false if (c == '/') commits = false
} } else if (regex && c == '/') { //正则的终点或[]平衡
else if(regex && c == '/') { //正则的终点或[]平衡
when (c) { when (c) {
'/' -> regex = false//匹配正则终点 '/' -> regex = false//匹配正则终点
@ -327,8 +334,7 @@ class RuleAnalyzer(data: String, code:Boolean = false) {
return if (pos > st) { return if (pos > st) {
startX = start startX = start
splitRule() //首段已匹配,但当前段匹配未完成,调用二段匹配 splitRule() //首段已匹配,但当前段匹配未完成,调用二段匹配
} } else { //执行到此,证明后面再无分隔字符
else { //执行到此,证明后面再无分隔字符
rule += queue.substring(pos) //将剩余字段压入数组末尾 rule += queue.substring(pos) //将剩余字段压入数组末尾
rule rule
} }
@ -388,8 +394,7 @@ class RuleAnalyzer(data: String, code:Boolean = false) {
return if (pos > st) { return if (pos > st) {
startX = start startX = start
splitRule() //首段已匹配,但当前段匹配未完成,调用二段匹配 splitRule() //首段已匹配,但当前段匹配未完成,调用二段匹配
} } else { //执行到此,证明后面再无分隔字符
else { //执行到此,证明后面再无分隔字符
rule += queue.substring(pos) //将剩余字段压入数组末尾 rule += queue.substring(pos) //将剩余字段压入数组末尾
rule rule
} }
@ -425,7 +430,12 @@ class RuleAnalyzer(data: String, code:Boolean = false) {
* @param fr 查找到内嵌规则时用于解析的函数 * @param fr 查找到内嵌规则时用于解析的函数
* *
* */ * */
fun innerRule( inner:String,startStep:Int = 1,endStep:Int = 1,fr:(String)->String?): String { fun innerRule(
inner: String,
startStep: Int = 1,
endStep: Int = 1,
fr: (String) -> String?
): String {
val start0 = pos //规则匹配前起点 val start0 = pos //规则匹配前起点
@ -455,39 +465,6 @@ class RuleAnalyzer(data: String, code:Boolean = false) {
} }
} }
// /**
// * 匹配并返回标签中的属性键字串(字母、数字、-、_、:)
// * @return 属性键字串
// */
// fun consumeAttributeKey(start:Int = pos): String {
// while (!isEmpty && (Character.isLetterOrDigit(queue[pos]) || matchesAny('-', '_', ':'))) pos++
// return queue.substring(start, pos)
// }
// fun splitRule(query:String,item:String = "other",listItem:String = "allInOne"):String{
//
// val cuurItem = item //当前项类型,list->列表项 mulu->章节列表项 url->链接项 search->搜索链接项 find发现链接列表项 other->其他项
// val cuurList = listItem//当前界面总列表项类型,allInOne,json,xml,kotin,java
// var Reverse = false //是否反转列表
//
// consumeWhitespace() //消耗开头空白
// var fisrt = consume() //拉出并消费首字符
//
// when(item){
// "search" ->
// "find" ->
// "mulu" -> if(fisrt == '-'){
// Reverse=true //开启反转
// consumeWhitespace() //拉出所有空白符
// fisrt = consume() //首字符后移
// }
// else ->
//
// }
//
// return query
// }
companion object { companion object {
/** /**
* 转义字符 * 转义字符

Loading…
Cancel
Save