Module:Infobox/tabber: Difference between revisions

From Nguhcraft Wiki
Jump to navigation Jump to search
No edit summary
(comments)
Line 2: Line 2:


p.generateTabber = function(frame)
p.generateTabber = function(frame)
-- Get access to the arguments of the template that called this function
local parentFrame = frame:getParent()
local parentFrame = frame:getParent()
-- Find which number arguments are present
local tabNums = {}
local tabNums = {}
for k, _ in pairs(parentFrame.args) do
for k, _ in pairs(parentFrame.args) do
Line 9: Line 11:
end
end
end
end
-- Sort them because order is not guaranteed
-- see https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#frame.args
-- "However, due to how Lua implements table iterators, iterating over
--  arguments will return them in an unspecified order, and there's no way
--  to know the original order as they appear in wikitext."
table.sort(tabNums)
table.sort(tabNums)
-- Create a string with the text that goes in the <tabber> tag
local tabberStr = ""
local tabberStr = ""
for _, i in ipairs(tabNums) do
for _, i in ipairs(tabNums) do
if parentFrame.args[i] then
tabberStr = tabberStr ..
tabberStr = tabberStr .. "|-|" .. (parentFrame.args["label" .. i] or i) .. "=<table style=\"width:100%;border-collapse:collapse\">" .. parentFrame.args[i] .. "</table>"
"|-|" ..
end
(parentFrame.args["label" .. i] or i) .. -- If no label it just uses the number as a label
"=<table style=\"width:100%;border-collapse:collapse\">" ..
parentFrame.args[i] ..
"</table>"
end
end
-- Return
if tabberStr == "" then
if tabberStr == "" then
return ""
return ""

Revision as of 09:08, 19 December 2024

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

local p = {}

p.generateTabber = function(frame)
	-- Get access to the arguments of the template that called this function
	local parentFrame = frame:getParent()
	-- Find which number arguments are present
	local tabNums = {}
	for k, _ in pairs(parentFrame.args) do
		if type(k) == "number" then
			table.insert(tabNums, k)
		end
	end
	-- Sort them because order is not guaranteed
	-- see https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#frame.args
	-- "However, due to how Lua implements table iterators, iterating over
	--  arguments will return them in an unspecified order, and there's no way
	--  to know the original order as they appear in wikitext."
	table.sort(tabNums)
	-- Create a string with the text that goes in the <tabber> tag
	local tabberStr = ""
	for _, i in ipairs(tabNums) do
		tabberStr = tabberStr ..
			"|-|" ..
			(parentFrame.args["label" .. i] or i) .. -- If no label it just uses the number as a label
			"=<table style=\"width:100%;border-collapse:collapse\">" ..
			parentFrame.args[i] ..
			"</table>"
	end
	-- Return
	if tabberStr == "" then
		return ""
	else
		return "<tr><td colspan=\"2\" style=\"padding:0\">" .. frame:extensionTag("tabber", tabberStr) .. "</td></tr>"
	end
end

return p