51 lines
1.5 KiB
Lua
51 lines
1.5 KiB
Lua
--[[--
|
|
JS / WebView 规则一律拒绝:抛出带固定提示的错误表,由 parser 转成用户可见的错误。
|
|
|
|
@module moonsource_legadorule.unsupported
|
|
--]]
|
|
|
|
local Regex = require("moonsource_legadorule.regex")
|
|
|
|
local Unsupported = {}
|
|
|
|
Unsupported.MESSAGE = "该书源需要 JS/WebView,暂不支持"
|
|
|
|
local ErrMeta = { __tostring = function(e) return e.message end }
|
|
|
|
function Unsupported.fail()
|
|
error(setmetatable({ message = Unsupported.MESSAGE, unsupported = true }, ErrMeta), 0)
|
|
end
|
|
|
|
--- pcall 抛出的错误转成文字。
|
|
function Unsupported.message(err)
|
|
if type(err) == "table" and err.message then return err.message end
|
|
return tostring(err)
|
|
end
|
|
|
|
function Unsupported.isJsSnippet(text)
|
|
return Regex.test("(?i)<js\\b|@js:", text)
|
|
end
|
|
|
|
function Unsupported.throwIfJsSnippet(text)
|
|
if Unsupported.isJsSnippet(text) then Unsupported.fail() end
|
|
end
|
|
|
|
function Unsupported.isSelectorRule(rule)
|
|
rule = rule:gsub("^%s+", "")
|
|
return rule:sub(1, 1) == "@" or rule:sub(1, 2) == "$." or rule:sub(1, 2) == "$[" or rule:sub(1, 2) == "//"
|
|
end
|
|
|
|
function Unsupported.isJsExpression(expr)
|
|
expr = expr:gsub("^%s+", ""):gsub("%s+$", "")
|
|
if expr == "" then return false end
|
|
local compact = expr:gsub("%s+", "")
|
|
if Regex.test("(?i)^(key|keyword|page)([+\\-*/%]\\d+)?$", compact) then return false end
|
|
return not Unsupported.isSelectorRule(expr)
|
|
end
|
|
|
|
function Unsupported.throwIfJsExpression(expr)
|
|
if Unsupported.isJsExpression(expr) then Unsupported.fail() end
|
|
end
|
|
|
|
return Unsupported
|