Module:Sandbox/BrandonXLF/Module:Map params
Appearance
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module depends on the following other modules: |
{{#invoke:Map params|map}}
maps parameters received by a function to wikitext using |1=
or to a template call using |call=
.
Usage
[edit]{{#invoke:Map params|main|parameters}}
Parameters
[edit]Input
[edit]|prefixn=
- The prefix of the parameters to process. All registered prefixes will be checked to determine if a number is present in the provided arguments.|prefix1=
defaults to processing unnamed parameters.|extran=
- An extra parameter to pass to the wikitext or template being called. If the extra parameter is suffixed with a number (same as prefixed parameters but not checked to determine if a number is present), add the suffix(i)
. For example,style(i)
.|sparse=
- Set toyes
to support parameter lists with gaps. Defaults tono
.|optnum=
- Whether or not to support passing the first parameter without a number, i.e. allow it to be|prefix=
rather than|prefix1=
. Defaults toyes
.
Map to
[edit]|1=
- The wikitext to map parameters to. Parameters are substituted with their number removed, i.e.|itemn=
becomes|item=
etc. The parameter{{{i}}}
is replaced with the current index and{{{1}}}
is replaced with the current unnamed parameter (if applicable).|call=
- Template to call. Parameters are passed with their number removed, i.e.|itemn=
becomes|item=
etc. The parameter{{{i}}}
is the current index and{{{1}}}
is the current unnamed parameter (if applicable). Overrides|1=
.
Concatenation
[edit]|sep=
/|2=
- Separator to add between outputs. To preserve leading and tailing whitespace,|2=
can be used instead, or|1=
when|call=
is being used.|conj=
- Special separator to use before the final output. Outer spaces can be added with{{sp}}
or 
.
Advanced
[edit]|expand=
- Whether or not to expand templates, parser functions, extension tags, etc. after substituting parameters. This is most useful when|1=
is used without<nowiki>...</nowiki>
tags and can provide a slight performance advantage. Only relevant when using|1=
. Defaults toyes
.
See also
[edit]localp={}localyesno=require('Module:Yesno')-- Escape a string to add to a Lua patternlocalfunctionescPattern(s)returns:gsub('([%(%)%%%.%[%]%*%+%-%?%^%$])','%%%1')end-- Get the value of a prefixed argumentfunctiongetPrefixedArg(args,prefix,num,optNum)returnargs[prefix..num]or((num==1andoptNumandargs[prefix])ornil)end-- Get the prefixed parameters to processlocalfunctiongetPrefixes(args)localprefixes={}locali=1localcur=getPrefixedArg(args,'prefix',i,true)repeattable.insert(prefixes,curor'')i=i+1cur=getPrefixedArg(args,'prefix',i,true)untilnotcurreturnprefixesend-- Get the extra parameters to processlocalfunctiongetExtras(args)localextras={}locali=1localcur=getPrefixedArg(args,'extra',i,true)whilecurdolocalnumMatch=cur:match('^(.+)%(i%)$')ifnumMatchthentable.insert(extras,{numMatch,true})elsetable.insert(extras,{cur,false})endi=i+1cur=getPrefixedArg(args,'extra',i,true)endreturnextrasend-- Perform wikitext parameter substitutionfunctionsubstParams(code,num,args)args['i']=numlocalprocessed=code:gsub("{{{([^{}|]*)|?[^{}]*}}}",args)-- Substitute parametersreturnprocessedend-- Return the function that processes each iterationfunctionmakeProcessor(frame,parent)localtemplate=frame.args.calliftemplatethenreturnfunction(num,args)args['i']=numreturnframe:expandTemplate{title=template,args=args}endend-- Note: The difference between angle brackets and their entities is lostlocalcode=mw.text.unstripNoWiki(frame.args[1]):gsub('<','<'):gsub('>','>')localexpand=yesno(frame.args.expandortrue)ifexpandthenlocalprocessingFrame=parent:newChild{title='Module:Map params preprocessor',args={}}returnfunction(num,args)returnprocessingFrame:preprocess(substParams(code,num,args))endendreturnfunction(num,args)returnsubstParams(code,num,args)endendfunctionnumPairs(args,parentArgs,prefixes,optNum)localstart=tonumber(args.startor1)ifyesno(args.sparseorfalse)thenlocalnums={}localseenNums={}fork,_inpairs(parentArgs)dolocalprefixMatch=falselocalj=1-- Check every prefix for a matchwhileprefixes[j]andnotprefixMatchdolocalnumStr=tostring(k):match('^'..escPattern(prefixes[j])..'(%d*)$')ifnumStr~=nilthenlocalnum=tonumber(numStr)or(optNumand1ornil)ifnum~=nilandnum>=startandnotseenNums[num]thentable.insert(nums,num)seenNums[num]=trueendprefixMatch=trueendj=j+1endendtable.sort(nums)-- Iterate over each found numberfunctionsequenceIter(a,i)i=i+1localv=a[i]ifvthenreturni,vendendreturnsequenceIter,nums,0end-- Iterate each number and check for any matcheslocalfunctionprefixIter(a,i)i=i+1localj=1localprefixMatch=false-- Check every prefix for a matchwhileprefixes[j]andnotprefixMatchdoprefixMatch=getPrefixedArg(a,prefixes[j],i,optNum)~=nilj=j+1endifprefixMatchthenreturni,iendendreturnprefixIter,parentArgs,start-1endfunctionp.map(frame)localresult={}localparent=frame:getParent()localsep=frame.args.callandframe.args[1]orframe.args[2]orframe.args.sepor''localconj=frame.args.conjorseplocalprefixes=getPrefixes(frame.args)localextras=getExtras(frame.args)localprocess=makeProcessor(frame,parent)localoptNum=yesno(frame.args.optnumortrue)for_,numinnumPairs(frame.args,parent.args,prefixes,optNum)dolocalargs={}-- Pass extra registered argumentsfor_,extraInfoinipairs(extras)dolocalextra,numbered=unpack(extraInfo)ifnumberedthenargs[extra]=getPrefixedArg(parent.args,extra,num,optNum)elseargs[extra]=parent.args[extra]endend-- Pass current parameters without their numeric suffixfor_,prefixinipairs(prefixes)doargs[prefix~=''andprefixor'1']=getPrefixedArg(parent.args,prefix,num,optNum)endtable.insert(result,process(num,args))endreturnmw.text.listToText(result,sep,conj)endreturnp