![]() |
VOOZH | about |
Referenced from the Unsubst module on Wikipedia.
localcheckType=require('libraryUtil').checkType localp={} localBODY_PARAM='$B' localspecialParams={ ['$params']='parameter list', ['$aliases']='parameter aliases', ['$flags']='flags', ['$B']='template content' } functionp.main(frame,body) -- If we are substing, this function returns a template invocation, and if -- not, it returns the template body. The template body can be specified in -- the body parameter, or in the template parameter defined in the -- BODY_PARAM variable. This function can be called from Lua or from -- #invoke. -- Return the template body if we aren't substing. ifnotmw.isSubsting()then ifbody~=nilthen returnbody elseifframe.args[BODY_PARAM]~=nilthen returnframe.args[BODY_PARAM] else error(string.format( "no template content specified (use parameter '%s' from #invoke)", BODY_PARAM ),2) end end -- Sanity check for the frame object. iftype(frame)~='table' ortype(frame.getParent)~='function' ornotframe:getParent() then error( "argument #1 to 'main' must be a frame object with a parent ".. "frame available", 2 ) end -- Find the invocation name. localmTemplateInvocation=require('Module:Template invocation') localname=mTemplateInvocation.name(frame:getParent():getTitle()) -- Combine passed args with passed defaults localargs={} ifstring.find(','..(frame.args['$flags']or'')..',',',%s*override%s*,')then fork,vinpairs(frame:getParent().args)do args[k]=v end fork,vinpairs(frame.args)do ifnotspecialParams[k]then ifv=='__DATE__'then v=mw.getContentLanguage():formatDate('F Y') end args[k]=v end end else fork,vinpairs(frame.args)do ifnotspecialParams[k]then ifv=='__DATE__'then v=mw.getContentLanguage():formatDate('F Y') end args[k]=v end end fork,vinpairs(frame:getParent().args)do args[k]=v end end -- Trim parameters, if not specified otherwise ifnotstring.find(','..(frame.args['$flags']or'')..',',',%s*keep%-whitespace%s*,')then fork,vinpairs(args)doargs[k]=mw.ustring.match(v,'^%s*(.*)%s*$')or''end end -- Pull information from parameter aliases localaliases={} ifframe.args['$aliases']then locallist=mw.text.split(frame.args['$aliases'],'%s*,%s*') fork,vinipairs(list)do localtmp=mw.text.split(v,'%s*>%s*') aliases[tonumber(mw.ustring.match(tmp[1],'^[1-9][0-9]*$'))ortmp[1]]=((tonumber(mw.ustring.match(tmp[2],'^[1-9][0-9]*$')))ortmp[2]) end end fork,vinpairs(aliases)do ifargs[k]and(notargs[v]orargs[v]=='')then args[v]=args[k] end args[k]=nil end -- Remove empty parameters, if specified ifstring.find(','..(frame.args['$flags']or'')..',',',%s*remove%-empty%s*,')then localtmp=0 fork,vinipairs(args)do ifv~=''or(args[k+1]andargs[k+1]~='')or(args[k+2]andargs[k+2]~='')then tmp=k else break end end fork,vinpairs(args)do ifv==''then ifnot(type(k)=='number'andk<tmp)thenargs[k]=nilend end end end -- Order parameters ifframe.args['$params']then localparams,tmp=mw.text.split(frame.args['$params'],'%s*,%s*'),{} fork,vinipairs(params)do v=tonumber(mw.ustring.match(v,'^[1-9][0-9]*$'))orv ifargs[v]thentmp[v],args[v]=args[v],nilend end fork,vinpairs(args)dotmp[k],args[k]=args[k],nilend args=tmp end returnmTemplateInvocation.invocation(name,args) end p['']=p.main-- For backwards compatibility returnp