https://www.bayoen.fr/wiki/index.php?title=Module:Arguments&feed=atom&action=historyModule:Arguments - Historique des versions2024-03-29T14:01:02ZHistorique pour cette page sur le wikiMediaWiki 1.22.2https://www.bayoen.fr/wiki/index.php?title=Module:Arguments&diff=1146&oldid=prevBLueBLue : 1 version2014-03-13T00:37:47Z<p>1 version</p>
<table class='diff diff-contentalign-left'>
<tr style='vertical-align: top;'>
<td colspan='1' style="background-color: white; color:black; text-align: center;">← Version précédente</td>
<td colspan='1' style="background-color: white; color:black; text-align: center;">Version du 13 mars 2014 à 00:37</td>
</tr><tr><td colspan='2' style='text-align: center;'><div class="mw-diff-empty">(Aucune différence)</div>
</td></tr></table>BLueBLuehttps://www.bayoen.fr/wiki/index.php?title=Module:Arguments&diff=1145&oldid=prevMr. Stradivarius : tweak one of the comments and make some beautification fixes, now that this is in the job queue2014-01-13T05:40:58Z<p>tweak one of the comments and make some beautification fixes, now that this is in the job queue</p>
<p><b>Nouvelle page</b></p><div>-- This module provides easy processing of arguments passed to Scribunto from #invoke.<br />
-- It is intended for use by other Lua modules, and should not be called from #invoke directly.<br />
<br />
local libraryUtil = require('libraryUtil')<br />
local checkType = libraryUtil.checkType<br />
<br />
local arguments = {}<br />
<br />
local nilArg = {} -- Used for memoizing nil arguments in metaArgs.<br />
<br />
-- Generate four different tidyVal functions, so that we don't have to check the options every time we call it.<br />
<br />
local function tidyValDefault(key, val)<br />
if type(val) == 'string' then<br />
val = val:match('^%s*(.-)%s*$')<br />
if val == '' then<br />
return nil<br />
else<br />
return val<br />
end<br />
else<br />
return val<br />
end<br />
end<br />
<br />
local function tidyValTrimOnly(key, val)<br />
if type(val) == 'string' then<br />
return val:match('^%s*(.-)%s*$')<br />
else<br />
return val<br />
end<br />
end<br />
<br />
local function tidyValRemoveBlanksOnly(key, val)<br />
if type(val) == 'string' then<br />
if val:find('%S') then<br />
return val<br />
else<br />
return nil<br />
end<br />
else<br />
return val<br />
end<br />
end<br />
<br />
local function tidyValNoChange(key, val)<br />
return val<br />
end<br />
<br />
function arguments.getArgs(frame, options)<br />
checkType('getArgs', 1, frame, 'table', true)<br />
checkType('getArgs', 2, options, 'table', true)<br />
frame = frame or {}<br />
options = options or {}<br />
<br />
-- Get the arguments from the frame object if available. If the frame object is not available, we are being called<br />
-- from another Lua module or from the debug console, so assign the args to a new variable so we can differentiate them.<br />
local fargs, pargs, luaArgs<br />
if type(frame.args) == 'table' and type(frame.getParent) == 'function' then<br />
if not options.parentOnly then<br />
fargs = frame.args<br />
end<br />
if not options.frameOnly then<br />
pargs = frame:getParent().args<br />
end<br />
if options.parentFirst then<br />
fargs, pargs = pargs, fargs<br />
end<br />
else<br />
luaArgs = frame<br />
end<br />
<br />
-- Set up the args and metaArgs tables. args will be the one accessed from functions, and metaArgs will hold the actual arguments.<br />
-- The metatable connects the two together.<br />
local args, metaArgs, metatable = {}, {}, {}<br />
setmetatable(args, metatable)<br />
<br />
-- Generate the tidyVal function. If it has been specified by the user, we use that; if not, we choose one of four functions<br />
-- depending on the options chosen. This is so that we don't have to call the options table every time the function is called.<br />
local tidyVal = options.valueFunc<br />
if tidyVal then<br />
if type(tidyVal) ~= 'function' then<br />
error("bad value assigned to option 'valueFunc' (function expected, got " .. type(tidyVal) .. ')', 2)<br />
end<br />
elseif options.trim ~= false then<br />
if options.removeBlanks ~= false then<br />
tidyVal = tidyValDefault<br />
else<br />
tidyVal = tidyValTrimOnly<br />
end<br />
else<br />
if options.removeBlanks ~= false then<br />
tidyVal = tidyValRemoveBlanksOnly<br />
else<br />
tidyVal = tidyValNoChange<br />
end<br />
end<br />
<br />
local function mergeArgs(iterator, tables)<br />
-- Accepts multiple tables as input and merges their keys and values into one table using the specified iterator.<br />
-- If a value is already present it is not overwritten; tables listed earlier have precedence.<br />
-- We are also memoizing nil values, but those values can be overwritten.<br />
for _, t in ipairs(tables) do<br />
for key, val in iterator(t) do<br />
local metaArgsVal = metaArgs[key]<br />
if metaArgsVal == nil or metaArgsVal == nilArg then<br />
local tidiedVal = tidyVal(key, val)<br />
if tidiedVal == nil then<br />
metaArgs[key] = nilArg<br />
else<br />
metaArgs[key] = tidiedVal<br />
end<br />
end<br />
end<br />
end<br />
end<br />
<br />
-- Set the order of precedence of the argument tables. If the variables are nil, nothing will be added to the table,<br />
-- which is how we avoid clashes between the frame/parent args and the Lua args. <br />
local argTables = {fargs}<br />
argTables[#argTables + 1] = pargs<br />
argTables[#argTables + 1] = luaArgs<br />
<br />
--[[<br />
-- Define metatable behaviour. Arguments are memoized in the metaArgs table, and are only fetched from the<br />
-- argument tables once. Nil arguments are also memoized using the nilArg variable in order to increase<br />
-- performance. Also, we keep a record in the metatable of when pairs and ipairs have been called, so we<br />
-- do not run pairs and ipairs on fargs and pargs more than once. We also do not run ipairs on fargs and<br />
-- pargs if pairs has already been run, as all the arguments will already have been copied over.<br />
--]]<br />
<br />
metatable.__index = function (t, key)<br />
local val = metaArgs[key]<br />
if val ~= nil then<br />
if val == nilArg then<br />
return nil<br />
else<br />
return val<br />
end<br />
end<br />
for _, argTable in ipairs(argTables) do<br />
local argTableVal = tidyVal(key, argTable[key])<br />
if argTableVal == nil then<br />
metaArgs[key] = nilArg<br />
else<br />
metaArgs[key] = argTableVal<br />
return argTableVal<br />
end<br />
end<br />
return nil<br />
end<br />
<br />
metatable.__newindex = function (t, key, val)<br />
if options.readOnly then<br />
error('could not write to argument table key "' .. tostring(key) .. '"; the table is read-only', 2)<br />
elseif options.noOverwrite and args[key] ~= nil then<br />
error('could not write to argument table key "' .. tostring(key) .. '"; overwriting existing arguments is not permitted', 2)<br />
elseif val == nil then<br />
metaArgs[key] = nilArg -- Memoize nils.<br />
else<br />
metaArgs[key] = val<br />
end<br />
end<br />
<br />
metatable.__pairs = function ()<br />
if not metatable.donePairs then<br />
mergeArgs(pairs, argTables)<br />
metatable.donePairs = true<br />
metatable.doneIpairs = true<br />
end<br />
return function (t, k)<br />
local nk, val = next(metaArgs, k)<br />
if val == nilArg then<br />
val = nil<br />
end<br />
return nk, val<br />
end<br />
end<br />
<br />
metatable.__ipairs = function ()<br />
if not metatable.doneIpairs then<br />
mergeArgs(ipairs, argTables)<br />
metatable.doneIpairs = true<br />
end<br />
return function (t, i)<br />
local val = metaArgs[i + 1]<br />
if val == nil then<br />
return nil<br />
elseif val == nilArg then<br />
val = nil<br />
end<br />
return i + 1, val<br />
end, nil, 0<br />
end<br />
<br />
return args<br />
end<br />
<br />
return arguments</div>Mr. Stradivarius