Jump to content

Module:2-ary truth table

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 19:32, 12 March 2024 (try lua version of template:2-ary truth table). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

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

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	
	-- start table
	root = mw.html.create('table')
		:addClass('wikitable')
		:addClass('sortable')
		:addClass('two-ary-tt')
	-- add headings
	local row = root.tag('tr')
	row:tag('th'):tag('math'):wikitext(args['A'] or 'A')
	row:tag('th'):tag('math'):wikitext(args['B'] or 'B')
	local thick_border = {}
	local col = 1
	thick_border[col] = false
	while args[6*col - 1] do
		thick_border[col + 1] = (args[6*col] or '').lower() == 'thick'
		row:tag('th')
			:addClass('unsortable')
			:addClass(thick_border[col] and 'border' or nil)
			:wikitext(args[6*col - 1])
		col = col + 1
	end
	local total_cols = col - 1
	for i = 1,4 do
		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
			local val = tonumber(args[6*j - 5 + i]) or 0
			row:tag('td')
				:addClass(val and 't' or 'f')
				:addClass(thick_border[j] and 'border' or nil)
				: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