Module:Infobox/tabber: Difference between revisions

From Nguhcraft Wiki
Jump to navigation Jump to search
(I think this should make it so that if there is only one tab there is no tabber)
(that didn't work but hopefully this should)
Line 10: Line 10:
if type(k) == "number" then
if type(k) == "number" then
table.insert(tabNums, k)
table.insert(tabNums, k)
end
end
-- If there is only one tab, don't generate tabber unless allowOneTab=yes
if not parentFrame.args["allowOneTab"] == "yes" and #tabNums == 1 then
if string.find(makeEmptyPartEmptyNoFrame(parentFrame.args[tabNums[1]]), "%S") then
return parentFrame.args[tabNums[1]]
else
return frame:preprocess("<nowiki />")
end
end
end
end
Line 28: Line 20:
-- Create a string with the text that goes in the <tabber> tag
-- Create a string with the text that goes in the <tabber> tag
local tabberStr = ""
local tabberStr = ""
local nonEmptyTabs = {}
for _, i in ipairs(tabNums) do
for _, i in ipairs(tabNums) do
if string.find(makeEmptyPartEmptyNoFrame(parentFrame.args[i]), "%S") then
if string.find(makeEmptyPartEmptyNoFrame(parentFrame.args[i]), "%S") then
table.insert(nonEmptyTabs, i)
local tabLabel = parentFrame.args["label" .. i]
local tabLabel = parentFrame.args["label" .. i]
-- If no label it just uses the number as a label
-- If no label it just uses the number as a label
Line 45: Line 39:
parentFrame.args[i] ..
parentFrame.args[i] ..
"</table>"
"</table>"
end
end
-- If there is only one tab, don't generate tabber unless allowOneTab=yes
if not parentFrame.args["allowOneTab"] == "yes" and #nonEmptyTabs == 1 then
if string.find(makeEmptyPartEmptyNoFrame(parentFrame.args[nonEmptyTabs[1]]), "%S") then
return parentFrame.args[nonEmptyTabs[1]]
else
return frame:preprocess("<nowiki />")
end
end
end
end

Revision as of 15:02, 19 December 2024

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

local makeEmptyPartEmptyNoFrame = require("Module:Infobox").makeEmptyPartEmptyNoFrame
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 = ""
	local nonEmptyTabs = {}
	for _, i in ipairs(tabNums) do
		if string.find(makeEmptyPartEmptyNoFrame(parentFrame.args[i]), "%S") then
			table.insert(nonEmptyTabs, i)
			local tabLabel = parentFrame.args["label" .. i]
			-- If no label it just uses the number as a label
			if tabLabel then
				if not string.find(tabLabel, "%S") then
					tabLabel = i
				end
			else
				tabLabel = i
			end
			tabberStr = tabberStr ..
				"|-|" ..
				tabLabel ..
				"=<table style=\"width:100%;border-collapse:collapse\">" ..
				parentFrame.args[i] ..
				"</table>"
		end
	end
	-- If there is only one tab, don't generate tabber unless allowOneTab=yes
	if not parentFrame.args["allowOneTab"] == "yes" and #nonEmptyTabs == 1 then
		if string.find(makeEmptyPartEmptyNoFrame(parentFrame.args[nonEmptyTabs[1]]), "%S") then
			return parentFrame.args[nonEmptyTabs[1]]
		else
			return frame:preprocess("<nowiki />")
		end
	end
	-- Return
	if tabberStr == "" then
		return frame:preprocess("<nowiki />")
	else
		return "<tr><td colspan=\"2\" style=\"padding:0;width:min-content\"><div style=\"width:21.6em\">" .. frame:extensionTag("tabber", tabberStr) .. "</div></td></tr>"
	end
end

return p