Jump to content

Module:LogicUtils

From Logic World Wiki
Revision as of 16:27, 7 September 2025 by Felipe (talk | contribs) (change style to red and black background)

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

local p = {}

function parse_io(kind, html, str)
	local count = tonumber(str)
	local names = {}

	if count == nil then
		-- str is a list of IO names
		names = mw.text.split(str, ",")
	else
		-- str is a number
		if count == 1 then
			names[1] = kind
		else
			for i=1,count do
				names[i] = string.format("%s %i", kind, i)
			end
		end
	end

	for i, v in ipairs(names) do
		html = html..string.format('<th>%s</th>', v)
	end

	return html, #names
end

p.truth_table = function(frame, args)
	local args = args or frame:getParent().args
	local html = "<table class=\"wikitable\"><tr>"

	if args.caption ~= nil then
		html = html.."<caption>"..args.caption.."</caption>"	
	end

	local html, nInputs = parse_io("Input", html, args.inputs)
	local html, nOutputs = parse_io("Output", html, args.outputs)
	html = html.."</tr>"

	local i = 1
	while args[i] ~= nil do
		html = html.."<tr>"
		local ioCounter = 1
		for token in string.gmatch(args[i], "[^%s]+") do
			local style = "color:white; text-align:center;"
			if token == "0" then
				html = html..'<td style="background-color:#1f1e1e;'..style..'">0</td>'
			elseif token == "1" then
				html = html..'<td style="background-color:#fd140f;'..style..'">1</td>'
			else
				html = html.."<td>"..token.."</td>"
			end
			ioCounter = ioCounter + 1
		end
		html = html.."</tr>"
		
		i = i + 1
	end

	html = html.."</table>"	
	return html
end


return p