模組:ConditionSubst
模块文档[创建]
您可能想要创建本Scribunto模块的文档。 编者可以在本模块的沙盒 (创建 | 镜像)和测试样例 (创建)页面进行实验。 请在/doc子页面中添加分类。本模块的子页面。 |
local p = {}
local ifs = {}
local gbframe = mw.getCurrentFrame()
local function yesno (val)
return require('Module:Yesno')(val, val)
end
local function tosubst (val)
local gsub = mw.ustring.gsub(val,'<!subst!(.*)>','{{safesubst:%1}}')
return (val ~= gsub) and gbframe:preprocess(gsub) or val
end
ifs['if'] = function (a)
return (not a or a == '') and true or false
end
function ifs.ifeq (a, b)
if not a or not b then
return 'err'
end
a = tonumber(a) or a
b = tonumber(b) or b
return (a == b) and true or false
end
function ifs.ifexpr (a)
return (gbframe:callParserFunction{ name = '#ifexpr', args = {a, true, false}}) and true or false
end
function ifs.ifexist (page)
return (gbframe:callParserFunction{ name = '#ifexist', args = {page, true, false}}) and true or false
end
function p.main (frame)
local args, type
if frame == gbframe then
args = frame.args
else
args = frame
if type(args) ~= type({}) then
args = {frame}
end
end
local yes = args['yes'] or ''
local no = args['no'] or ''
if ifs['if'](args[1]) then
if ifs['if'](tosubst(yesno(args[2]))) then
return yes
end
elseif args[1] == 'eq' then
if ifs.ifeq(tosubst(yesno(args[2])), tosubst(yesno(args[3]))) then
return yes
end
elseif args[1] == 'expr' then
if ifs.ifexpr(args[2]) then
return yes
end
elseif args[1] == 'exist' then
if ifs.ifexist(args[2]) then
return yes
end
else
return require('Module:Error')({'無效的值' .. args[1]})
end
if mw.isSubsting() then
return mw.text.trim(require('Module:Template invocation').invocation('safesubst:#invoke:ConditionSubst|main', args))
else
return no
end
end
return p