Module:Check for unknown parameters

From WikiProjectMed
Jump to navigation Jump to search

Documentation for this module may be created at Module:Check for unknown parameters/doc

  1 -- This module may be used to compare the arguments passed to the parent
  2 -- with a list of arguments, returning a specified result if an argument is
  3 -- not on the list
  4 local p = {}
  5 
  6 local function trim(s)
  7 	return s:match('^%s*(.-)%s*$')
  8 end
  9 
 10 local function isnotempty(s)
 11 	return s and s:match('%S')
 12 end
 13 
 14 local function clean(text)
 15 	-- Return text cleaned for display and truncated if too long.
 16 	-- Strip markers are replaced with dummy text representing the original wikitext.
 17 	local pos, truncated
 18 	local function truncate(text)
 19 		if truncated then
 20 			return ''
 21 		end
 22 		if mw.ustring.len(text) > 25 then
 23 			truncated = true
 24 			text = mw.ustring.sub(text, 1, 25) .. '...'
 25 		end
 26 		return mw.text.nowiki(text)
 27 	end
 28 	local parts = {}
 29 	for before, tag, remainder in text:gmatch('([^\127]*)\127[^\127]*%-(%l+)%-[^\127]*\127()') do
 30 		pos = remainder
 31 		table.insert(parts, truncate(before) .. '<' .. tag .. '>...</' .. tag .. '>')
 32 	end
 33 	table.insert(parts, truncate(text:sub(pos or 1)))
 34 	return table.concat(parts)
 35 end
 36 
 37 function p._check(args, pargs)
 38 	if type(args) ~= "table" or type(pargs) ~= "table" then
 39 		-- TODO: error handling
 40 		return
 41 	end
 42 
 43 	-- create the list of known args, regular expressions, and the return string
 44 	local knownargs = {}
 45 	local regexps = {}
 46 	for k, v in pairs(args) do
 47 		if type(k) == 'number' then
 48 			v = trim(v)
 49 			knownargs[v] = 1
 50 		elseif k:find('^regexp[1-9][0-9]*$') then
 51 			table.insert(regexps, '^' .. v .. '$')
 52 		end
 53 	end
 54 
 55 	-- loop over the parent args, and make sure they are on the list
 56 	local ignoreblank = isnotempty(args['ignoreblank'])
 57 	local showblankpos = isnotempty(args['showblankpositional'])
 58 	local values = {}
 59 	for k, v in pairs(pargs) do
 60 		if type(k) == 'string' and knownargs[k] == nil then
 61 			local knownflag = false
 62 			for _, regexp in ipairs(regexps) do
 63 				if mw.ustring.match(k, regexp) then
 64 					knownflag = true
 65 					break
 66 				end
 67 			end
 68 			if not knownflag and ( not ignoreblank or isnotempty(v) )  then
 69 				table.insert(values, clean(k))
 70 			end
 71 		elseif type(k) == 'number' and
 72 			knownargs[tostring(k)] == nil and
 73 			( showblankpos or isnotempty(v) )
 74 		then
 75 			table.insert(values, k .. ' = ' .. clean(v))
 76 		end
 77 	end
 78 
 79 	-- add results to the output tables
 80 	local res = {}
 81 	if #values > 0 then
 82 		local unknown_text = args['unknown'] or 'Found _VALUE_, '
 83 
 84 		if mw.getCurrentFrame():preprocess( "{{REVISIONID}}" ) == "" then
 85 			local preview_text = args['preview']
 86 			if isnotempty(preview_text) then
 87 				preview_text = require('Module:If preview')._warning({preview_text})
 88 			elseif preview == nil then
 89 				preview_text = unknown_text
 90 			end
 91 			unknown_text = preview_text
 92 		end
 93 		for _, v in pairs(values) do
 94 			-- Fix odd bug for | = which gets stripped to the empty string and
 95 			-- breaks category links
 96 			if v == '' then v = ' ' end
 97 
 98 			-- avoid error with v = 'example%2' ("invalid capture index")
 99 			local r = unknown_text:gsub('_VALUE_', {_VALUE_ = v})
100 			table.insert(res, r)
101 		end
102 	end
103 
104 	return table.concat(res)
105 end
106 
107 function p.check(frame)
108 	local args = frame.args
109 	local pargs = frame:getParent().args
110 	return p._check(args, pargs)
111 end
112 
113 return p