From 148c060256657d4f0eafaaea1ce554695aeaa758 Mon Sep 17 00:00:00 2001 From: syomie Date: Sun, 27 Feb 2022 12:52:52 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E7=BC=96=E8=BE=91=E8=A7=84=E5=88=99?= =?UTF-8?q?=E6=97=B6=E5=AF=B9=E9=83=A8=E5=88=86=E8=A7=84=E5=88=99=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=AE=80=E5=8D=95=E8=A1=A5=E5=85=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * APP内编写规则时,对由XPath|JSOUP|CSS组成的规则进行简单的默认补全。 * 对需求文本的获取text * 对需求文本的img元素(以img结尾)的获取alt属性 * 对需求链接的获取href属性 * 对需求图片的获取src属性 * 详情页预处理存在js/json/正则的不对详情页规则进行补全 * 多条规则只补全最后一条规则 * 书源编辑页点击调试/保存时补全开始生效,注意:不改变编辑框内容显示,保存后再次编辑可查看修改后的规则,方便调试时快速修改规则 --- .../java/io/legado/app/help/ruleComplete.kt | 46 +++++++++++++ .../source/edit/BookSourceEditActivity.kt | 69 ++++++++++--------- 2 files changed, 81 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/ruleComplete.kt diff --git a/app/src/main/java/io/legado/app/help/ruleComplete.kt b/app/src/main/java/io/legado/app/help/ruleComplete.kt new file mode 100644 index 000000000..da04af7db --- /dev/null +++ b/app/src/main/java/io/legado/app/help/ruleComplete.kt @@ -0,0 +1,46 @@ +package io.legado.app.help + +// 补全时忽略匹配规则 +val completeIgnore=Regex("""##|@js:||@Json:|\$.|(text|ownText|textNodes|href|content|html|alt|all|value|src)(\(\)|##.*)?\s*$""") +// 匹配从图片获取信息的规则 +val imgComplete=Regex("""(?<=(tag\.|[\+/@~>\| \&]))img[@/]text(\(\))?$|^img[@/]text(\(\))?$""",RegexOption.IGNORE_CASE) +// 补全时忽略匹配的规则(仅对详情页预处理规则生效) +val completeIgnorePreRule=Regex("""^:|##|@js:||@Json:|\$.""") +/** + * 对简单规则进行补全,简化部分书源规则的编写 + * 该方法仅对对JSOUP/XPath/CSS规则生效 + * @author 希弥 + * @return 补全后的规则 或 原规则 + * @param rules 需要补全的规则 + * @param preRule 预处理规则 + * 用于分析详情页预处理规则 + * @param type 补全结果的类型,可选的值有: + * 1 文字(默认) + * 2 链接 + * 3 图片 + */ +fun ruleComplete(rule:String?,preRule:String?="",type:Int=1):String?{ + if (rule.isNullOrEmpty()||rule.contains(completeIgnore)||preRule?.contains(completeIgnorePreRule)?:false){ + return rule + } + var textRule:String + var linkRule:String + var imgRule:String + if (rule.contains(Regex("/[^@]+$"))){ + textRule="/text()" + linkRule="/@href" + imgRule="/@src" + }else{ + textRule="@text" + linkRule="@href" + imgRule="@src" + } + var ret:String=rule + when(type){ + 1 -> ret = rule.replace(Regex("$"),textRule).replace(imgComplete,"img@alt") + 2 -> ret = rule.replace(Regex("$"),linkRule) + 3 -> ret = rule.replace(Regex("$"),imgRule) + } + return ret +} + diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 804d534c4..14dd98f18 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -21,6 +21,7 @@ import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.* import io.legado.app.databinding.ActivityBookSourceEditBinding import io.legado.app.help.LocalConfig +import io.legado.app.help.ruleComplete import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.backgroundColor @@ -327,63 +328,63 @@ class BookSourceEditActivity : "searchUrl" -> source.searchUrl = it.value "checkKeyWord" -> searchRule.checkKeyWord = it.value "bookList" -> searchRule.bookList = it.value - "name" -> searchRule.name = it.value - "author" -> searchRule.author = it.value - "kind" -> searchRule.kind = it.value - "intro" -> searchRule.intro = it.value - "updateTime" -> searchRule.updateTime = it.value - "wordCount" -> searchRule.wordCount = it.value - "lastChapter" -> searchRule.lastChapter = it.value - "coverUrl" -> searchRule.coverUrl = it.value - "bookUrl" -> searchRule.bookUrl = it.value + "name" -> searchRule.name = ruleComplete(it.value) + "author" -> searchRule.author = ruleComplete(it.value) + "kind" -> searchRule.kind = ruleComplete(it.value) + "intro" -> searchRule.intro = ruleComplete(it.value) + "updateTime" -> searchRule.updateTime = ruleComplete(it.value) + "wordCount" -> searchRule.wordCount = ruleComplete(it.value) + "lastChapter" -> searchRule.lastChapter = ruleComplete(it.value) + "coverUrl" -> searchRule.coverUrl = ruleComplete(it.value,type=3) + "bookUrl" -> searchRule.bookUrl = ruleComplete(it.value,type=2) } } findEntities.forEach { when (it.key) { "exploreUrl" -> source.exploreUrl = it.value "bookList" -> exploreRule.bookList = it.value - "name" -> exploreRule.name = it.value - "author" -> exploreRule.author = it.value - "kind" -> exploreRule.kind = it.value - "intro" -> exploreRule.intro = it.value - "updateTime" -> exploreRule.updateTime = it.value - "wordCount" -> exploreRule.wordCount = it.value - "lastChapter" -> exploreRule.lastChapter = it.value - "coverUrl" -> exploreRule.coverUrl = it.value - "bookUrl" -> exploreRule.bookUrl = it.value + "name" -> exploreRule.name = ruleComplete(it.value) + "author" -> exploreRule.author = ruleComplete(it.value) + "kind" -> exploreRule.kind = ruleComplete(it.value) + "intro" -> exploreRule.intro = ruleComplete(it.value) + "updateTime" -> exploreRule.updateTime = ruleComplete(it.value) + "wordCount" -> exploreRule.wordCount = ruleComplete(it.value) + "lastChapter" -> exploreRule.lastChapter = ruleComplete(it.value) + "coverUrl" -> exploreRule.coverUrl = ruleComplete(it.value,type=3) + "bookUrl" -> exploreRule.bookUrl = ruleComplete(it.value,type=2) } } infoEntities.forEach { when (it.key) { - "init" -> bookInfoRule.init = it.value - "name" -> bookInfoRule.name = it.value - "author" -> bookInfoRule.author = it.value - "kind" -> bookInfoRule.kind = it.value - "intro" -> bookInfoRule.intro = it.value - "updateTime" -> bookInfoRule.updateTime = it.value - "wordCount" -> bookInfoRule.wordCount = it.value - "lastChapter" -> bookInfoRule.lastChapter = it.value - "coverUrl" -> bookInfoRule.coverUrl = it.value - "tocUrl" -> bookInfoRule.tocUrl = it.value + "init" -> bookInfoRule.init = it.value ?: "" + "name" -> bookInfoRule.name = ruleComplete(it.value,bookInfoRule.init) + "author" -> bookInfoRule.author = ruleComplete(it.value,bookInfoRule.init) + "kind" -> bookInfoRule.kind = ruleComplete(it.value,bookInfoRule.init) + "intro" -> bookInfoRule.intro = ruleComplete(it.value,bookInfoRule.init) + "updateTime" -> bookInfoRule.updateTime = ruleComplete(it.value,bookInfoRule.init) + "wordCount" -> bookInfoRule.wordCount = ruleComplete(it.value,bookInfoRule.init) + "lastChapter" -> bookInfoRule.lastChapter = ruleComplete(it.value,bookInfoRule.init) + "coverUrl" -> bookInfoRule.coverUrl = ruleComplete(it.value,bookInfoRule.init,3) + "tocUrl" -> bookInfoRule.tocUrl = ruleComplete(it.value,bookInfoRule.init,2) "canReName" -> bookInfoRule.canReName = it.value } } tocEntities.forEach { when (it.key) { "chapterList" -> tocRule.chapterList = it.value - "chapterName" -> tocRule.chapterName = it.value - "chapterUrl" -> tocRule.chapterUrl = it.value + "chapterName" -> tocRule.chapterName = ruleComplete(it.value) + "chapterUrl" -> tocRule.chapterUrl = ruleComplete(it.value,type=2) "isVolume" -> tocRule.isVolume = it.value "updateTime" -> tocRule.updateTime = it.value "isVip" -> tocRule.isVip = it.value "isPay" -> tocRule.isPay = it.value - "nextTocUrl" -> tocRule.nextTocUrl = it.value + "nextTocUrl" -> tocRule.nextTocUrl = ruleComplete(it.value,type=2) } } contentEntities.forEach { when (it.key) { - "content" -> contentRule.content = it.value - "nextContentUrl" -> contentRule.nextContentUrl = it.value + "content" -> contentRule.content = ruleComplete(it.value) + "nextContentUrl" -> contentRule.nextContentUrl = ruleComplete(it.value,type=2) "webJs" -> contentRule.webJs = it.value "sourceRegex" -> contentRule.sourceRegex = it.value "replaceRegex" -> contentRule.replaceRegex = it.value @@ -486,4 +487,4 @@ class BookSourceEditActivity : } } -} \ No newline at end of file +} From de41eac7a1a0f796cc40c4b3be0a0575e53e4e9d Mon Sep 17 00:00:00 2001 From: syomie Date: Sun, 27 Feb 2022 13:22:19 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0CHANGELOG.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 12 +++-- .../java/io/legado/app/help/ruleComplete.kt | 51 +++++++++---------- .../source/edit/BookSourceEditActivity.kt | 1 - 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cae673ed..f17e7d22a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ -**2021/08/09** +**2022/02/27** -1. 修复选择文字不能选择单个文字的bug -2. +* 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 da04af7db..3351f4b5a 100644 --- a/app/src/main/java/io/legado/app/help/ruleComplete.kt +++ b/app/src/main/java/io/legado/app/help/ruleComplete.kt @@ -1,11 +1,11 @@ package io.legado.app.help // 补全时忽略匹配规则 -val completeIgnore=Regex("""##|@js:||@Json:|\$.|(text|ownText|textNodes|href|content|html|alt|all|value|src)(\(\)|##.*)?\s*$""") -// 匹配从图片获取信息的规则 -val imgComplete=Regex("""(?<=(tag\.|[\+/@~>\| \&]))img[@/]text(\(\))?$|^img[@/]text(\(\))?$""",RegexOption.IGNORE_CASE) +val completeIgnore=Regex("""\\n|##|@js:||@Json:|\$.|(text|ownText|textNodes|href|content|html|alt|all|value|src)(\(\)|##.*)?\s*$""") // 补全时忽略匹配的规则(仅对详情页预处理规则生效) val completeIgnorePreRule=Regex("""^:|##|@js:||@Json:|\$.""") +// 匹配从图片获取信息的规则 +val imgComplete=Regex("""(?<=(tag\.|[\+/@~>\| \&]))img[@/]text(\(\))?$|^img[@/]text(\(\))?$""",RegexOption.IGNORE_CASE) /** * 对简单规则进行补全,简化部分书源规则的编写 * 该方法仅对对JSOUP/XPath/CSS规则生效 @@ -20,27 +20,26 @@ val completeIgnorePreRule=Regex("""^:|##|@js:||@Json:|\$.""") * 3 图片 */ fun ruleComplete(rule:String?,preRule:String?="",type:Int=1):String?{ - if (rule.isNullOrEmpty()||rule.contains(completeIgnore)||preRule?.contains(completeIgnorePreRule)?:false){ - return rule - } - var textRule:String - var linkRule:String - var imgRule:String - if (rule.contains(Regex("/[^@]+$"))){ - textRule="/text()" - linkRule="/@href" - imgRule="/@src" - }else{ - textRule="@text" - linkRule="@href" - imgRule="@src" - } - var ret:String=rule - when(type){ - 1 -> ret = rule.replace(Regex("$"),textRule).replace(imgComplete,"img@alt") - 2 -> ret = rule.replace(Regex("$"),linkRule) - 3 -> ret = rule.replace(Regex("$"),imgRule) - } - return ret + if (rule.isNullOrEmpty()||rule.contains(completeIgnore)||preRule?.contains(completeIgnorePreRule)?:false){ + return rule + } + var textRule:String + var linkRule:String + var imgRule:String + if (rule.contains(Regex("/[^@]+$"))){ + textRule="/text()" + linkRule="/@href" + imgRule="/@src" + }else{ + textRule="@text" + linkRule="@href" + imgRule="@src" + } + var ret:String=rule + when(type){ + 1 -> ret = rule.replace(Regex("$"),textRule).replace(imgComplete,"img@alt") + 2 -> ret = rule.replace(Regex("$"),linkRule) + 3 -> ret = rule.replace(Regex("$"),imgRule) + } + return ret } - diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 14dd98f18..044fb660c 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -486,5 +486,4 @@ class BookSourceEditActivity : } } } - } From 510ee30f448d637dab6a1a78ba10632407985b07 Mon Sep 17 00:00:00 2001 From: syomie Date: Sun, 27 Feb 2022 15:10:20 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dxpath=E8=8E=B7=E5=8F=96al?= =?UTF-8?q?t=E5=B1=9E=E6=80=A7=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/help/ruleComplete.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 3351f4b5a..2f608f33b 100644 --- a/app/src/main/java/io/legado/app/help/ruleComplete.kt +++ b/app/src/main/java/io/legado/app/help/ruleComplete.kt @@ -26,18 +26,21 @@ fun ruleComplete(rule:String?,preRule:String?="",type:Int=1):String?{ var textRule:String var linkRule:String var imgRule:String + var imgText:String if (rule.contains(Regex("/[^@]+$"))){ textRule="/text()" linkRule="/@href" imgRule="/@src" + imgText="img/@alt" }else{ textRule="@text" linkRule="@href" imgRule="@src" + imgText="img@alt" } var ret:String=rule when(type){ - 1 -> ret = rule.replace(Regex("$"),textRule).replace(imgComplete,"img@alt") + 1 -> ret = rule.replace(Regex("$"),textRule).replace(imgComplete,imgText) 2 -> ret = rule.replace(Regex("$"),linkRule) 3 -> ret = rule.replace(Regex("$"),imgRule) } From 2b3abdfdb267b60213ebb16cf8407b7a127391f2 Mon Sep 17 00:00:00 2001 From: syomie Date: Sun, 27 Feb 2022 18:36:03 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f17e7d22a..9f2faeedb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,4 +7,5 @@ * 对需求图片的获取src属性 * 详情页预处理存在js/json/正则的不对详情页规则进行补全 * 多条规则只补全最后一条规则 -* 书源编辑页点击调试/保存时补全开始生效,注意:不改变编辑框内容显示,保存后再次编辑可查看补全后的规则,方便调试时快速修改规则 +* 书源编辑页点击调试/保存时补全开始生效 +* 注意:不改变编辑框内容显示,保存后再次编辑可查看补全后的规则,方便调试时快速修改规则