More actions
m 'first' |
No edit summary |
||
Line 1: | Line 1: | ||
local exports = {} | local exports = {} | ||
local function trim(str) | local function trim(str) | ||
Line 12: | Line 5: | ||
end | end | ||
local | local anchor_template = [[<span id="field_$name"></span>]] | ||
=== <code>c$n</code> < | local name_template = [[<code>$name</code>]] | ||
local template = trim [=[ | |||
=== Column fields $colanchors === | |||
Column fields in the form <code>c''N''</code> ($colnames) read the values in the specified column as a {{type|Vector$T}}. | |||
The field produces the same result as an equivalent call to [[#getColumn|<code>getColumn(n)</code>]]. | |||
Column fields are also available by indexing by the column number alone (i.e. <code>mat[1]</code>.) | |||
=== Row fields $rowanchors === | |||
Row fields in the form <code>r''N''</code> ($rownames) read the values in the specified row as a {{type|Vector$T}}. | |||
The field produces the same result as an equivalent call to [[#getRow|<code>getRow(n)</code>]]. | |||
=== Value fields $valueanchors === | |||
=== | |||
Value fields in the form <code>v''RC''</code> ($valuenames) read the value (a {{type|number}}) at the specified row and column. | |||
]=] | ]=] | ||
Line 37: | Line 34: | ||
local row_t2 = row_template:gsub("$T", tostring(object_size)) | local row_t2 = row_template:gsub("$T", tostring(object_size)) | ||
local | local vars = { | ||
T = tostring(object_size), | |||
colanchors = {}, | |||
colnames = {}, | |||
rowanchors = {}, | |||
rownames = {}, | |||
valueanchors = {}, | |||
valuenames = {} | |||
} | |||
for i = 1, object_size do | for i = 1, object_size do | ||
local fn = "c" .. i | |||
vars.colanchors[i] = anchor_template:gsub("$name", fn) | |||
vars.colnames[i] = name_template:gsub("$name", fn) | |||
end | end | ||
for i = 1, object_size do | for i = 1, object_size do | ||
local fn = "r" .. i | |||
vars.rowanchors[i] = anchor_template:gsub("$name", fn) | |||
vars.rownames[i] = name_template:gsub("$name", fn) | |||
end | end | ||
for x = 1, object_size do | for x = 1, object_size do | ||
for y = 1, object_size do | for y = 1, object_size do | ||
local fn = "r" .. y .. x | |||
vars.valueanchors[i] = anchor_template:gsub("$name", fn) | |||
vars.valuenames[i] = name_template:gsub("$name", fn) | |||
end | end | ||
end | end | ||
local | local result = template | ||
return frame:preprocess( | for k, v in pairs(vars) do | ||
if k:find("anchors$") then v = table.concat(v, "") end | |||
if k:find("names$") then v = table.concat(v, ", ") end | |||
result = result:gsub("$"..k, v) | |||
end | |||
return frame:preprocess(result) | |||
end | end | ||
return exports | return exports |
Revision as of 04:40, 21 December 2024
Documentation for this module may be created at Module:MatrixFields/doc
local exports = {}
local function trim(str)
return str:gsub("^%s*", ""):gsub("%s*$", "")
end
local anchor_template = [[<span id="field_$name"></span>]]
local name_template = [[<code>$name</code>]]
local template = trim [=[
=== Column fields $colanchors ===
Column fields in the form <code>c''N''</code> ($colnames) read the values in the specified column as a {{type|Vector$T}}.
The field produces the same result as an equivalent call to [[#getColumn|<code>getColumn(n)</code>]].
Column fields are also available by indexing by the column number alone (i.e. <code>mat[1]</code>.)
=== Row fields $rowanchors ===
Row fields in the form <code>r''N''</code> ($rownames) read the values in the specified row as a {{type|Vector$T}}.
The field produces the same result as an equivalent call to [[#getRow|<code>getRow(n)</code>]].
=== Value fields $valueanchors ===
Value fields in the form <code>v''RC''</code> ($valuenames) read the value (a {{type|number}}) at the specified row and column.
]=]
function exports.run(frame)
local input_args = frame.args
local object_size = input_args.size or error "No size provided (size=)"
object_size = tonumber(object_size) or error "Size provided was not a number"
local col_t2 = col_template:gsub("$T", tostring(object_size))
local row_t2 = row_template:gsub("$T", tostring(object_size))
local vars = {
T = tostring(object_size),
colanchors = {},
colnames = {},
rowanchors = {},
rownames = {},
valueanchors = {},
valuenames = {}
}
for i = 1, object_size do
local fn = "c" .. i
vars.colanchors[i] = anchor_template:gsub("$name", fn)
vars.colnames[i] = name_template:gsub("$name", fn)
end
for i = 1, object_size do
local fn = "r" .. i
vars.rowanchors[i] = anchor_template:gsub("$name", fn)
vars.rownames[i] = name_template:gsub("$name", fn)
end
for x = 1, object_size do
for y = 1, object_size do
local fn = "r" .. y .. x
vars.valueanchors[i] = anchor_template:gsub("$name", fn)
vars.valuenames[i] = name_template:gsub("$name", fn)
end
end
local result = template
for k, v in pairs(vars) do
if k:find("anchors$") then v = table.concat(v, "") end
if k:find("names$") then v = table.concat(v, ", ") end
result = result:gsub("$"..k, v)
end
return frame:preprocess(result)
end
return exports