Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:MatrixFields: Difference between revisions

From FiguraMC
PenguinEncounter (talk | contribs)
it might work, maybe
 
PenguinEncounter (talk | contribs)
oops that's incorrect too :sob:
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
local exports = {}
local exports = {}
local ordinals = {
[1] = "first",
[2] = "second",
[3] = "third",
[4] = "fourth"
}


local function trim(str)
local function trim(str)
Line 12: Line 5:
end
end


local col_template = trim [=[
local anchor_template = [[<span id="field_$name"></span>]]
=== <code>c$n</code> <span id="field_c$n"></span> ===
local name_template = [[<code>$name</code>]]
{{aliases|$n}}


The $ord column as a {{type|Vector$T}}. Equivalent to calling [[#getColumn|<code>getColumn($n)</code>]].
local template = trim [=[
]=]
=== Column fields $colanchors ===
local row_template = trim [=[
 
=== <code>r$n</code> <span id="field_r$n"></span> ===
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>]].


The $ord row as a {{type|Vector$T}}. Equivalent to calling [[#getRow|<code>getRow($n)</code>]].
=== Value fields $valueanchors ===
]=]
local item_template = trim [=[
=== <code>v$y$x</code> <span id="field_v$y$x"></span> ===


The value in row $y and column $x.
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 col_t2 = col_template:gsub("$T", tostring(object_size))
local vars = {
local row_t2 = row_template:gsub("$T", tostring(object_size))
T = tostring(object_size),
colanchors = {},
local parts = {}
colnames = {},
rowanchors = {},
rownames = {},
valueanchors = {},
valuenames = {}
}
for i = 1, object_size do
for i = 1, object_size do
parts[#parts + 1] = col_t2:gsub("$n", tostring(i))
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
parts[#parts + 1] = row_t2:gsub("$n", tostring(i))
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
parts[#parts + 1] = item_template:gsub("$y", tostring(y)):gsub("$x", tostring(x))
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 packaged = table.concat(parts, "\n\n")
local result = template
return frame:preprocess(packaged)
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