From 2af5ce705c3c118e1fe2349a17ff172f8443bb0e Mon Sep 17 00:00:00 2001 From: Celeter Date: Sat, 30 Nov 2019 23:50:01 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B9=A6=E6=BA=90?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/storage/OldRule.kt | 120 ++++++++++++------ 1 file changed, 78 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/OldRule.kt b/app/src/main/java/io/legado/app/help/storage/OldRule.kt index 36e134afa..f78aaa60b 100644 --- a/app/src/main/java/io/legado/app/help/storage/OldRule.kt +++ b/app/src/main/java/io/legado/app/help/storage/OldRule.kt @@ -9,6 +9,7 @@ import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonObject import io.legado.app.utils.readInt import io.legado.app.utils.readString +import io.legado.app.utils.readBool import java.util.regex.Pattern object OldRule { @@ -26,7 +27,7 @@ object OldRule { val jsonItem = jsonPath.parse(json.trim()) bookSourceUrl = jsonItem.readString("bookSourceUrl") ?: "" bookSourceName = jsonItem.readString("bookSourceName") ?: "" - bookSourceGroup = jsonItem.readString("bookSourceGroup") ?: "" + bookSourceGroup = jsonItem.readString("bookSourceGroup") loginUrl = jsonItem.readString("loginUrl") bookUrlPattern = jsonItem.readString("ruleBookUrlPattern") customOrder = jsonItem.readInt("serialNumber") ?: 0 @@ -35,56 +36,57 @@ object OldRule { exploreUrl = toNewUrls(jsonItem.readString("ruleFindUrl")) bookSourceType = if (jsonItem.readString("bookSourceType") == "AUDIO") BookType.audio else BookType.default + enabled = jsonItem.readBool("enable") ?: true if (exploreUrl.isNullOrBlank()) { enabledExplore = false } val searchRule = SearchRule( - bookList = jsonItem.readString("ruleSearchList"), - name = jsonItem.readString("ruleSearchName"), - author = jsonItem.readString("ruleSearchAuthor"), - intro = jsonItem.readString("ruleSearchIntroduce"), - kind = jsonItem.readString("ruleSearchKind"), - bookUrl = jsonItem.readString("ruleSearchNoteUrl"), - coverUrl = jsonItem.readString("ruleSearchCoverUrl"), - lastChapter = jsonItem.readString("ruleSearchLastChapter") + bookList = toNewRule(jsonItem.readString("ruleSearchList")), + name = toNewRule(jsonItem.readString("ruleSearchName")), + author = toNewRule(jsonItem.readString("ruleSearchAuthor")), + intro = toNewRule(jsonItem.readString("ruleSearchIntroduce")), + kind = toNewRule(jsonItem.readString("ruleSearchKind")), + bookUrl = toNewRule(jsonItem.readString("ruleSearchNoteUrl")), + coverUrl = toNewRule(jsonItem.readString("ruleSearchCoverUrl")), + lastChapter = toNewRule(jsonItem.readString("ruleSearchLastChapter")) ) ruleSearch = GSON.toJson(searchRule) val exploreRule = ExploreRule( - bookList = jsonItem.readString("ruleFindList"), - name = jsonItem.readString("ruleFindName"), - author = jsonItem.readString("ruleFindAuthor"), - intro = jsonItem.readString("ruleFindIntroduce"), - kind = jsonItem.readString("ruleFindKind"), - bookUrl = jsonItem.readString("ruleFindNoteUrl"), - coverUrl = jsonItem.readString("ruleFindCoverUrl"), - lastChapter = jsonItem.readString("ruleFindLastChapter") + bookList = toNewRule(jsonItem.readString("ruleFindList")), + name = toNewRule(jsonItem.readString("ruleFindName")), + author = toNewRule(jsonItem.readString("ruleFindAuthor")), + intro = toNewRule(jsonItem.readString("ruleFindIntroduce")), + kind = toNewRule(jsonItem.readString("ruleFindKind")), + bookUrl = toNewRule(jsonItem.readString("ruleFindNoteUrl")), + coverUrl = toNewRule(jsonItem.readString("ruleFindCoverUrl")), + lastChapter = toNewRule(jsonItem.readString("ruleFindLastChapter")) ) ruleExplore = GSON.toJson(exploreRule) val bookInfoRule = BookInfoRule( - init = jsonItem.readString("ruleBookInfoInit"), - name = jsonItem.readString("ruleBookName"), - author = jsonItem.readString("ruleBookAuthor"), - intro = jsonItem.readString("ruleIntroduce"), - kind = jsonItem.readString("ruleBookKind"), - coverUrl = jsonItem.readString("ruleCoverUrl"), - lastChapter = jsonItem.readString("ruleBookLastChapter"), - tocUrl = jsonItem.readString("ruleChapterUrl") + init = toNewRule(jsonItem.readString("ruleBookInfoInit")), + name = toNewRule(jsonItem.readString("ruleBookName")), + author = toNewRule(jsonItem.readString("ruleBookAuthor")), + intro = toNewRule(jsonItem.readString("ruleIntroduce")), + kind = toNewRule(jsonItem.readString("ruleBookKind")), + coverUrl = toNewRule(jsonItem.readString("ruleCoverUrl")), + lastChapter = toNewRule(jsonItem.readString("ruleBookLastChapter")), + tocUrl = toNewRule(jsonItem.readString("ruleChapterUrl")) ) ruleBookInfo = GSON.toJson(bookInfoRule) val chapterRule = TocRule( - chapterList = jsonItem.readString("ruleChapterList"), - chapterName = jsonItem.readString("ruleChapterName"), - chapterUrl = jsonItem.readString("ruleContentUrl"), - nextTocUrl = jsonItem.readString("ruleChapterUrlNext") + chapterList = toNewRule(jsonItem.readString("ruleChapterList")), + chapterName = toNewRule(jsonItem.readString("ruleChapterName")), + chapterUrl = toNewRule(jsonItem.readString("ruleContentUrl")), + nextTocUrl = toNewRule(jsonItem.readString("ruleChapterUrlNext")) ) ruleToc = GSON.toJson(chapterRule) - var content = jsonItem.readString("ruleBookContent") ?: "" + var content = toNewRule(jsonItem.readString("ruleBookContent")) ?: "" if (content.startsWith("$") && !content.startsWith("$.")) { content = content.substring(1) } val contentRule = ContentRule( content = content, - nextContentUrl = jsonItem.readString("ruleContentUrlNext") + nextContentUrl = toNewRule(jsonItem.readString("ruleContentUrlNext")) ) ruleContent = GSON.toJson(contentRule) } @@ -93,21 +95,55 @@ object OldRule { return source } - private fun toNewUrls(oldUrl: String?): String? { - if (oldUrl == null) return null - if (!oldUrl.contains("\n") && !oldUrl.contains("&&")) - return toNewUrl(oldUrl) - - val urls = oldUrl.split("(&&|\n)+".toRegex()) - var newUrl = "" - for (url in urls) { - newUrl += toNewUrl(url)?.replace("\\n\\s*".toRegex(), "") + "\n" + // default规则适配 + // #正则#替换内容 替换成 ##正则##替换内容 + // | 替换成 || + // & 替换成 && + private fun toNewRule(oldRule: String?): String? { + if (oldRule.isNullOrBlank()) return null + var newRule = oldRule + if (oldRule.startsWith("-")) { + newRule = oldRule.substring(1) + } + if (!newRule.startsWith("@CSS:", true) && + !newRule.startsWith("@XPath:", true) && + !newRule.startsWith("//") && + !newRule.startsWith("##") && + !newRule.startsWith(":") + ) { + if (newRule.contains("#") && !newRule.contains("##")) { + newRule = oldRule.replace("#", "##") + } + if (newRule.contains("|") && !newRule.contains("||")) { + if (newRule.contains("##")) { + if (newRule.split("##")[0].contains("|")) { + newRule = newRule.replace("|", "||") + } + } else { + newRule = newRule.replace("|", "||") + } + } + if (newRule.contains("&") && !newRule.contains("&&")) { + newRule = newRule.replace("&", "&&") + } } - return newUrl + return newRule + } + + private fun toNewUrls(oldUrls: String?): String? { + if (oldUrls.isNullOrBlank()) return null + if (!oldUrls.contains("\n") && !oldUrls.contains("&&")) + return toNewUrl(oldUrls) + + val urls = oldUrls.split("(&&|\n)+".toRegex()) + val newUrls = urls.map { + toNewUrl(it)?.replace("\n\\s*".toRegex(),"") + }.joinToString("\n") + return newUrls } private fun toNewUrl(oldUrl: String?): String? { - if (oldUrl == null) return null + if (oldUrl.isNullOrBlank()) return null var url: String = oldUrl if (oldUrl.startsWith("", true)) { url = url.replace("=searchKey", "={{key}}") From 76f8b020c4af4c29d67e119a7834bc7f983cf0ca Mon Sep 17 00:00:00 2001 From: Celeter Date: Sun, 1 Dec 2019 00:39:13 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/storage/OldRule.kt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/OldRule.kt b/app/src/main/java/io/legado/app/help/storage/OldRule.kt index f78aaa60b..126dcf076 100644 --- a/app/src/main/java/io/legado/app/help/storage/OldRule.kt +++ b/app/src/main/java/io/legado/app/help/storage/OldRule.kt @@ -102,7 +102,9 @@ object OldRule { private fun toNewRule(oldRule: String?): String? { if (oldRule.isNullOrBlank()) return null var newRule = oldRule + var reverse = false if (oldRule.startsWith("-")) { + reverse = true newRule = oldRule.substring(1) } if (!newRule.startsWith("@CSS:", true) && @@ -116,8 +118,12 @@ object OldRule { } if (newRule.contains("|") && !newRule.contains("||")) { if (newRule.contains("##")) { - if (newRule.split("##")[0].contains("|")) { - newRule = newRule.replace("|", "||") + var list = newRule.split("##") + if (list[0].contains("|")) { + newRule = list[0].replace("|", "||") + for (i in 1 until list.size - 1) { + newRule += "##" + list[i] + } } } else { newRule = newRule.replace("|", "||") @@ -127,6 +133,9 @@ object OldRule { newRule = newRule.replace("&", "&&") } } + if (reverse) { + newRule += "-" + } return newRule } From 06d3421716cecc026cf69dcfb21122f26cfd1f78 Mon Sep 17 00:00:00 2001 From: Celeter <48249130+Celeter@users.noreply.github.com> Date: Sun, 1 Dec 2019 00:55:25 +0800 Subject: [PATCH 3/3] Update OldRule.kt --- app/src/main/java/io/legado/app/help/storage/OldRule.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/OldRule.kt b/app/src/main/java/io/legado/app/help/storage/OldRule.kt index 126dcf076..715b07159 100644 --- a/app/src/main/java/io/legado/app/help/storage/OldRule.kt +++ b/app/src/main/java/io/legado/app/help/storage/OldRule.kt @@ -121,7 +121,7 @@ object OldRule { var list = newRule.split("##") if (list[0].contains("|")) { newRule = list[0].replace("|", "||") - for (i in 1 until list.size - 1) { + for (i in 1 until list.size) { newRule += "##" + list[i] } } @@ -206,4 +206,4 @@ object OldRule { return GSON.toJson(map) } -} \ No newline at end of file +}