Pereiti prie turinio

Module:Check for unknown parameters

Puslapis iš Vikipedijos, laisvosios enciklopedijos.

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

-- This module may be used to compare the arguments passed to the parent
-- with a list of arguments, returning a specified result if an argument is
-- not on the list
localp={}

localfunctiontrim(s)
returns:match('^%s*(.-)%s*$')
end

localfunctionisnotempty(s)
returnsandtrim(s)~=''
end

functionp.check(frame)
localargs=frame.args
localpargs=frame:getParent().args
localignoreblank=isnotempty(frame.args['ignoreblank'])
localcheckpos=isnotempty(frame.args['checkpositional'])
localknownargs={}
localunknown=frame.args['unknown']or'Found _VALUE_, '
localpreview=frame.args['preview']

localres={}
localregexps={}
localcomments={}
localcommentstr=''

localispreview=frame:preprocess("{{REVISIONID}}")==""and1or0

-- create the list of known args, regular expressions, and the return string
fork,vinpairs(args)do
iftype(k)=='number'then
v=trim(v)
knownargs[v]=1
elseifk:find('^regexp[1-9][0-9]*$')then
table.insert(regexps,'^'..v..'$')
end
end
ifisnotempty(preview)then
preview='<div class= "hatnote" style= "color:red" ><strong>Warning:</strong> '..preview..' (this message is shown only in preview).</div>'
elseifpreview==nilthen
preview=unknown
end
ifispreview==1then
unknown=preview
ignoreblank=false
end

-- adds one result to the output tables
localfunctionaddresult(k)
ifk==''then
-- Fix odd bug for | = which gets stripped to the empty string and
-- breaks category links
k=' '
end
localr=unknown:gsub('_VALUE_',k)
table.insert(res,r)
table.insert(comments,' "'..k..' "')
end

-- loop over the parent args, and make sure they are on the list
fork,vinpairs(pargs)do
iftype(k)=='string'andknownargs[k]==nilthen
localknownflag=false
fori,regexpinipairs(regexps)do
ifmw.ustring.match(k,regexp)then
knownflag=true
break
end
end
ifnotknownflagand(notignoreblankorisnotempty(v))then
k=mw.ustring.gsub(k,'[^%w\-_ ]','?')
addresult(k)
end
elseifcheckposand
type(k)=='number'and
knownargs[tostring(k)]==niland
(notignoreblankorisnotempty(v))
then
addresult(k)
end
end

if#comments>0then
commentstr='<!-- Module:Check for unknown parameters results: '..
table.concat(comments,', ')..'-->'
end

returntable.concat(res)..commentstr
end

returnp