Naar inhoud springen

Module:Arguments

Fu Wikipedia

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:Arguments/doc

-- This module provides easy processing of arguments passed to Scribunto from #invoke.-- It is intended for use by other Lua modules, and should not be called from #invoke directly.locallibraryUtil=require('libraryUtil')localcheckType=libraryUtil.checkTypelocalarguments={}localnilArg={}-- Used for memoizing nil arguments in metaArgs.-- Generate four different tidyVal functions, so that we don't have to check the options every time we call it.localfunctiontidyValDefault(key,val)iftype(val)=='string'thenval=val:match('^%s*(.-)%s*$')ifval==''thenreturnnilelsereturnvalendelsereturnvalendendlocalfunctiontidyValTrimOnly(key,val)iftype(val)=='string'thenreturnval:match('^%s*(.-)%s*$')elsereturnvalendendlocalfunctiontidyValRemoveBlanksOnly(key,val)iftype(val)=='string'thenifval:find('%S')thenreturnvalelsereturnnilendelsereturnvalendendlocalfunctiontidyValNoChange(key,val)returnvalendfunctionarguments.getArgs(frame,options)checkType('getArgs',1,frame,'table',true)checkType('getArgs',2,options,'table',true)frame=frameor{}options=optionsor{}-- Get the arguments from the frame object if available. If the frame object is not available, we are being called-- from another Lua module or from the debug console, so assign the args to a new variable so we can differentiate them.localfargs,pargs,luaArgsiftype(frame.args)=='table'andtype(frame.getParent)=='function'thenifnotoptions.parentOnlythenfargs=frame.argsendifnotoptions.frameOnlythenpargs=frame:getParent().argsendifoptions.parentFirstthenfargs,pargs=pargs,fargsendelseluaArgs=frameend-- Set up the args and metaArgs tables. args will be the one accessed from functions, and metaArgs will hold the actual arguments.-- The metatable connects the two together.localargs,metaArgs,metatable={},{},{}setmetatable(args,metatable)-- Generate the tidyVal function. If it has been specified by the user, we use that; if not, we choose one of four functions-- depending on the options chosen. This is so that we don't have to call the options table every time the function is called.localtidyVal=options.valueFunciftidyValtheniftype(tidyVal)~='function'thenerror("bad value assigned to option 'valueFunc' (function expected, got "..type(tidyVal)..')',2)endelseifoptions.trim~=falsethenifoptions.removeBlanks~=falsethentidyVal=tidyValDefaultelsetidyVal=tidyValTrimOnlyendelseifoptions.removeBlanks~=falsethentidyVal=tidyValRemoveBlanksOnlyelsetidyVal=tidyValNoChangeendendlocalfunctionmergeArgs(iterator,tables)-- Accepts multiple tables as input and merges their keys and values into one table using the specified iterator.-- If a value is already present it is not overwritten; tables listed earlier have precedence.-- We are also memoizing nil values, but those values can be overwritten.for_,tinipairs(tables)doforkey,valiniterator(t)dolocalmetaArgsVal=metaArgs[key]ifmetaArgsVal==nilormetaArgsVal==nilArgthenlocaltidiedVal=tidyVal(key,val)iftidiedVal==nilthenmetaArgs[key]=nilArgelsemetaArgs[key]=tidiedValendendendendend-- Set the order of precedence of the argument tables. If the variables are nil, nothing will be added to the table,-- which is how we avoid clashes between the frame/parent args and the Lua args. localargTables={fargs}argTables[#argTables+1]=pargsargTables[#argTables+1]=luaArgs--[[ -- Define metatable behaviour. Arguments are memoized in the metaArgs table, and are only fetched from the -- argument tables once. Nil arguments are also memoized using the nilArg variable in order to increase -- performance. Also, we keep a record in the metatable of when pairs and ipairs have been called, so we -- do not run pairs and ipairs on fargs and pargs more than once. We also do not run ipairs on fargs and -- pargs if pairs has already been run, as all the arguments will already have been copied over. --]]metatable.__index=function(t,key)localval=metaArgs[key]ifval~=nilthenifval==nilArgthenreturnnilelsereturnvalendendfor_,argTableinipairs(argTables)dolocalargTableVal=tidyVal(key,argTable[key])ifargTableVal==nilthenmetaArgs[key]=nilArgelsemetaArgs[key]=argTableValreturnargTableValendendreturnnilendmetatable.__newindex=function(t,key,val)ifoptions.readOnlythenerror('could not write to argument table key "'..tostring(key)..'"; the table is read-only',2)elseifoptions.noOverwriteandargs[key]~=nilthenerror('could not write to argument table key "'..tostring(key)..'"; overwriting existing arguments is not permitted',2)elseifval==nilthenmetaArgs[key]=nilArg-- Memoize nils.elsemetaArgs[key]=valendendmetatable.__pairs=function()ifnotmetatable.donePairsthenmergeArgs(pairs,argTables)metatable.donePairs=truemetatable.doneIpairs=trueendreturnfunction(t,k)localnk,val=next(metaArgs,k)ifval==nilArgthenval=nilendreturnnk,valendendmetatable.__ipairs=function()ifnotmetatable.doneIpairsthenmergeArgs(ipairs,argTables)metatable.doneIpairs=trueendreturnfunction(t,i)localval=metaArgs[i+1]ifval==nilthenreturnnilelseifval==nilArgthenval=nilendreturni+1,valend,nil,0endreturnargsendreturnarguments
close