More actions
it might work, maybe |
oops that's incorrect too :sob: |
||
(4 intermediate revisions by the same user not shown) | |||
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>]] | ||
local name_template = [[<code>$name</code>]] | |||
local template = trim [=[ | |||
]= | === Column fields $colanchors === | ||
=== <code>r | 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. | |||
The row number comes first, then the column number. | |||
]=] | ]=] | ||
Line 34: | Line 32: | ||
object_size = tonumber(object_size) or error "Size provided was not a number" | object_size = tonumber(object_size) or error "Size provided was not a number" | ||
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 | ||
local i = 0 | |||
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 = "v" .. y .. x | |||
i = i + 1 | |||
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 |
Latest revision as of 04:43, 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.
The row number comes first, then the column number.
]=]
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 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
local i = 0
for x = 1, object_size do
for y = 1, object_size do
local fn = "v" .. y .. x
i = i + 1
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