From 777e7a79e8e40d8604e72fd31688392695ace062 Mon Sep 17 00:00:00 2001 From: syomie Date: Thu, 3 Mar 2022 05:58:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A1=A5=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 补全对由||、&&、%%分割的规则生效 * 补全对尾部存在##替换规则生效 --- CHANGELOG.md | 13 +--- .../java/io/legado/app/help/RuleComplete.kt | 71 +++++++++---------- 2 files changed, 36 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f2faeedb..cff728e0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,4 @@ -**2022/02/27** +**2022/03/03** -* APP内编写规则时,对由XPath|JSOUP|CSS组成的规则进行简单的默认补全。 -* 对需求文本的获取text -* 对需求文本的img元素(以img结尾)的获取alt属性 -* 对需求链接的获取href属性 -* 对需求图片的获取src属性 -* 详情页预处理存在js/json/正则的不对详情页规则进行补全 -* 多条规则只补全最后一条规则 -* 书源编辑页点击调试/保存时补全开始生效 -* 注意:不改变编辑框内容显示,保存后再次编辑可查看补全后的规则,方便调试时快速修改规则 +* 补全对由||、&&、%%分割的规则生效 +* 补全对尾部存在##替换规则生效 diff --git a/app/src/main/java/io/legado/app/help/RuleComplete.kt b/app/src/main/java/io/legado/app/help/RuleComplete.kt index 5ac274aa0..ae0d73a54 100644 --- a/app/src/main/java/io/legado/app/help/RuleComplete.kt +++ b/app/src/main/java/io/legado/app/help/RuleComplete.kt @@ -2,69 +2,64 @@ package io.legado.app.help object RuleComplete { + // 需要补全 + private val needComplete =Regex( + """(?!=(@|/|^)(attr|text|ownText|textNodes|href|content|html|alt|all|value|src)(\(\))?)(?\&{2}|%%|\|{2}|$)""") - // 补全时忽略匹配规则 - private val completeIgnore = - Regex( - """\\n|##|@js:||@Json:|\$\.|(attr|text|ownText|textNodes|href|content|html|alt|all|value|src)(\(\)|##.*)?\s*$""", - RegexOption.MULTILINE - ) + // 不能补全 存在js/json/{{xx}}的复杂情况 + private val notComplete = Regex("""^:|^##|\{\{|@js:||@Json:|\$\.""") - // 补全时忽略匹配的规则(判断列表项和详情页预处理规则生效) - private val completeIgnorePreRule = Regex("""^:|##|@js:||@Json:|\$\.""") - - // 匹配从图片获取信息的规则 - private val imgComplete = Regex( - """(?<=(tag\.|[+/@~>| &]))img[@/]text(\(\))?$|^img[@/]text(\(\))?$""", - RegexOption.IGNORE_CASE - ) + // 修正从图片获取信息 + private val fixImgInfo = Regex("""(?<=(^|tag\.|[\+/@>~| &]))img(?\[@.+\]|\.[-\w]+)?[@/]+text(\(\))?(?\&{2}|%%|\|{2}|$)""") + private val isXpath= Regex("^//|^@Xpath:") /** * 对简单规则进行补全,简化部分书源规则的编写 - * 该方法仅对对JSOUP/XPath/CSS规则生效 + * 对JSOUP/XPath/CSS规则生效 * @author 希弥 * @return 补全后的规则 或 原规则 - * @param rule 需要补全的规则 - * @param preRule 预处理规则 - * 用于分析详情页预处理规则 + * @param rules 需要补全的规则 + * @param preRule 预处理规则或列表规则 * @param type 补全结果的类型,可选的值有: * 1 文字(默认) * 2 链接 * 3 图片 */ fun autoComplete( - rule: String?, + rules: String?, preRule: String? = null, type: Int = 1 ): String? { - if (rule.isNullOrEmpty() || rule.contains(completeIgnore) - || preRule?.contains(completeIgnorePreRule) == true - ) { - return rule + if (rules.isNullOrEmpty()||rules.contains(notComplete) || preRule?.contains(notComplete) ?: false){ + return rules } + // 分离正则 + val regexSplit=rules.split("##".toRegex(),2) + val cleanedRule=regexSplit[0] + val regexRule=if (regexSplit.size>1) "##"+regexSplit[1] else "" + val textRule: String val linkRule: String val imgRule: String val imgText: String - if (rule.contains(Regex("/[^@]+$"))) { - textRule = "/text()" - linkRule = "/@href" - imgRule = "/@src" - imgText = "img/@alt" + if (cleanedRule.contains(isXpath)){ + textRule = "//text()\${seq}" + linkRule = "//@href\${seq}" + imgRule = "//@src\${seq}" + imgText = "img\${at}/@alt\${seq}" } else { - textRule = "@text" - linkRule = "@href" - imgRule = "@src" - imgText = "img@alt" + textRule = "@text\${seq}" + linkRule = "@href\${seq}" + imgRule = "@src\${seq}" + imgText = "img\${at}@alt\${seq}" } - var ret: String = rule - when (type) { - 1 -> ret = rule.replace(Regex("$"), textRule).replace(imgComplete, imgText) - 2 -> ret = rule.replace(Regex("$"), linkRule) - 3 -> ret = rule.replace(Regex("$"), imgRule) + return when (type) { + 1 -> needComplete.replace(cleanedRule, textRule).replace(fixImgInfo, imgText) + regexRule + 2 -> needComplete.replace(cleanedRule, linkRule) + regexRule + 3 -> needComplete.replace(cleanedRule, imgRule) + regexRule + else -> rules } - return ret }