Jump to content

Module:Sandbox/BrandonXLF/Module:Map params

From Wikipedia, the free encyclopedia

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('&lt;','<'):gsub('&gt;','>')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
close