Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Dec 14, 2023
1 parent 87c930c commit e59de71
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 141 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/io/legado/app/model/Debug.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ object Debug {
log(debugSource, "︽目录页解析完成")
log(debugSource, showTime = false)
val toc = chapters.filter { !(it.isVolume && it.url.startsWith(it.title)) }
if (toc.isEmpty()) {
log(debugSource, "≡没有正文章节")
return@onSuccess
}
val nextChapterUrl = toc.getOrNull(1)?.url ?: toc.first().url
contentDebug(scope, bookSource, book, toc.first(), nextChapterUrl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class AnalyzeByJSonPath(json: Any) {
* 解决阅读”&&“、”||“与jsonPath支持的”&&“、”||“之间的冲突
* 解决{$.rule}形式规则可能匹配错误的问题,旧规则用正则解析内容含‘}’的json文本时,用规则中的字段去匹配这种内容会匹配错误.现改用平衡嵌套方法解决这个问题
* */
fun getString(rule: String): String? {
if (rule.isEmpty()) return null
fun getString(rule: String): String {
if (rule.isEmpty()) return ""
var result: String
val ruleAnalyzes = RuleAnalyzer(rule, true) //设置平衡组为代码平衡
val rules = ruleAnalyzes.splitRule("&&", "||")
Expand Down Expand Up @@ -57,7 +57,7 @@ class AnalyzeByJSonPath(json: Any) {
val textList = arrayListOf<String>()
for (rl in rules) {
val temp = getString(rl)
if (!temp.isNullOrEmpty()) {
if (temp.isNotEmpty()) {
textList.add(temp)
if (ruleAnalyzes.elementsType == "||") {
break
Expand Down Expand Up @@ -126,7 +126,7 @@ class AnalyzeByJSonPath(json: Any) {
return ctx.read(rule)
}

internal fun getList(rule: String): ArrayList<Any>? {
internal fun getList(rule: String): ArrayList<Any> {
val result = ArrayList<Any>()
if (rule.isEmpty()) return result
val ruleAnalyzes = RuleAnalyzer(rule, true) //设置平衡组为代码平衡
Expand All @@ -143,7 +143,7 @@ class AnalyzeByJSonPath(json: Any) {
val results = ArrayList<ArrayList<*>>()
for (rl in rules) {
val temp = getList(rl)
if (!temp.isNullOrEmpty()) {
if (temp.isNotEmpty()) {
results.add(temp)
if (temp.isNotEmpty() && ruleAnalyzes.elementsType == "||") {
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class AnalyzeByJSoup(doc: Any) {
* 合并内容列表,得到内容
*/
internal fun getString(ruleStr: String) =
if (ruleStr.isEmpty()) null
else getStringList(ruleStr).takeIf { it.isNotEmpty() }?.joinToString("\n")
if (ruleStr.isEmpty()) ""
else getStringList(ruleStr).takeIf { it.isNotEmpty() }?.joinToString("\n") ?: ""

/**
* 获取一个字符串
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import java.util.regex.Pattern
@Keep
object AnalyzeByRegex {

fun getElement(res: String, regs: Array<String>, index: Int = 0): List<String>? {
fun getElement(res: String, regs: Array<String>, index: Int = 0): List<String> {
var vIndex = index
val resM = Pattern.compile(regs[vIndex]).matcher(res)
if (!resM.find()) {
return null
return emptyList()
}
// 判断索引的规则是最后一个规则
return if (vIndex + 1 == regs.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ class AnalyzeByXPath(doc: Any) {
}
}

internal fun getElements(xPath: String): List<JXNode>? {
internal fun getElements(xPath: String): List<JXNode> {

if (xPath.isEmpty()) return null
if (xPath.isEmpty()) return emptyList()

val jxNodes = ArrayList<JXNode>()
val ruleAnalyzes = RuleAnalyzer(xPath)
val rules = ruleAnalyzes.splitRule("&&", "||", "%%")

if (rules.size == 1) {
return getResult(rules[0])
return getResult(rules[0]) ?: emptyList()
} else {
val results = ArrayList<List<JXNode>>()
for (rl in rules) {
val temp = getElements(rl)
if (!temp.isNullOrEmpty()) {
if (temp.isNotEmpty()) {
results.add(temp)
if (temp.isNotEmpty() && ruleAnalyzes.elementsType == "||") {
break
Expand Down Expand Up @@ -130,19 +130,19 @@ class AnalyzeByXPath(doc: Any) {
return result
}

fun getString(rule: String): String? {
fun getString(rule: String): String {
val ruleAnalyzes = RuleAnalyzer(rule)
val rules = ruleAnalyzes.splitRule("&&", "||")
if (rules.size == 1) {
getResult(rule)?.let {
return TextUtils.join("\n", it)
}
return null
return ""
} else {
val textList = arrayListOf<String>()
for (rl in rules) {
val temp = getString(rl)
if (!temp.isNullOrEmpty()) {
if (temp.isNotEmpty()) {
textList.add(temp)
if (ruleAnalyzes.elementsType == "||") {
break
Expand Down
Loading

0 comments on commit e59de71

Please sign in to comment.