Jump to content

Module:2-ary truth table: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
test
Line 1: Line 1:
local p = {}
local p = {}

local A_col = {false, false, true, true}
local B_col = {false, true, false, true}


function p.main(frame)
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame)
local args = require('Module:Arguments').getArgs(frame)
-- fill in the table
local table_headings = {
{frame:extensionTag("math",args['A'] or 'A',{}), ''},
{frame:extensionTag("math",args['B'] or 'B',{}), ''}
}
local truth_table = {
{false, false, true, true},
{false, true, false, true}
}
local thick_border = {false, false, true}
local col = 3
while args[6*(col-2) - 1] do
thick_border[col + 1] = string.lower(args[6*(col-2)] or '') == 'thick'
table_headings[col] = {args[6*(col-2) - 1], 'unsortable'}
truth_table[col] = {}
for i = 1,4 do
truth_table[col][i] = (tonumber(args[6*(col-2) - 5]) or 0) == 1
end
col = col + 1
end
local total_cols = col - 1

-- start table
-- start table
root = mw.html.create('table')
root = mw.html.create('table')
Line 12: Line 31:
:addClass('sortable')
:addClass('sortable')
:addClass('two-ary-tt')
:addClass('two-ary-tt')

-- add headings
-- add headings
local A = frame:extensionTag("math",args['A'] or 'A',{})
local B = frame:extensionTag("math",args['B'] or 'B',{})
local row = root:tag('tr')
local row = root:tag('tr')
for col = 1,total_cols do
row:tag('th'):wikitext(A)
local headings = table_headings[col]
row:tag('th'):wikitext(B)
local thick_border = {}
local col = 1
thick_border[col] = true
while args[6*col - 1] do
thick_border[col + 1] = string.lower(args[6*col] or '') == 'thick'
row:tag('th')
row:tag('th')
:addClass('unsortable')
:addClass(headings[2] ~= '' and headings[2] or nil)
:addClass(thick_border[col] and 'border' or nil)
:addClass(thick_border[col] and 'border' or nil)
:wikitext(args[6*col - 1])
:wikitext(headings[1])
col = col + 1
end
end

local total_cols = col - 1
-- add rows
for i = 1,4 do
for i = 1,4 do
row = root:tag('tr')
row = root:tag('tr')
-- A col
row:tag('td')
:addClass('bold')
:addClass(A_col[i] and 't' or 'f')
:wikitext(A_col[i] and 'T' or 'F')
-- B col
row:tag('td')
:addClass('bold')
:addClass(B_col[i] and 't' or 'f')
:wikitext(B_col[i] and 'T' or 'F')
-- Remaining cols
for j = 1,total_cols do
for j = 1,total_cols do
local val = (tonumber(args[6*j - 6 + i]) or 0) == 1
local val = truth_table[j][i]
row:tag('td')
row:tag('td')
:addClass(val and 't' or 'f')
:addClass((j < 3) and 'bold' or nil)
:addClass(thick_border[j] and 'border' or nil)
:addClass(thick_border[j] and 'border' or nil)
:addClass(val and 't' or 'f')
:wikitext(val and 'T' or 'F')
:wikitext(val and 'T' or 'F')
end
end

Revision as of 16:00, 13 March 2024

local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	
	-- fill in the table
	local table_headings = {
		{frame:extensionTag("math",args['A'] or 'A',{}), ''},
		{frame:extensionTag("math",args['B'] or 'B',{}), ''}
	}
	local truth_table = {
		{false, false, true, true},
		{false, true, false, true}
	}
	local thick_border = {false, false, true}
	local col = 3
	while args[6*(col-2) - 1] do
		thick_border[col + 1] = string.lower(args[6*(col-2)] or '') == 'thick'
		table_headings[col] = {args[6*(col-2) - 1], 'unsortable'}
		truth_table[col] = {}
		for i = 1,4 do
			truth_table[col][i] = (tonumber(args[6*(col-2) - 5]) or 0) == 1
		end
		col = col + 1
	end
	local total_cols = col - 1

	-- start table
	root = mw.html.create('table')
		:addClass('wikitable')
		:addClass('sortable')
		:addClass('two-ary-tt')

	-- add headings
	local row = root:tag('tr')
	for col = 1,total_cols do
		local headings = table_headings[col]
		row:tag('th')
			:addClass(headings[2] ~= '' and headings[2] or nil)
			:addClass(thick_border[col] and 'border' or nil)
			:wikitext(headings[1])
	end

	-- add rows
	for i = 1,4 do
		row = root:tag('tr')
		for j = 1,total_cols do
			local val = truth_table[j][i]
			row:tag('td')
				:addClass((j < 3) and 'bold' or nil)
				:addClass(thick_border[j] and 'border' or nil)
				:addClass(val and 't' or 'f')
				:wikitext(val and 'T' or 'F')
		end
	end

	local templatestyles = frame:extensionTag{
		name = 'templatestyles', args = { src = 'Template:2-ary truth table/style.css' }
	}
	
	return templatestyles .. tostring(root)
end

return p