跳转到内容

模組:IfUtil

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2020年3月3日 (二) 14:00编辑。这可能和当前版本存在着巨大的差异。

local p={}
local lib_arg={}
local yesno = require('Module:Yesno')

function p.noTrim(frame)--{{#invoke:IfUtil|noTrim|expr|5>3|Yes|No}}
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local command = mw.ustring.lower(mw.text.trim(args['1'] or args[1] or ''))
    local if_table = require('Module:Var')._seek_arg(args,1)
    if command == 'if' or command == '' or command == '#if' then
    	return p.ifnoTrim(if_table)
    elseif command == 'ifexpr' or command == 'expr' or command == '#ifexpr' then
    	return p.ifexprnoTrim(if_table)
    elseif command == 'ifeq' or command == 'eq' or command == '#ifeq' then
    	if yesno(mw.text.trim(args.trim or 'yes')) then
    		return working_frame:callParserFunction{ name = '#ifeq:' .. mw.text.trim(args['2'] or args[2] or ''), args = {
			    mw.text.trim(args['3'] or args[3] or ''), 
			    args['4'] or args[4] or '', 
			    args['5'] or args[5] or ''
			} }
    	else
    		if (args['2'] or args[2] or '') == (args['3'] or args[3] or '') then return args['4'] or args[4] or ''
    		else return args['5'] or args[5] or '' end
    	end
    elseif command == 'iferror' or command == 'error' or command == '#iferror' then
	    local output_error = args['3'] or args[3] or ''
	    local output_success = args['4'] or args[4]
    	if not output_success then output_success = args['2'] or args[2] or '' end
    	local result = working_frame:callParserFunction{ name = '#iferror:' .. mw.text.trim(args['2'] or args[2] or ''), args = {'error', 'success'} }
    	result = mw.ustring.lower(result)
    	if result == 'success' then return output_success end
    	return output_error
    elseif command == 'ifexist' or command == 'exist' or command == '#ifexist' then
	    local output_exist = args['3'] or args[3]
	    local output_no = args['4'] or args[4] or ''
    	if not output_exist then output_exist = args['2'] or args[2] or '' end
    	local result = working_frame:callParserFunction{ name = '#ifexist:' .. mw.text.trim(args['2'] or args[2] or ''), args = {'1', '0'} }
    	result = mw.ustring.lower(result)
    	if result == '1' then return output_exist end
    	return output_no
    else return require('Module:Template invocation').invocation( (mw.isSubsting() and 'safesubst:' or '') .. (args['1'] or args[1] or 'void'), if_table)
    end
    return ''
end

function p.ifexprnoTrim(frame)--{{#invoke:IfUtil|ifexprnoTrim|5>3|Yes|No}}
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_str = args['1'] or args[1] or '0'
    local output_if = args['2'] or args[2] or ''
    local check_max_arg = 2
    local ifexpr_table = {}
    for k,v in pairs(args) do
    	local check_num = tonumber(mw.text.trim(k))
    	if check_num and check_num > check_max_arg then
    		check_max_arg = check_num
    	end
    	ifexpr_table[k] = v
    end
    ifexpr_table['1'] = input_str
    ifexpr_table['2'] = output_if
    local tail = check_max_arg - check_max_arg % 2
    local output_else = (check_max_arg % 2 == 1) and (args[tostring(check_max_arg)] or args[check_max_arg]) or ''
    for i=1,tail,2 do
    	local expr = ifexpr_table[tostring(i)] or ifexpr_table[i] or '0'
    	local true_result = args[tostring(i+1)] or args[i+1] or ''
    	local flag,ifexpr = xpcall(function() return mw.ext.ParserFunctions.expr(expr)end,function()end)
    	--use original #ifexpr: to print original error
    	if flag~=true then return working_frame:preprocess("{{safesubst:#ifexpr:" .. expr .. '|' .. true_result .. '|' .. output_else .."}}") end
    	if (tonumber(ifexpr or'0') or 0) ~= 0 then return true_result end
    end
    return output_else
end

function p.ifnoTrim(frame) 
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_str = args['1'] or args[1] or ''
    local output_if = args['2'] or args[2] or ''
    local output_else = args['3'] or args[3] or ''
    if mw.text.trim(input_str) ~= '' then
    	return output_if
    end
    return output_else
end
function p.if2number(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_str = args['1'] or args[1] or ''
    local output_hasTwo = args['2'] or args[2] or '1'
    local output_otherwise = args['3'] or args[3] or ''
    local check_split = args['4'] or args[4] or '~'
    local local_check = mw.text.trim(require('Module:TemplateParameters').containsNumber({input_str}))
    if local_check ~= '' then
    	local split_end, split2_first = mw.ustring.find(input_str,check_split)
    	local first_num_str, second_num_str = '', ''
    	if split_end then first_num_str = mw.ustring.sub(input_str,1,split_end-1) end
    	if split2_first then second_num_str = mw.ustring.sub(input_str,split2_first+1,-1) end
    	local first_num_check, second_num_check = mw.text.trim(require('Module:TemplateParameters').containsNumber({first_num_str})),
    		mw.text.trim(require('Module:TemplateParameters').containsNumber({second_num_str}))
    	if first_num_check ~= '' and second_num_check ~= '' then
    		first_num_check, second_num_check = require('Module:TemplateParameters').getNumberValue({first_num_str}),
    			require('Module:TemplateParameters').getNumberValue({second_num_str})
    		local first_num, second_num = tonumber(first_num_check), tonumber(second_num_check)
    		if first_num and second_num then
    			if first_num ~= second_num then
    				return output_hasTwo
				else return output_otherwise end
			else return output_otherwise end
    	else return output_otherwise end
    else return output_otherwise end
end
return p