모듈:LoadData
Module:Data의 복잡한 버전으로, 더 많은 옵션을 제공하며 부등호 연산자로 인덱스를 선택할 수 있는 기능이 있습니다.
사용법
[편집]{{#invoke:LoadData|모듈 이름|index1|2 lteq=index2 limit|...|}}
- 0번째 매개변수는 읽어올 데이터 모듈의 이름이며, 모듈: 접두사는 생략합니다. (예:
Example/data) - 다음 매개변수들은 1부터 시작하는 인덱스 N에 사용됩니다. 원하는 정확한 인덱스를 지정하려면
|N=을 사용하고(가능한 경우 숫자로 강제 변환됨), 인수보다 작거나 같은 가장 큰 숫자 인덱스를 선택하려면|N lteq=, 그 반대의 경우(크거나 같은 가장 작은 숫자)에는|N gteq=를 지정합니다. |template=은 결과값을 보간(삽입)할 printf 형식의 문자열입니다. (예:<b>%s</b>)|preprocess=는|template=과 유사하지만, 문자열에frame:preprocess가 적용된다는 점이 다릅니다. 이를 통해 틀 끼워넣기(transclusion) 등이 정상적으로 작동하게 됩니다.|if nil=은 결과가 nil일 경우 반환할 문자열입니다. 기본값은 nil이며, 빈 문자열로 출력됩니다.
local p = {}
-- Finds the next key key <= or >= the given i.
-- operator is ±1
local function findItemRange(data, i, operator)
local bestIndex = nil
i = i * operator
for k, v in pairs(data) do
local kop = type(k) == 'number' and k * operator
if kop and kop <= i and (bestIndex == nil or kop > bestIndex * operator) then
bestIndex = k
end
end
if bestIndex then return data[bestIndex] else return nil end
end
local function load(datamodule, frame)
local args = frame.args
local data = mw.loadData(datamodule)
for i = 1, 20 do
if args[i] then data = data[tonumber(args[i]) or args[i]]
elseif args[i .. ' lteq'] then
data = findItemRange(data, tonumber(args[i .. ' lteq']), 1)
elseif args[i .. ' gteq'] then
data = findItemRange(data, tonumber(args[i .. ' gteq']), -1)
else break end
end
if data == nil then
return args['if_nil'] -- not a required argument, OK to return nil here.
end
if type(data) == 'table' then
-- Put the table into another table because the return value of loadData
-- is a "fake" table that only has certain metamethods.
local realdata = {}
for k, v in pairs(data) do
realdata[k] = v
end
data = realdata
else
data = { data }
end
if args['template'] then
return mw.text.unstripNoWiki(args['template']):format(unpack(data))
elseif args['preprocess'] then
return frame:preprocess(mw.text.unstripNoWiki(args['preprocess']):format(unpack(data)))
else
return table.concat(data)
end
end
return setmetatable({}, {
__index = function(t, k)
return function(frame)
return load('Module:' .. k, frame)
end
end
})