模块:Check for clobbered parameters

来自互联网公墓
Yzqzss讨论 | 贡献2021年8月9日 (一) 19:25的版本 (导入1个版本)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

此模块的文档可以在模块:Check for clobbered parameters/doc创建

local p = {}

local function trim(s)
	return s:match('^%s*(.-)%s*$')
end

local function isnotempty(s)
	return s and s:match('%S')
end

function p.check(frame)
	local args = frame.args
	local pargs = frame:getParent().args
	local checknested = isnotempty(args['nested'])
	local delimiter = isnotempty(args['delimiter']) and args['delimiter'] or ';'
	local cat = ''
	if args['cat'] and mw.ustring.match(args['cat'],'^[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]:') then
		cat = args['cat']
	end
	local res = ''

	local argpairs = {}
	for k, v in pairs(args) do
		if type(k) == 'number' then
			local plist = mw.text.split(v, delimiter)
			local pfound = {}
			local count = 0
			for ii, vv in ipairs(plist) do
				vv = trim(vv)
				if checknested and pargs[vv] or isnotempty(pargs[vv]) then
					count = count + 1
					table.insert(pfound, vv)
				end
			end
			if count > 1 then
				table.insert(argpairs, pfound)
			end
		end
	end
	
	local warnmsg = {}
	if #argpairs > 0 then
		for i, v in ipairs( argpairs ) do
			table.insert(warnmsg, '页面在以下列出的参数中使用了多过一个参数:<code>' ..
				table.concat(v, '</code>, <code>') .. '</code>')
			if cat ~= '' then
				res = res .. '[[' .. cat .. '|' .. (v[1] == '' and ' ' or '') .. v[1] .. ']]'
			end	
		end
	end
	
	if #warnmsg > 0 then
		if frame:preprocess( "{{REVISIONID}}" ) == "" then
			local ptxt = args['template'] and args['template'] .. ' 警告' or '警告'
			res = '<div class="hatnote" style="color:red"><strong>' .. ptxt .. ':</strong> ' .. table.concat(warnmsg, '<br>') .. '</div>' .. res
		end
	end
	
	return res
end

return p