Module:LogicUtils: Difference between revisions
Appearance
No edit summary |
change style to red and black background |
||
Line 20: | Line 20: | ||
for i, v in ipairs(names) do | for i, v in ipairs(names) do | ||
html = html.. | html = html..string.format('<th>%s</th>', v) | ||
end | end | ||
return html | return html, #names | ||
end | end | ||
Line 34: | Line 34: | ||
end | end | ||
html = parse_io("Input", html, args.inputs) | local html, nInputs = parse_io("Input", html, args.inputs) | ||
html = parse_io("Output", html, args.outputs) | local html, nOutputs = parse_io("Output", html, args.outputs) | ||
html = html.."</tr>" | html = html.."</tr>" | ||
local i = 1 | local i = 1 | ||
while args[i] ~= nil do | while args[i] ~= nil do | ||
html = html.."<tr>" | html = html.."<tr>" | ||
local ioCounter = 1 | |||
for token in string.gmatch(args[i], "[^%s]+") do | for token in string.gmatch(args[i], "[^%s]+") do | ||
local style = "color:white; text-align:center;" | |||
if token == "0" then | if token == "0" then | ||
html = html.. | html = html..'<td style="background-color:#1f1e1e;'..style..'">0</td>' | ||
elseif token == "1" then | elseif token == "1" then | ||
html = html.. | html = html..'<td style="background-color:#fd140f;'..style..'">1</td>' | ||
else | else | ||
html = html.."<td>"..token.."</td>" | html = html.."<td>"..token.."</td>" | ||
end | end | ||
ioCounter = ioCounter + 1 | |||
end | end | ||
html = html.."</tr>" | html = html.."</tr>" | ||
Line 58: | Line 61: | ||
return html | return html | ||
end | end | ||
return p | return p |
Revision as of 16:27, 7 September 2025
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