✨ (engine): 在本机直接执行 Legado 书源规则(CSS/XPath/JSONPath/正则/URL 构造)

This commit is contained in:
garfield committed 2026-12-04 00:33:36 +08:00
1 parent 485d0160a1
commit f73f9c4f39
13 files changed
+4503

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
--[[--
moonsource_legadorule.parser 离线用例:搜索、详情、目录翻页、正文翻页与取消,http 走脚本化桩。
@module tests.legadorule.parser_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch
local Assert = require("support.assert")
local pages, calls = {}, {}
package.preload["http.request"] = function()
local function respond(url, cb)
calls[#calls + 1] = url
local body = pages[url]
if body == nil then return cb(nil, "HTTP 404", { code = 404 }) end
cb(body, nil, { code = 200 })
end
return {
header = function() return nil end,
get = function(url, _opts, cb) respond(url, cb) return { cancel = function() end } end,
post = function(url, _body, _opts, cb) respond(url, cb) return { cancel = function() end } end,
}
end
package.preload["http.cookie_jar"] = function() return { new = function() return {} end } end
local Parser = require("moonsource_legadorule.parser")
local Client = require("moonsource_legadorule.client")
local rule = {
bookSourceUrl = "https://a.com",
searchUrl = "/s?q={{key}}",
ruleSearch = { bookList = ".r li", name = "a@text", bookUrl = "a@href", author = "span@text##作者:" },
ruleBookInfo = { name = "h1@text", intro = ".intro@text", tocUrl = ".toc@href" },
ruleToc = { chapterList = ".c a", chapterName = "text", chapterUrl = "href", nextTocUrl = ".next@href" },
ruleContent = { content = "#t@html", nextContentUrl = ".np@href" },
}
local function wait(fn)
local a, b
fn(function(x, y) a, b = x, y end)
return a, b
end
pages["https://a.com/s?q=%E6%96%97"] = '<ul class="r"><li><a href="/b/1">书名:斗破</a><span>作者:天蚕</span></li>'
.. '<li><a href="/b/1">重复</a></li><li><a href="/b/2">二</a></li></ul>'
pages["https://a.com/b/1"] = '<h1>斗破</h1><div class="intro">简介</div><a class="toc" href="/t/1">目录</a>'
pages["https://a.com/t/1"] = '<div class="c"><a href="/c/1">一</a><a href="/c/2">二</a></div><a class="next" href="/t/2">下页</a>'
pages["https://a.com/t/2"] = '<div class="c"><a href="/c/3">三</a></div><a class="next" href="/t/1">上页</a>'
pages["https://a.com/c/1"] = '<div id="t">第一段<br>第二段</div><a class="np" href="/c/1_2">下页</a>'
pages["https://a.com/c/1_2"] = '<div id="t">第三段</div><a class="np" href="/c/2">下章</a>'
local P = Parser.new(rule, Client.new(rule))
-- 搜索:书名前缀、作者前缀被清理,同书地址去重。
local books, err = wait(function(cb) P:search("斗", 1, cb) end)
Assert.is_nil(err)
Assert.len(books, 2)
Assert.eq(books[1].name, "斗破")
Assert.eq(books[1].author, "天蚕")
Assert.eq(books[1].bookUrl, "https://a.com/b/1")
-- 详情 → 目录:跟随 nextTocUrl,遇到回环停止。
local book = wait(function(cb) P:detail(books[1].bookUrl, books[1], cb) end)
Assert.eq(book.tocUrl, "https://a.com/t/1")
Assert.eq(book.intro, "简介")
local chapters = wait(function(cb) P:chapters(book, cb) end)
Assert.len(chapters, 3)
Assert.eq(chapters[3].url, "https://a.com/c/3")
-- 正文:拼接分页,遇到下一章地址停止。
local content = wait(function(cb) P:content(chapters[1], book, chapters[2].url, cb) end)
Assert.eq(content.text, "第一段\n第二段\n第三段")
-- 错误如实上报:404 与 JS 规则。
local _, e = wait(function(cb) P:content({ title = "x", url = "https://a.com/none", baseUrl = "https://a.com", isVolume = false }, book, nil, cb) end)
Assert.matches(e, "获取网页内容失败")
local js = Parser.new({ bookSourceUrl = "https://a.com", searchUrl = "/s?q={{java.md5(key)}}" }, Client.new({}))
local _r, e2 = wait(function(cb) js:search("x", 1, cb) end)
Assert.matches(e2, "JS/WebView")
+76
View File
@@ -0,0 +1,76 @@
--[[--
moonsource_legadorule.rule / url 离线用例:各规则模式、@put/@get、正则替换、JS 拒绝、URL 补全与构造。
@module tests.legadorule.rule_spec
--]]
---@diagnostic disable: undefined-field, missing-fields, redundant-parameter, missing-parameter, inject-field, assign-type-mismatch, param-type-mismatch
local Assert = require("support.assert")
local Rule = require("moonsource_legadorule.rule")
local Url = require("moonsource_legadorule.url")
local Unsupported = require("moonsource_legadorule.unsupported")
local HTML = '<div class="lis"><ul><li><a href="/b/1">书一</a><span>甲</span></li><li><a href="/b/2">书二</a></li></ul></div>'
-- 默认 JSoup 模式:列表、文本、绝对链接、下标、正则替换。
do
local an = Rule.new():setContent(HTML, "http://x.com/")
local items = an:getElements(".lis li")
Assert.len(items, 2)
local item = Rule.new():setContent(items[1], "http://x.com/")
Assert.eq(item:getString("a@text"), "书一")
Assert.eq(item:getString("a@href", nil, true), "http://x.com/b/1")
Assert.eq(an:getString("li.1@a@text"), "书二")
Assert.eq(an:getString("li.0@a@text##书##本"), "本一")
Assert.eq(an:getString("tag.a!0@text"), "书二")
end
-- XPath 与 JsonPath。
do
local an = Rule.new():setContent(HTML, "http://x.com/")
Assert.len(an:getElements("//li"), 2)
Assert.eq(an:getString("//li[2]/a/text()"), "书二")
local js = Rule.new():setContent('{"a":{"list":[{"n":"x","v":1},{"n":"y","v":2}],"t":"hi"}}', "http://x.com/")
Assert.eq(js:getString("$.a.t"), "hi")
Assert.eq(js:getString("$.a.list[-1].v"), "2")
local rows = js:getElements("$.a.list[?(@.v>1)]")
Assert.len(rows, 1)
Assert.eq(Rule.new():setContent(rows[1]):getString("$.n"), "y")
Assert.eq(js:getString("$..n"), "x\ny")
end
-- 正则列表模式与 $n 组引用;@put / @get。
do
local an = Rule.new():setContent("a1:一;a2:二;", "http://x.com/")
local rows = an:getElements(":a(\\d):(.)")
Assert.len(rows, 2)
Assert.eq(Rule.new():setContent(rows[2]):getString("$1-$2"), "2-二")
local put = Rule.new():setContent(HTML)
put:getString("li.0@a@text@put:{k:\"li.1@a@text\"}")
Assert.eq(put:get("k"), "书二")
end
-- JS / WebView 规则一律拒绝。
do
Assert.errors(function() Rule.new():setContent(HTML):getString("@js:result") end)
local ok, err = pcall(Rule.new():setContent(HTML).getString, Rule.new():setContent(HTML), "<js>1</js>")
Assert.is_false(ok)
Assert.eq(Unsupported.message(err), Unsupported.MESSAGE)
end
-- URL 补全与构造。
do
Assert.eq(Url.absolute("https://a.com/x/y.html", "../z.html"), "https://a.com/z.html")
Assert.eq(Url.absolute("https://a.com/x/", "//b.com/p"), "https://b.com/p")
Assert.eq(Url.absolute("https://a.com/x/", "javascript:void(0)"), "")
local spec = Url.build('/s,{"body":"kw={{key}}&p={{page}}","method":"POST"}', { sourceUrl = "https://a.com/", key = "你好", page = 2 })
Assert.eq(spec.url, "https://a.com/s")
Assert.eq(spec.method, "POST")
Assert.eq(spec.body, "kw=%E4%BD%A0%E5%A5%BD&p=2")
Assert.eq(Url.build("/search/{{key}}", { sourceUrl = "https://a.com/", key = "我的 书" }).url,
"https://a.com/search/%E6%88%91%E7%9A%84%20%E4%B9%A6")
Assert.eq(Url.build("/s/%E6%88%91?q=我", { sourceUrl = "https://a.com/" }).url, "https://a.com/s/%E6%88%91?q=%E6%88%91")
Assert.eq(Url.build("/l/<a,b,c>.html", { sourceUrl = "https://a.com/", page = 9 }).url, "https://a.com/l/c.html")
Assert.errors(function() Url.build('/s,{"webView":true}', { sourceUrl = "https://a.com/" }) end)
Assert.errors(function() Url.build("/s?q={{java.get('x')}}", { sourceUrl = "https://a.com/" }) end)
end