Module:ComponentMethods: Difference between revisions

From Resonite Wiki
Format parameters
Trim description
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- test in console:
-- test in console:
-- =p.main(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={" SetUpRenderer()  *", " ", " Attaches components for a default [[PBS_Metallic]] material and [[Component:MeshRenderer|MeshRenderer]] referencing this mesh."}}:newChild{title="Module:Test",args={}})
-- =p.main(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={" SetUpRenderer()  *", " ", " Attaches components for a default [[PBS_Metallic]] material and [[Component:MeshRenderer|MeshRenderer]] referencing this mesh."}}:newChild{title="Module:Test",args={}})
-- =p.main(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={"Resize(size, filtering)", "size:int,int2:Scaling factor.;filtering:Filtering:Resampling method.", "Replaces the 2D texture with a scaled copy, resampled using the given filtering method. Scales separately in the X and Y axes when an int2 is passed."}}:newChild{title="Module:Test",args={}})
-- =p.main(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={"Resize(size, filtering)\n", "size:int,int2:Scaling factor.\n;filtering:Filtering:Resampling method.\n", "Replaces the 2D texture with a scaled copy, resampled using the given filtering method. Scales separately in the X and Y axes when an int2 is passed."}}:newChild{title="Module:Test",args={}})
-- =p.footnote(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={" SetUpRenderer()  *", " ", " Attaches components for a default [[PBS_Metallic]] material and [[Component:MeshRenderer|MeshRenderer]] referencing this mesh."}}:newChild{title="Module:Test",args={}})
-- =p.footnote(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={" SetUpRenderer()  *", " ", " Attaches components for a default [[PBS_Metallic]] material and [[Component:MeshRenderer|MeshRenderer]] referencing this mesh."}}:newChild{title="Module:Test",args={}})


Line 23: Line 23:


local _, nameEnd = method:find(")")
local _, nameEnd = method:find(")")
if not nameEnd then error('Missing closing parenthesis in method name "' .. method .. '"') end
local name = mw.text.trim(method:sub(1, nameEnd)):gsub(" +", " ")
local name = mw.text.trim(method:sub(1, nameEnd)):gsub(" +", " ")
local rest = mw.text.trim(method:sub(nameEnd + 1))
local rest = mw.text.trim(method:sub(nameEnd + 1))


params = mw.text.trim(params)
params = mw.text.trim(params)
description = mw.text.trim(description)
local noParams = #params == 0
local noParams = #params == 0


body = body .. "|-\n"
body = body .. '|- style="vertical-align:top;"\n'
body = body .. "|<code>" .. name .. "</code> " .. rest .. "\n"
body = body .. "|<code>" .. name .. "</code> " .. rest .. "\n"
if noParams then
if noParams then
Line 39: Line 42:
local _, paramEnd = params:find(";")
local _, paramEnd = params:find(";")
paramEnd = paramEnd or #params + 1
paramEnd = paramEnd or #params + 1
local param = params:sub(0, paramEnd - 1)
local param = mw.text.trim(params:sub(0, paramEnd - 1))
local name, types, desc = param:match("([^:]*):([^:]*):(.*)")
local name, types, desc = param:match("([^:]*):([^:]*):(.*)")
if not desc then error('Missing parts in param spec "' .. param .. '"') end


body = body .. "<code>" .. name .. "</code>&nbsp;:&nbsp;"
body = body .. "\n<code>" .. name .. "</code>&nbsp;:&nbsp;"


local first = true
local first = true
for t in types:gmatch("([^,]+)") do
for t in types:gmatch("([^,]+)") do
if not first then body = body .. '&nbsp;or&nbsp;' end
if not first then body = body .. "&nbsp;or&nbsp;" end
-- use custom link if specified
-- use custom link if specified
if t:match("^%[%[.+]]$") then
if t:match("^%[%[.+]]$") then
Line 55: Line 59:
first = false
first = false
end
end
body = body .. "<br>" .. desc .. '\n'
body = body .. "<br>" .. desc .. "\n"


params = params:sub(paramEnd + 1)
params = mw.text.trim(params:sub(paramEnd + 1))
until #params == 0
until #params == 0
end
end

Latest revision as of 07:02, 15 July 2024

Documentation for this module may be created at Module:ComponentMethods/doc

-- test in console:
-- =p.main(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={" SetUpRenderer()  *", " ", " Attaches components for a default [[PBS_Metallic]] material and [[Component:MeshRenderer|MeshRenderer]] referencing this mesh."}}:newChild{title="Module:Test",args={}})
-- =p.main(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={"Resize(size, filtering)\n", "size:int,int2:Scaling factor.\n;filtering:Filtering:Resampling method.\n", "Replaces the 2D texture with a scaled copy, resampled using the given filtering method. Scales separately in the X and Y axes when an int2 is passed."}}:newChild{title="Module:Test",args={}})
-- =p.footnote(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={" SetUpRenderer()  *", " ", " Attaches components for a default [[PBS_Metallic]] material and [[Component:MeshRenderer|MeshRenderer]] referencing this mesh."}}:newChild{title="Module:Test",args={}})

local p = {}

function p.main(frame)
	local templateFrame = frame:getParent()

	local body = ""
	local i = 0

	while true do
		local argI = i * 3 + 1
		local method = templateFrame.args[argI]
		local params = templateFrame.args[argI + 1]
		local description = templateFrame.args[argI + 2]

		if not description then
			break
		end

		local _, nameEnd = method:find(")")
		if not nameEnd then error('Missing closing parenthesis in method name "' .. method .. '"') end
		local name = mw.text.trim(method:sub(1, nameEnd)):gsub(" +", "&nbsp;")
		local rest = mw.text.trim(method:sub(nameEnd + 1))

		params = mw.text.trim(params)
		description = mw.text.trim(description)
		
		local noParams = #params == 0

		body = body .. '|- style="vertical-align:top;"\n'
		body = body .. "|<code>" .. name .. "</code> " .. rest .. "\n"
		if noParams then
			body = body .. "|''none''\n"
		else
			body = body .. "|"
			local first = false
			repeat
				local _, paramEnd = params:find(";")
				paramEnd = paramEnd or #params + 1
				local param = mw.text.trim(params:sub(0, paramEnd - 1))
				local name, types, desc = param:match("([^:]*):([^:]*):(.*)")
				if not desc then error('Missing parts in param spec "' .. param .. '"') end

				body = body .. "\n<code>" .. name .. "</code>&nbsp;:&nbsp;"

				local first = true
				for t in types:gmatch("([^,]+)") do
					if not first then body = body .. "&nbsp;or&nbsp;" end
					-- use custom link if specified
					if t:match("^%[%[.+]]$") then
						body = body .. t
					else
						body = body .. "[[Type:" .. t .. "|" .. t .. "]]"
					end
					first = false
				end
				body = body .. "<br>" .. desc .. "\n"

				params = mw.text.trim(params:sub(paramEnd + 1))
			until #params == 0
		end
		body = body .. "|" .. description .. "\n"
		i = i + 1
	end

	return body
end

function p.footnote(frame)
	local templateFrame = frame:getParent()

	local body = ""
	local anyHidden = false
	local i = 0

	while true do
		local method = templateFrame.args[3 * i + 1]
		
		if not method then
			break
		end

		local _, j = method:find(")")
		if method:sub(j + 1):find("*") then
			anyHidden = true
			break
		end

		i = i + 1
	end
	
	if anyHidden then
		body = body .. "''* Methods marked with an asterisk are invisible in the inspector by default.''"
	end

	return body
end

return p