(Created page with "local p = {} function p.add(frame) return tostring(tonumber(frame.args[1]) + tonumber(frame.args[2])) end return p") |
No edit summary |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p. | function splitByPlainSeparator(str, sep, max) | ||
local z = #sep; sep = '^.-'..sep:gsub('[$%%()*+%-.?%[%]^]', '%%%0') | |||
local t,n,p, q,r = {},1,1, str:find(sep) | |||
while q and n~=max do | |||
t[n],n,p = str:sub(q,r-z),n+1,r+1 | |||
q,r = str:find(sep,p) | |||
end | |||
t[n] = str:sub(p) | |||
return t | |||
end | |||
function toMap(entries) | |||
local fields = splitByPlainSeparator(entries, '$$') | |||
mw.log(fields) | |||
local map = {} | |||
for var=1,#fields-1 do | |||
local tmp = {} | |||
local current = fields[var] | |||
for k, s in pairs(splitByPlainSeparator(current, '$')) do | |||
local tmp2 = splitByPlainSeparator(s, '=') | |||
tmp[tmp2[1]] = tmp2[2] | |||
end | |||
map[var] = tmp | |||
end | |||
return map | |||
end | |||
function value(entries, i, j, args) | |||
if not entries[i] then | |||
return 'Out Of Range' | |||
else | |||
if not entries[i][j] or entries[i][j] == '' then | |||
return args['default/' .. j] or '' | |||
else | |||
return entries[i][j] | |||
end | |||
end | |||
end | |||
function uniq(entries, i, j, args, columns) | |||
if not entries[i] then | |||
return 'Out Of Range' | |||
end | |||
if not args['unique/' .. j] then | |||
return value(entries, i, j, args) | |||
end | |||
local ret = args['unique/' .. j] | |||
for k, column in pairs(columns) do | |||
ret = ret:gsub(column, value(entries, i, column, args)) | |||
end | |||
return ret | |||
end | |||
function p.table(frame) | |||
local args = frame.args | |||
local entries = toMap(frame.args['entries']) | |||
local columns = splitByPlainSeparator(frame.args['columns'], ',') | |||
local result = "" | |||
for var=1,#entries do | |||
for k, column in pairs(columns) do | |||
local id = uniq(entries, var, column, args, columns) | |||
if (id ~= uniq(entries, var - 1, column, args, columns)) then | |||
local rowspan = 1 | |||
while (id == uniq(entries, var + rowspan, column, args, columns)) do | |||
rowspan = rowspan + 1 | |||
end | |||
local value = value(entries, var, column, args) | |||
if value ~= '' and frame.args['display/' .. column] then | |||
value = frame.args['display/' .. column]:gsub('value', value) | |||
end | |||
result = result .. '|rowspan="' .. tostring(rowspan) .. '"|' .. value .. "\n" | |||
end | |||
end | |||
if (var ~= #entries) then | |||
result = result .. "|-\n" | |||
end | |||
end | |||
return result | |||
end | end | ||
return p | return p |
Latest revision as of 13:43, 11 July 2024
Documentation for this module may be created at Module:TestModule/doc
local p = {} function splitByPlainSeparator(str, sep, max) local z = #sep; sep = '^.-'..sep:gsub('[$%%()*+%-.?%[%]^]', '%%%0') local t,n,p, q,r = {},1,1, str:find(sep) while q and n~=max do t[n],n,p = str:sub(q,r-z),n+1,r+1 q,r = str:find(sep,p) end t[n] = str:sub(p) return t end function toMap(entries) local fields = splitByPlainSeparator(entries, '$$') mw.log(fields) local map = {} for var=1,#fields-1 do local tmp = {} local current = fields[var] for k, s in pairs(splitByPlainSeparator(current, '$')) do local tmp2 = splitByPlainSeparator(s, '=') tmp[tmp2[1]] = tmp2[2] end map[var] = tmp end return map end function value(entries, i, j, args) if not entries[i] then return 'Out Of Range' else if not entries[i][j] or entries[i][j] == '' then return args['default/' .. j] or '' else return entries[i][j] end end end function uniq(entries, i, j, args, columns) if not entries[i] then return 'Out Of Range' end if not args['unique/' .. j] then return value(entries, i, j, args) end local ret = args['unique/' .. j] for k, column in pairs(columns) do ret = ret:gsub(column, value(entries, i, column, args)) end return ret end function p.table(frame) local args = frame.args local entries = toMap(frame.args['entries']) local columns = splitByPlainSeparator(frame.args['columns'], ',') local result = "" for var=1,#entries do for k, column in pairs(columns) do local id = uniq(entries, var, column, args, columns) if (id ~= uniq(entries, var - 1, column, args, columns)) then local rowspan = 1 while (id == uniq(entries, var + rowspan, column, args, columns)) do rowspan = rowspan + 1 end local value = value(entries, var, column, args) if value ~= '' and frame.args['display/' .. column] then value = frame.args['display/' .. column]:gsub('value', value) end result = result .. '|rowspan="' .. tostring(rowspan) .. '"|' .. value .. "\n" end end if (var ~= #entries) then result = result .. "|-\n" end end return result end return p