Module:Parameters
Appearance
From Wikipedia, the free encyclopedia
This module is rated as alpha. It is ready for limited use and third-party feedback. It may be used on a small number of pages, but should be monitored closely. Suggestions for new features or adjustments to input and output are welcome.
This module depends on the following other modules:
Implements {{parameters }}
The above documentation is transcluded from Module:Parameters/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
-- This module implements [[Template:Parameters]]. -- [SublimeLinter luacheck-globals:mw] localDEFINITIONS={ alt={ code='<!-- text alternative for image; see WP:ALT -->', dlist='text alternative for image; see [[WP:ALT]]'}, coordinates={ code='<!-- use {{Coord}} -->', dlist='using {{tl|Coord}}'}, coords={ code='<!-- use {{Coord}} -->', dlist='using {{tl|Coord}}'}, native_name={ code='<!-- name in local language; if more than one, separate '.. 'using {{Plainlist}} use {{lang}}, and omit native_name_lang -->', dlist='name in local language; if more than one, separate '.. 'using {{tl|Plainlist}}, use {{tl|lang}}, and omit {{para|native_name_lang}}'}, native_name_lang={ code='<!-- language two- or three-letter ISO code -->', dlist='language two- or three-letter ISO code'}, start_date={ code='<!-- {{Start date|YYYY|MM|DD|df=y}} -->', dlist='use {{tlx|Start date|YYYY|MM|DD|df=y}}'}, end_date={ code='<!-- {{End date|YYYY|MM|DD|df=y}} -->', dlist='use {{tlx|Start date|YYYY|MM|DD|df=y}}'}, url={ code='<!-- use {{URL|example.com}} -->', dlist='using {{tl|URL}}'}, website={ code='<!-- use {{URL|example.com}} -->', dlist='using {{tls|URL|example.com}}'},} localp={} localremoveDuplicates=require('Module:TableTools').removeDuplicates localyesno=require('Module:Yesno') localfunctionmakeInvokeFunction(funcName) returnfunction(frame) localgetArgs=require('Module:Arguments').getArgs returnp[funcName](getArgs(frame,{removeBlanks=false})) end end localfunctionextractParams(page) localsource=mw.title.new(page,'Template'):getContent() localparameters={} forparameterinstring.gmatch(source,'{{{(.-)%f[}|<>]')do table.insert(parameters,parameter) end returnremoveDuplicates(parameters) end localfunctionmap(tbl,transform) localreturnTable={} fork,vinpairs(tbl)do returnTable[k]=transform(v) end returnreturnTable end localfunctionstrMap(tbl,transform) localreturnTable=map(tbl,transform) returntable.concat(returnTable) end functionp._check(args) localtitle=args.baseormw.title.getCurrentTitle().fullText returnstring.format( '{{#invoke:Check for unknown parameters|check|unknown='.. '[[Category:Pages using %s with unknown parameters]]|%s}}',title, table.concat(extractParams(args.base),'|')) end functionp._code(args) localdefinitions=yesno(args.definitions) localpad=yesno(args.pad) localparameters=extractParams(args.base) -- Space-pad the parameters to align the equal signs vertically ifpadthen locallengthPerPara=map(parameters,function(parameter) returnstring.len(parameter)end) -- Lua doesn't support printf's <*> to specify the width, apparently localfs=string.format('%%-%ss',math.max(unpack(lengthPerPara))) fori,parameterinpairs(parameters)do parameters[i]=string.format(fs,parameter) end end localtitle=args.baseormw.title.getCurrentTitle().baseText returnstring.format([[<nowiki>{{%s %s}}</nowiki>]],title,strMap(parameters, function(s) ifdefinitionsthen returnstring.format('| %s = %s\n',s, DEFINITIONS[s]andDEFINITIONS[s].codeor'') else returnstring.format('| %s = \n',s) end end)) end functionp._flatcode(args) localparameters=extractParams(args.base) localtitle=args.baseormw.title.getCurrentTitle().baseText returnstring.format(' {{tlp|%s%s}}',title,strMap(parameters, function(s) returnstring.format(' |%s{{=}}<var>%s</var>',s,s) end) ) end functionp._compare(args) localSet=require('Module:Set') localfunctionnormaliseParams(parameters) localparamsNorm={} -- Prepare a key lookup metatable, which will hold the original -- parameter names for each normalised parameter, e.g. -- [test] = {TEST, Test}. paramIndex functions like a Python -- defaultdict, where the default is a table. localparamIndex=setmetatable({},{__index=function(t,k) ifnotrawget(t,k)then rawset(t,k,{}) end returnrawget(t,k) end}) for_,parameterinpairs(parameters)do table.insert(paramsNorm, string.lower(string.gsub(parameter,'%A',''))) table.insert(paramIndex[ string.lower(string.gsub(parameter,'%A',''))],parameter) end paramsNorm=removeDuplicates(paramsNorm) -- Overload key lookup in paramsNorm. While [[Module:Set]] will -- operate on the table (which is to say, the normalised parameters -- array), key access will be by way of the paramIndex metatable. setmetatable(paramsNorm,{__index=paramIndex}) returnparamsNorm end localbaseParams=extractParams(args.base) localotherParams=extractParams(args.other) localbaseNormParams=normaliseParams(Set.valueComplement( otherParams,baseParams)) localotherNormParams=normaliseParams(otherParams) returnstring.format([[Identical: %s Similar: %s Disparate: %s]], strMap(Set.valueIntersection(baseParams,otherParams), function(v)returnstring.format('* %s\n',v)end), strMap(Set.valueIntersection(baseNormParams,otherNormParams), function(v)returnstring.format('* %s < %s [%s]\n', table.concat(baseNormParams[v],'; '), table.concat(otherNormParams[v],'; '), v) end), strMap(Set.valueComplement(otherNormParams,baseNormParams), function(v)returnstrMap(baseNormParams[v], function(s)returnstring.format('* %s\n',s)end) end)) end functionp._demo(args) localtitle=args.baseand('|_template='..args.base)or'' returnstring.format('{{Parameter names example%s|%s}}',title, table.concat(extractParams(args.base),'|')) end functionp._dlist(args) localdefinitions=yesno(args.definitions,true) localdefFormat='; %s: %s\n' localnonDefFormat='; %s: \n' ifargs._parathen defFormat='; {{para|%s}}: %s\n' nonDefFormat='; {{para|%s}}: \n' end returnstrMap(extractParams(args.base), function(s) ifdefinitionsthen returnstring.format(defFormat,s, DEFINITIONS[s]andDEFINITIONS[s].dlistor'') else returnstring.format(nonDefFormat,s) end end) end functionp._dlistpara(args) args._para=true returnp._dlist(args) end functionp._list(args) returnstrMap(extractParams(args.base), function(s)returnstring.format('* %s\n',s)end) end p.check=makeInvokeFunction('_check') p.code=makeInvokeFunction('_code') p.flatcode=makeInvokeFunction('_flatcode') p.compare=makeInvokeFunction('_compare') p.demo=makeInvokeFunction('_demo') p.dlist=makeInvokeFunction('_dlist') p.dlistpara=makeInvokeFunction('_dlistpara') p.list=makeInvokeFunction('_list') returnp